【问题标题】:pinging multiple users in discord bot在不和谐机器人中ping多个用户
【发布时间】:2021-05-14 07:40:30
【问题描述】:

我在发出命令后尝试“@”特定成员,以下似乎不起作用。

from tokens.token import discord_token
from discord.ext import commands
from discord.utils import get
intents = discord.Intents(messages=True, guilds=True)

client = commands.Bot(command_prefix="!",intents = intents)

@client.event
async def on_ready():
    print("Bot is ready!")

@client.command
async def play(ctx):
    paul = get(ctx.guild.members, name = 'paul')
    john = get(ctx.guild.members, name = 'john')
    await ctx.send("f{paul.mention} {john.mention} lets play games!")
    
client.run(discord_token)

它应该返回类似的东西

MyBotName: @john @paul lets play games!

谢谢!

【问题讨论】:

  • 您需要启用 Intent,更多信息请参考:discordpy.readthedocs.io/en/latest/intents.html 同时确保启用特权成员 Intent。
  • @ŁukaszKwieciński 添加意图后,我仍然收到错误Command raised an exception: AttributeError: 'NoneType' object has no attribute 'mention'
  • 我看到您实际上启用了一些意图,尽管您缺少 intents.members,正如我在上面的评论中所说,请确保启用它们
  • @ŁukaszKwieciński 有没有办法遍历多个人并提及他们?而不是手动指定 get?
  • “循环多人”是什么意思?您的意思是遍历成员名称/ID 列表?

标签: python python-3.x discord discord.py


【解决方案1】:

您在代码中使用了错误的 f-string。 f 必须在字符串之前:

await ctx.send(f"{paul.mention} {john.mention} lets play games!")✅
await ctx.send("f{paul.mention} {john.mention} lets play games!")❌

另外,如果您没有启用 Intent,请转到开发人员门户并启用 Intent。然后将其添加到您的机器人的属性中:

bot = commands.Bot(command_prefix="!", discord.Intents.all()) # or what you need

【讨论】:

    猜你喜欢
    • 2018-01-22
    • 1970-01-01
    • 2022-06-12
    • 2022-01-26
    • 2021-11-25
    • 2021-09-17
    • 2018-03-20
    • 2021-03-28
    • 2021-04-19
    相关资源
    最近更新 更多