您好,我正在尝试为我的机器人创建一个代码,该代码可以 ping 不和谐中的特定成员,但遇到问题成员是必需的参数,但缺少。我尝试搜索它并尝试修复它,但没有任何效果。我对 python 很陌生,不知道如何修复它。
我的代码供参考
import discord, datetime, time
import os
from discord.ext import commands
from discord.ext.commands import Bot
from discord.utils import get
member = 548378867723665409
BOT_PREFIX = ("!")
bot = commands.Bot(command_prefix=BOT_PREFIX)
@bot.command()
async def pong(ctx, member : discord.Member):
await ctx.send('test')
await ctx.send(f"PONG {member}")
@bot.event
async def on_ready():
print ("------------------------------------")
print ("Bot Name: " + bot.user.name)
print ("------------------------------------")
bot.run(os.getenv('TOKEN'))
答案 0 :(得分:0)
您收到该错误是因为您没有传递任何成员参数。您的命令应如下所示!pong @member
。该成员应在您的消息中提及。我注意到您已经初始化了一个具有成员 ID 的全局变量 member
。如果您想提及该成员而不是将成员对象作为参数传递,则必须这样做:
memberID = 548378867723665409
@bot.command()
async def pong(ctx):
member = await bot.fetch_user(memberID)
await ctx.send(f"PONG {member.mention}")
答案 1 :(得分:0)
如果您只想 ping 消息的作者,有一种简单的方法可以做到这一点。您将使用 {message.author.mention}
如何在代码中使用它的一个例子是:
if i == "hi":
await message.channel.send(f' Hello! {message.author.mention}')
注意:如果你想ping某人并说更多,你需要一个f字符串:
(f'{message.author.mention} hello')