python - Discord.py - 如何从 Bot Command 中提及 Discord 用户

标签 python discord.py

<分区>

真的不是骗子吗?解释:
This question is not looking to (at)mention a user
This question lists member.mention but does not provide a complete example

我的问题
使用 Discord.py(如果标题不够清楚)我试图弄清楚如何在运行命令时向 Discord 用户添加 @mention 属性。

示例:
用户输入:$99
机器人输出:@User 你的报价是:Bingpot!

更新:2019 年 10 月 24 日
- @epic-programmer 指出了一个非常严重的复制粘贴错误,我已经修复了 :)
- 该修复程序将“member”设置为命令的参数。我想要的是获取成员显示名称,并在命令输出中使用它(作为 @mention)

谷歌答案

@commands.command()
async def mention_ping(self, ctx, member : discord.Member):
    await ctx.send(f"PONG {member}")

我的代码(更新:使用@epic-programmers 部分修复)

import random
import discord

from discord.ext import commands

#----
tokenfile = 'token.txt'
with open(tokenfile) as tf:
    line = tf.readline()
    TOKEN = line.rstrip()
#----

bot = commands.Bot(command_prefix='$')

@bot.command(name='99', help='Responds with a random quote from Brooklyn 99')
async def nine_nine(ctx, member : discord.Member):
    brooklyn_99_quotes = [
        'I\'m the human form of the 💯 emoji.',
        'Bingpot!',
        (
            'Cool. Cool cool cool cool cool cool cool, '
            'no doubt no doubt no doubt no doubt.'
        ),
    ]

    response = random.choice(brooklyn_99_quotes)
    await ctx.send("{} your quote is: {}".format(member, response))

bot.run( TOKEN)

我看过的资源

最佳答案

member 参数位于错误的位置。尝试将其移动到 def 语句。

@bot.command(name='99', help='Responds with a random quote from Brooklyn 99')
async def nine_nine(ctx, member : discord.Member):
    brooklyn_99_quotes = [
        'I\'m the human form of the 💯 emoji.',
        'Bingpot!',
        (
            'Cool. Cool cool cool cool cool cool cool, '
            'no doubt no doubt no doubt no doubt.'
        ),
    ]

    response = random.choice(brooklyn_99_quotes)
    await ctx.send("{} your quote is: {}".format(member, response))

bot.run( TOKEN)

编辑:为了提及调用命令的当前用户,您可以:

from typing import Optional

@bot.command(name='99', help='Responds with a random quote from Brooklyn 99')
async def nine_nine(ctx, member : Optional[discord.Member] = None):
    member = member or ctx.author
    brooklyn_99_quotes = [
        'I\'m the human form of the 💯 emoji.',
        'Bingpot!',
        (
            'Cool. Cool cool cool cool cool cool cool, '
            'no doubt no doubt no doubt no doubt.'
        ),
    ]

    response = random.choice(brooklyn_99_quotes)
    await ctx.send("{} your quote is: {}".format(member, response))

bot.run( TOKEN)

此代码块将检查 ping,如果未找到,则默认为调用命令的用户。

关于python - Discord.py - 如何从 Bot Command 中提及 Discord 用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58532552/

相关文章:

python - vim 正确缩进 python 片段

python - 分割刻度标签或包裹刻度标签

python - Django URL 类型错误 : view must be a callable or a list/tuple in the case of include()

discord.py - 如何为斜杠命令的响应添加 react ?

python - 如何使用discord py连续编辑你的昵称

python - 如何更改部分 tkinter 标签的字体样式

python - 我可以在我的 Apache 服务器上运行 Bottle.py 而不中断我的 PHP 应用程序吗?

python - Discord.py 尝试循环日志并发送到服务器时卡住

python - Discord.py 重写冷却命令

python - 不和谐.py |为特定用户添加角色