I am trying to make a discord bot with the Discord.py library. The commands with the @client.command() decorator work fine for me, but none of the event ones that I tried work.
@client.event
async def on_member_join(member):
channel = client.get_channel(ChannelId) #I did define channel Id in my code
await channel.send("someone has joined")
@client.event
async def on_member_remove(member):
print("Someone has left")
I would expect this to output to the terminal or in the channel id I put in, but nothing appears, not even an error message.
*I used client. for all functions.
*I am doing this on mac.
Im not quite sure why its working, I do not get any error messages, and I cant seem to find anyone else with this problem.
Thanks in advance
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
With version >1.5.0 you can do something like this:
import discord
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
@client.event
async def on_member_join(member):
await member.send("Welcome!")
You also need to enable intents in the developer portal in https://discord.com/developers/applications. Bot > Bot > Presence & Server Members Intents > Toggle On
Method 2
import discord intents = discord.Intents.all() discord.member = True bot = commands.Bot(command_prefix="[",intents = intents)
and you need to go to developer portal –> applications(select your bot)
and under the setting have a bot. Click it under the page has PRESENCE INTENT and SERVER MEMBERS INTENT you need to open it. That will be working.
Method 3
If you are using discord.py v1.5.0, see the docs for Gateway Intents
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0