【问题标题】:How to make discord.py bot say what prefix the server is using?如何让 discord.py 机器人说出服务器使用的前缀?
【发布时间】:2021-05-16 16:51:12
【问题描述】:

我让我的机器人为每台服务器设置一个特定的前缀。我希望它能够通过使用 {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)

我尝试了上面的代码并得到了这个,

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

【问题讨论】:

  • 你为什么不将参数传递给{get_prefix}
  • 因为我还是新手,我真的不知道该怎么做。对不起
  • 你声明了一个带有两个参数的函数def get_prefix(client, message)client & message。为了显示函数在您的嵌入中返回的内容,您必须提供这两个参数。喜欢f'The current prefix for this server is {get_prefix(arg1,arg2)}'
  • 所以需要f'The current prefix for this server is {get_prefix(client, message)}' 吗?
  • 可能会解决它

标签: python discord discord.py


【解决方案1】:

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

【讨论】:

  • 我很高兴你能够让它工作!
猜你喜欢
  • 2021-02-12
  • 2021-09-03
  • 2021-08-01
  • 2021-04-13
  • 2021-10-30
  • 2021-03-19
  • 1970-01-01
  • 2020-06-26
  • 2018-11-21
相关资源
最近更新 更多