Discord.py-如何从Bot命令中提及Discord用户



真的不是傻瓜吗?解释:
此问题不想提及用户
该问题列出了成员说明,但没有提供完整的示例

我的问题
使用Discord.py(如果标题不够清晰(我正试图找出如何在Discord用户运行命令时向其添加@提及属性

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

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

谷歌答案

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

我的代码(更新:使用@pepic程序员部分修复(

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)

我看过的资源

  • https://www.programcreek.com/python/example/107413/discord.Member
  • https://discordpy.readthedocs.io/en/latest/api.html#message
  • https://github.com/Rapptz/discord.py/blob/master/examples/basic_bot.py#L46-L49

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,如果找不到,则默认为用户调用该命令。

最新更新