【问题标题】:How do I mention a member in a text? Discord bot python如何在文本中提及成员?不和谐机器人 python
【发布时间】:2021-11-07 12:17:44
【问题描述】:
if message.content.startswith('!givecookie @member'):
   await message.channel.send('Hello, @member, here is your cookie~ :cookie:')

那么当提及某人时,如何在命令中添加成员(@member)?该命令应该做的是 - 给一个成员 cookie。

【问题讨论】:

    标签: python discord discord.py userid


    【解决方案1】:

    首先,你不应该使用 if message.content.startswith(“!givecookie @member") 最好使用声明客户端 client = commands.Bot(command_prefix="!”)。 之后就可以使用了

    @client.command()
    async def givecookie(ctx, member : commands.MemberConverter):
      await ctx.send(f"Hello, {member.mention}, here is your cookie~ :cookie:")
    

    这意味着您的机器人现在有前缀“!”,并且使用@client.command 可以发出更多可以在不和谐中使用的命令。此命令中传递的参数 (ctx, member : commands.MemberConverter) 分别表示您希望将 cookie 提供给的上下文和特定成员。 ctx.send(f“Hello, {member.mention}, here is your cookie~ :cookie:") 基本上会发送消息,但它也会 ping 成员。告诉机器人发送“@member”将使其真正发送“@member”。此外,该命令仅在您使用“!givecookie @member”开始消息时有效,但如果您使用“!give cookie {pinging那个特定的成员}”。

    总之,你应该使用 client.command() 来使用命令而不是检查消息的内容。

    API 参考

    https://discordpy.readthedocs.io/en/stable/index.html

    前缀: https://discordpy.readthedocs.io/en/stable/ext/commands/api.html?highlight=commands#prefix-helpers

    命令: https://discordpy.readthedocs.io/en/stable/ext/commands/api.html?highlight=commands#commands

    【讨论】:

    • 它应该可以工作,但现在它在commands.Bot(command_prefix="!”)commands.MemberConverter 中显示一个错误,说“名称“命令”未定义”。你知道为什么吗?
    • 对不起,我忘了提到你还必须在 python 文件的顶部使用from discord.ext import commands 导入命令。
    猜你喜欢
    • 2019-02-02
    • 1970-01-01
    • 2020-05-18
    • 2020-11-08
    • 2020-11-18
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 2021-05-27
    相关资源
    最近更新 更多