【问题标题】:on_member_join not working even with intentson_member_join 即使有意图也无法正常工作
【发布时间】:2021-06-30 21:49:58
【问题描述】:

我正在为我的服务器制作一个机器人,我希望它自动为刚加入的成员分配一个角色。我在谷歌上四处寻找答案,它说要使用意图。我使用了它们,甚至在开发门户中搞砸了,但它仍然无法正常工作。

import discord
from discord.utils import get

bot = discord.Client(intents=intents)
bot = commands.Bot(command_prefix="!")
Default_Role = 'Starineer'

@bot.event
async def on_member_join(member):
    role = get(member.guild.roles, name=Default_Role)
    await member.add_roles(role)

(如果我将 bot = discord.Client(intents=intents) 更改为 client = discord.Client(intents=intents),我将无法使用的命令)

【问题讨论】:

  • 你为什么使用discord.Clientcommands.bot

标签: discord.py


【解决方案1】:

不能定义bot 两次。请决定您要使用什么。

其次要通过discord.utils.get获取角色,只传名字是不行的。

这是一个例子:

from discord.utils import get

intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix="YourPrefix", intents=intents)

@client.event / @bot.event # Depends on how you defined it
async def on_member_join(member):
    role = discord.utils.get(member.guild.roles, name="Test") # Get the role (ID works too)
    await member.add_roles(role) # Add the role
    print(f"{role} assigned to {member}!")

记住还要将意图导入到您的代码中,而不仅仅是激活它们。如果你像例子中那样做,你会没事的。

我们做了什么:

  • 通过名字或ID获取角色->name="Test"/id=RoleID
  • 已导入discord.utils.get 以获取角色。
  • 打印出结果,看看它是否有效。

【讨论】:

  • 谢谢!现在完美运行
猜你喜欢
  • 2019-03-15
  • 1970-01-01
  • 1970-01-01
  • 2020-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多