【问题标题】:How to change discord bot prefix using command (discord.py)如何使用命令(discord.py)更改不和谐机器人前缀
【发布时间】:2021-10-30 13:49:25
【问题描述】:

我已经在 StackOverflow 中搜索过,并创建了它。但这并不是我想要的……

所以请不要称其为重复:)

顺便说一下我正在使用的代码:

@client.command()
async def change_prefix(ctx, arg=None):
    if str(type(arg)) == "<class 'NoneType'>":
        await ctx.send("Please insert an argument...")
    else:
        client = commands.Bot(command_prefix = str(arg))
        command_prefix = str(arg)

        embed = discord.Embed(title = "Prefix set!",
        description = f"The bot prefix has been set to {arg}",
        color = discord.Color.red())

        now = datetime.now()
        current_time = now.strftime("%H:%M")

        embed.set_footer(text=f"Requested by {ctx.author} at {current_time}")

        await ctx.send(embed=embed)

【问题讨论】:

  • 您能详细说明您要做什么吗?要更改前缀,您需要有一个 json 文件和所有这些。
  • 您是要更改每个公会的command_prefix 还是整个机器人?请相应地编辑您的问题以指定您的需求。
  • 使用if arg == None 而不是if str(type(arg)) == "&lt;class 'NoneType'&gt;"。这是非常低效的。

标签: python discord.py bots


【解决方案1】:

你需要一个数据库来存储不同服务器的前缀。

您不应该像在代码中那样重新初始化 Bot 类。

将您的前缀存储在数据库或其他地方,并创建一个get_prefix 方法并在您的Bot 类中使用它。

async def get_prefix(bot, message):
  # you can do your database queries or things to get prefix. 
  # Finally, Just return prefix as str or list.
  return prefix

然后在你的 Bot 类实例中:

bot = commands.Bot(command_prefix=get_prefix)

此函数将调用每条消息,因此请考虑设置缓存系统以避免数据库问题。

【讨论】:

  • 我不太明白,你能给我完整的代码吗?
猜你喜欢
  • 2023-03-20
  • 2022-01-21
  • 2021-07-07
  • 2019-11-09
  • 2021-03-10
  • 2017-09-13
  • 2020-03-05
  • 2021-06-20
  • 2021-02-12
相关资源
最近更新 更多