Discord.py welcome bot on_member_join event not getting callded

强颜欢笑 提交于 2020-11-25 03:56:25

问题


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')

回答1:


FOR discord.py >= 1.5

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)

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.




回答2:


You forgot the brackets for the decorators.

@client.event

should be

@client.event()

EDIT: Not it, apparently. Will update this answer when OP replies with more information.



来源:https://stackoverflow.com/questions/64141932/discord-py-welcome-bot-on-member-join-event-not-getting-callded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!