如何让 discord.py bot 说出服务器使用的前缀?

时间:2021-05-16 08:37:27

标签: python discord discord.py

我的机器人为每台服务器设置了一个特定的前缀。我希望它能够通过使用 {prefix}help 来说明它用于该服务器的前缀。下面是我的前缀代码。

def get_prefix(client, message):
    with open('prefixes.json', 'r') as f:
        prefixes = json.load(f)
    return prefixes[str(message.guild.id)]

bot = commands.Bot(command_prefix=(get_prefix), intents=discord.Intents.all())
bot.remove_command("help")

这里是修改前缀。

@bot.command(pass_context=True)
@commands.has_permissions(administrator=True)
async def changeprefix(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}')

这是我必须显示服务器正在使用的当前前缀的代码。这只是我的帮助命令的一部分。

  embed.add_field(name="Current Prefix", value=f'The current prefix for this server is {get_prefix}', inline=False)
  embed.set_footer(text="I'm strongly recommened for FAMILY FRIENDLY servers!")
  await ctx.send(embed=embed)

我尝试了上面的代码并得到了这个, enter image description here

我问我怎样才能让机器人说,“这个服务器的前缀是 {prefix}

1 个答案:

答案 0 :(得分:1)

您的代码正在输出一个函数对象,因为这是您要求的。这是 Python 中一个非常常见的错误,也是我们时不时陷入的陷阱。您应该可以通过将值更改为 get_prefix(client, ctx.message) 来解决此问题。