【问题标题】:discord.py on_member_join not working even though the bot is in the server and online即使机器人在服务器中并且在线,discord.py on_member_join 也不起作用
【发布时间】:2020-11-07 16:52:09
【问题描述】:

我一直在尝试制作一个不和谐的机器人,但我遇到了on_member_join 函数的问题。该机器人已获得管理员权限,我在控制台中也没有遇到任何错误

这里是代码

import discord
intents = discord.Intents.default()
intents.members = True
client = discord.Client()

@client.event
async def on_ready():
    print(f'We have logged in as {client.user}')


@client.event
async def on_member_join(member):
    await member.send('welcome !')

client.run('TOKEN')

【问题讨论】:

  • 请添加有关问题的适当详细信息和上下文。同时粘贴您遇到的错误。
  • @SerialLazer 我没有遇到任何错误。除了 on_ready 事件的信息之外,我的终端上没有打印任何内容

标签: python discord discord.py


【解决方案1】:

您需要在 Client() 初始化程序中传递意图

以下是修改后的代码:

import discord
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)

@client.event
async def on_ready():
    print(f'We have logged in as {client.user}')


@client.event
async def on_member_join(member):
    await member.send('welcome !')

client.run('TOKEN')

【讨论】:

    【解决方案2】:

    尝试使用意图。在此版本中,您将需要使用意图。

    https://discordpy.readthedocs.io/en/latest/api.html?highlight=intents#discord.Intents

    Intents 是 discord.py 1.5.0 + 版本所需的新功能 如果您的机器人不使用意图并且您正在使用诸如 on_member_join() 之类的事件,它将无法工作!

    import discord
    from discord.ext import commands
    
    intents = discord.Intents.all()
    bot = commands.Bot(command_prefix='?', intents=intents)
    

    【讨论】:

      猜你喜欢
      • 2021-02-25
      • 2020-11-21
      • 2021-01-16
      • 2020-09-19
      • 1970-01-01
      • 2021-08-05
      • 2020-06-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多