I
I
Ivan2021-11-07 23:10:03
Python
Ivan, 2021-11-07 23:10:03

I wanted to make a system so that I could change the prefix, I did, but the bot ... can you help (discord.py)?

I wanted to make a system so that I could change the prefix, I did it, but the bot does not want to respond to the prefix for some reason, can you help?

The code

def get_prefix(bot, message):
  with open("prefixDB.json", "r") as f:
      prefix = json.load(f)
  return prefix[str(message.guild.id)]

bot = commands.Bot(command_prefix = commands.when_mentioned_or(get_prefix), help_command = None)
bot.remove_command("help")


Mistake
Traceback (most recent call last):
  File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.9/site-packages/discord/client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.9/site-packages/discord/ext/commands/bot.py", line 979, in on_message
    await self.process_commands(message)
  File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.9/site-packages/discord/ext/commands/bot.py", line 975, in process_commands
    ctx = await self.get_context(message)
  File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.9/site-packages/discord/ext/commands/bot.py", line 909, in get_context
    raise TypeError("Iterable command_prefix or list returned from get_prefix must "
TypeError: Iterable command_prefix or list returned from get_prefix must contain only strings, not function

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tatsuki1, 2021-11-08
@botpython

def get_prefix(client, message):
    with open('prefixes.json', 'r') as f:
        prefixes = json.load(f)

    return prefixes[str(message.guild.id)]



client = commands.Bot(command_prefix = get_prefix, intents = discord.Intents.all())
client.remove_command('help')

@client.event
async def on_guild_join(guild):
    with open('prefixes.json', 'r') as f:
        prefixes = json.load(f)

    prefixes[str(guild.id)] = 't.'

    with open('prefixes.json', 'w') as f:
        json.dump(prefixes, f, indent=4)

@client.event
async def on_guild_remove(guild):
    with open('prefixes.json', 'r') as f:
        prefixes = json.load(f)

    prefixes.pop[str(guild.id)] = 't.'

    with open('prefixes.json', 'w') as f:
        json.dump(prefixes, f, indent=4)

@client.command(aliases = ['prefix', 'refix'])
async def setprefix(ctx, prefixset):
    if (not ctx.author.guild_permissions.manage_guild):
        await ctx.send('Для этой команды требуются права **Управление сервером**')
        return

    if (prefixset == None):
        prefixset = 't.'


    with open('prefixes.json', 'r') as f:
        prefixes = json.load(f)

    prefixes[str(ctx.guild.id)] = prefixset

    with open('prefixes.json', 'w') as f:
        json.dump(prefixes, f, indent=4)

    prefix = discord.Embed(description = f'Вы изменили префикс бота на `{prefixset}`', color = discord.Colour.random())
    await ctx.send(embed = prefix)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question