欢迎不使用discord.py的新成员

2024-05-12 20:20:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试设置一个启用discord.py的简单bot,但在欢迎新成员时遇到问题。我有以下代码,当一个新成员加入时,bot能够处理Discord发送的默认欢迎消息,但不处理on_member_join()函数中的任何内容。我已经在Discord(网关和成员)中启用了意图,但仍然不明白为什么它不会处理新成员加入。我还测试了一个全新创建的成员,仍然不会触发

import discord

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

client = discord.Client()

@client.event
async def on_ready(): # When ready
    print('We have logged in as {0.user}'.format(client))

@client.event
async def on_member_join(member):
    print('Someone new!')
    await member.send("Welcome")

@client.event
async def on_message(message): # On every message
    if message.author == client.user: # Cancel own message
        return

    if message.content.startswith('?'):
        await message.channel.send('Command')

client.run(DISCORD_TOKEN)

Tags: clienteventmessageasyncondefbot成员
2条回答

这与使用Client而不是Bot无关

您创建了intents变量,但从未使用过它。您应该将它传递到您的discord.Client(),而您没有这样做,因此members意图将始终被禁用

# Original question
intents = discord.Intents.default()
intents.members = True

client = discord.Client()  # <  You're not passing intents here! The variable is never used so intents are disabled

这也是为什么你的答案能解决这个问题:因为你实际上是在使用你的意图

# Your "fix"
client = commands.Bot(command_prefix = "!", intents = intents)  # <  Notice the intents

使用discord.Clientcommands.Bot对这一点没有影响:commands.Bot没有传递意图也不会做任何事情

# This would cause the exact same issue, because intents aren't used
client = commands.Bot(command_prefix = "!")

将您的意图传递到Client也会起作用,就像这样可以解决Bot的问题

client = discord.Client(intents=intents)

编辑:参见答案。将以下正确传递的意图(members=True)更改为bot

不确定为什么,但我需要使用bot命令来解决这个问题

增加:

from discord.ext import commands

改变

client = discord.Client()

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

我认为这是“完全启用”意图所必需的,但我不完全确定为什么这会纠正这个问题

相关问题 更多 >