我正在用 Python 创建一个 Discord 机器人,目前我想添加一个功能,当机器人使用命令 _best
提及随机用户时
我尝试了这段代码:
if message.content.startswith('_best'):
Channel = message.channel
randomMember = random.choice(Channel.members)
await message.channel.send(f'{randomMember.mention} is the best')
但是机器人一直在提到自己!有什么想法吗?
最佳答案
意图
您必须包含意图,如下所示:
intents = discord.Intents.all()
Bot = discord.commands.Bot(intents=intents)
您必须启用 Discord developer portal
中的意图.
成员
最好使用此代码:
member = random.choice(message.guild.members)
因为它包括公会的所有用户。
命令
为什么不使用discord.Command
?像这样:
@Bot.command()
async def best(ctx):
member = random.choice(ctx.guild.members)
ctx.send(member.mention)
关于python - Discord.py Bot - 如何提及随机用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69584736/