python - Discord.py Bot - 如何提及随机用户

标签 python discord bots

我正在用 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/

相关文章:

python - 如何为列表中的每个项目多次调用 discord.py 函数?

c - IRC 机器人故障

python - 命令行与 IDLE 中的 dir() 函数

python - pyDrive 给出 FileNotDownloadableError 错误

javascript - 如何在 Selenium 中执行 JavaScript 并获取数据?

python - 当有人加入时创建规则协议(protocol)

javascript - 删除被禁止用户的消息?

Python:从列表中删除逗号,以便我可以使用 pandas 将数据导入到 Excel 中的单独单元格中

python - 引发 MissingRequiredArgument(参数)discord.ext.commands.errors.MissingRequiredArgument : cooldown is a required argument that is missing

javascript - 尝试在嵌入中使用时,message.author.joinedAt 输出未定义