GuildPrefix Command Confused
Question:
@bot.command()
async def guildprefix(ctx, prefix):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
prefixes[str(ctx.guild.id)] = prefix
with open('prefixes.json', 'w') as f:
json.dump(prefixes, f, indent=4)
await ctx.send(f'Prefix changed to: {prefix}')
name=f'{prefix}BotBot'
def get_prefix(bot, message):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
prefixes[str(message.guild.id)] = '.'
return prefixes[str(message.guild.id)]
It logs the prefix but won’t send when I do the prefix that I set it to
Answers:
Your get_prefix
method is setting the prefix to ".".
def get_prefix(bot, message):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
if str(message.guild.id) not in prefixes:
# only set the default if it's not already in the file
prefixes[str(message.guild.id)] = '.'
return prefixes[str(message.guild.id)]
Now get_prefix
will return the value in the file or "." if the guild ID isn’t already in the file.
@bot.command()
async def guildprefix(ctx, prefix):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
prefixes[str(ctx.guild.id)] = prefix
with open('prefixes.json', 'w') as f:
json.dump(prefixes, f, indent=4)
await ctx.send(f'Prefix changed to: {prefix}')
name=f'{prefix}BotBot'
def get_prefix(bot, message):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
prefixes[str(message.guild.id)] = '.'
return prefixes[str(message.guild.id)]
It logs the prefix but won’t send when I do the prefix that I set it to
Your get_prefix
method is setting the prefix to ".".
def get_prefix(bot, message):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
if str(message.guild.id) not in prefixes:
# only set the default if it's not already in the file
prefixes[str(message.guild.id)] = '.'
return prefixes[str(message.guild.id)]
Now get_prefix
will return the value in the file or "." if the guild ID isn’t already in the file.