【问题标题】:Discord.py Bot - How to mention random userDiscord.py Bot - 如何提及随机用户
【发布时间】:2023-03-24 20:45:01
【问题描述】:

我正在用 Python 制作 Discord Bot,目前我想添加一个功能,当 bot 使用命令 _best 提及随机用户时

我试过这段代码:

if message.content.startswith('_best'):
    Channel = message.channel
    randomMember = random.choice(Channel.members)
    await message.channel.send(f'{randomMember.mention} is the best')

但是机器人一直在提到自己!有什么想法吗?

【问题讨论】:

  • message.channel.send(f'<@{randomMember.id}> is the best') ?
  • @GhostOps,机器人仍然提到自己:(
  • 那里的频道是什么?语音通道、文本通道还是服务器?
  • 这是文字频道
  • 所以你想在服务器中随机ping一个人,不是吗?

标签: python discord bots


【解决方案1】:

尝试random.choice(message.guild.members)而不是random.choice(Channel.members)来获取服务器中的成员列表

因为从文本频道获取随机成员与从整个服务器获取相同

然后试试message.channel.send(f'{randomMember.mention} is the best')

此外,机器人只会提及自己,除非您通过转到 https://discord.com/developers/applications/{your_bot's_client_id_goes_here}/bot 为您的机器人启用 Privileged Gateway Intents 并启用 Server Members Intent 以使您的bot 访问服务器成员列表

并且还为on_message 中的每个命令使用@bot.command 而不是大量的if-else 语句

代码:

import discord
from discord.ext.commands import Bot

intents = discord.Intents.default()
intents.members = True

bot = commands.Bot('_', intents=intents)

@bot.command()
async def best(ctx):
    randomMember = random.choice(ctx.guild.members)
    await ctx.send(f'{randomMember.mention} is the best')

如果上面的代码仍然不适合你...

改变这个

@bot.event
async def on_message(message):
    # do all your stuff here

进入这个

@bot.listen('on_message')
async def whatever_you_want_to_call_it(message):
    # do stuff here
    # do not process commands here

如果您仍想将 on_message 与 if 语句一起使用,请使用此代码

@bot.event
async def on_message(message):
    randomMember = random.choice(message.guild.members)
    await message.channel.send(f'{randomMember.mention} is the best')

如果它不适合你,请告诉我......

【讨论】:

  • 感谢您的回复!但是,不幸的是,机器人仍然提到了他自己。 P.S.:顺便说一下,Bot 有管理员权限:)
  • @Aaron 对不起,现在我编辑了答案,检查一下!
  • 感谢您的建议!但是,可悲的是,我的机器人仍然在提到他自己。 1. 我试图将你的答案与@FLAK-ZOSO 的答案结合起来,所以我得到了this(这是一张 Pinterest 图片,别担心)。结果:Bot 仍然只提到他自己。 2. 好的,我会听你的建议,发出命令@bot.command,但是我必须更改所有机器人的命令吗? (我不太喜欢更改我的机器人的 114 条命令,你知道 :)。)
  • @Aaron 你喜欢intents.members = True 吗?而且,您必须在我在答案中提到的网站中启用一个选项。你也做过吗?而且,我猜你还没有将意图传递给机器人类......
  • 是的,我添加了intents.members = True,但同样的问题。另外,是的,我检查了特权网关意图。但我不明白的一件事是 - 如何将意图传递给机器人类? (我是编码的初学者:)。)。谢谢
【解决方案2】:

意图


您必须包含意图,如下所示:

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)

【讨论】:

  • 我有这个错误然后:Bot = discord.Bot(intents=intents) AttributeError: module 'discord' has no attribute 'Bot'
  • 您需要将 bot 变量分配给您的主要 python 命令
  • 我修好了,它是 discord.commands.Bot。然后我的意思是你必须在 Bot 声明中添加意图。
  • 非常感谢!但是,不幸的是,我收到了this 错误(对于低质量的图像感到抱歉)。 还有:如果我将命令更改为discord.Command 类型,我是否必须更改所有机器人的命令? (我的意思是,我真的不喜欢更改 114 个命令,你知道 :)。)
  • @Aaron 其实是discord.ext.commands.Bot
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-13
  • 1970-01-01
  • 2021-07-04
  • 2019-04-01
  • 1970-01-01
  • 2021-02-19
相关资源
最近更新 更多