Discord.py 欢迎机器人 on_member_join 事件未被调用 [英] Discord.py welcome bot on_member_join event not getting callded

查看:30
本文介绍了Discord.py 欢迎机器人 on_member_join 事件未被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近对使用不和谐机器人很感兴趣,据我所见,这段代码应该可以工作,但不是......我只是在玩 API,因为它很有趣,所以我对此很陌生.我只是希望机器人在某人加入时欢迎他们.

I've been interested in working with discord bots lately, and from what I'm seeing this code should work but it is not... I'm simply just playing around with the API because it's fun so I'm pretty new with this. I just want the bot to welcome someone when they join.

import discord

client = discord.Client()

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))
    channel = client.guilds[0].get_channel(CHANNEL ID)
    await channel.send("Bot online")

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('$hello'):
        await message.channel.send('Hello!')

@client.event
async def on_member_join(member):
    print(f'{member.name} has joined the server')
    channel = client.guilds[0].get_channel(CHANNEL ID)
    print(channel)
    await channel.send(f'{member.name} has joined the server')

@client.event
async def on_member_remove(member):
    print(f'{member.name} has left the server')
    channel = client.guilds[0].get_channel(CHANNEL ID)
    print(channel)
    await channel.send(f'{member.name} has left the server')

client.run('TOKEN HERE')

推荐答案

FOR discord.py >= 1.5

1.5 添加了对 Gateway Intents 的支持,默认情况下您的机器人不支持不能像以前的版本那样访问公会成员.如果您的机器人位于少于 100 台服务器中,那么您无需验证即可启用这些意图.您应该在 Discord 开发者门户的 Bot 页面底部看到这些设置.如果您同时启用两者,那么您需要更改您的客户端(或 commands.Bot)实例化:

1.5 adds support for Gateway Intents, by default your bot doesn't have access to guild members like it did in previous versions. If your bot is in less than 100 servers then you can enable these intents without verification. You should see these settings at the bottom of your Bot page on the Discord Developer Portal. If you enable both, then you need to change your Client (or commands.Bot) instantiation as such:

intents = discord.Intents.all()
client = discord.Client(intents=intents)

当我测试这个时,事件正在触发,但我敢打赌你的问题在于:

When I test this, the event is firing, but I'll bet your issue lies in the line:

channel = client.guilds[0].get_channel(CHANNEL ID)

使用 client.get_channel 以确保您实际上是在抓取预期的频道,discord 上的所有频道 ID 都是唯一的,因此您不需要使用公会对象.CHANNEL ID 也不是一个有效的变量,但我猜你只是修改了它.

It will be far more reliable to just use use client.get_channel to ensure you are actually grabbing the intended channel, all channel IDs on discord are unique so you do not need to use a guild object. Also CHANNEL ID will not be a valid variable, but I am guessing you just redacted it.

这篇关于Discord.py 欢迎机器人 on_member_join 事件未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆