Discord 'on_member_join' function not working - python

My on_member_join doesnt seem to work. I wanted my bot to say out the names of the members that joined the sever but it doesnt detect if someone has joined or left.
import discord
from discord.ext import commands
client = commands.Bot(command_prefix = '.')
#client.event
async def on_ready():
print("bot is ready ")
#client.event
async def on_member_join(member):
print(f'{member.name} has joined this server')
#client.event
async def on_member_remove(member):
print(f'{member}was removed')
client.run('*************************')
It is printing "bot is ready" on the terminal so the bot is working. But isn't detecting members leaving or joining pls help.

You're probably using discord python 1.5.0 or above, it's a common error, you just need Intents.
If there's a error, you are supposed to read it, it will redirect you to your bot in the discord dev portal, there you can activate the privileged gateway intents check this out
And add this to your code
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix='.', intents=intents)

i solved this problem to downgrade discord.py
maybe intents option is error.
i'm already using discord api forum intents option but not working
so if u wanna on_member_join and remove events, downgrade your discord.py under version 1.5.0
u type this in console
pip install discord.py==1.4.2
and in your cords remove all about intents cord and activate your cords
that's it.

Related

message.content is empty for bot client (pycord)

I have simplest python program of discord bot
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
#bot.event
async def on_message(msg):
print(msg.content)
bot.run('token')
And it prints just empty string. Before that I tried bot.command() but bot simply doesn't responds to it probably because message is empty so like there's no command. I saw this problem mostly occurs for selfbot clients but in my case client is bot. Would be glad for any help
You have to enable the message intents on https://discord.com/developers/applications
and need to pass them to your commands.Bot
bot = commands.Bot(command_prefix="!", intents=discord.Intents.all())
would be an example of how you can do that.

Discord.py bot commands don't respond when using Heroku

I have a pre made Code with Discord.py and hosted using Heroku, it is currently online except that whenever i use a command, it returns nothing, but when running it locally using Notepad++ the commands are working just fine.
main.py
import discord
from discord.ext import commands
token = 'OBVIOUSLY THE TOKEN IS HERE'
prefix = "?"
intents = discord.Intents.default() # or .all() if you ticked all, that is easier
intents.members = True # If you ticked the SERVER MEMBERS INTENT
bot = commands.Bot(command_prefix=prefix, intents=intents) # "Import" the intents
#bot.event
async def on_ready():
print('Internal has connected to Discord!')
await bot.change_presence(activity=discord.Game(name="Discord"))
#bot.command()
async def ping(ctx):
await ctx.send(f'Pong! In {round(bot.latency * 1000)}ms')
#bot.command()
async def about(ctx):
embed = discord.Embed(title='About me!', description=f'Im an Utility bot, that is coded under discord.py\n\n> Statistics\nLatency: `{round(bot.latency * 1000)}`ms', color=0xEE8700)
await ctx.send(embed=embed)
#bot.command()
async def say(ctx, *, content:str):
await ctx.send(content)
bot.run(token)
Versions:
python= 3.8.8
discord.py= 1.7.3
(Adding this as an answer because I assume someone else will have this problem)
In discord.py version 1.7.3, the messages intent, which is required for viewing message content, is set enabled by default. However, in 2.x you need to set it separately.
You can solve the issue by:
Force discord.py version to go back to 1.7.3 in requirements.txt; or
Explicitly enable the intents by doing intents.message_content = True.

On_member_join and on_member_remove events do not work

I have tried to make a Discord bot which prints the user joining or leaving to the console. However it seems like it's not triggering my Bot. Here's my code:
Here is my current code
import discord
from discord.ext import commands
client = commands.Bot(command_prefix='!')
#client.event
async def on_ready():
print("Bot is ready")
#client.event
async def on_member_join():
print("{member} hat den Server betreten.")
```#client.event
async def on_member_remove():
print("{member} hat den Server verlassen.")
client.run('My Token')
Can someone please help me?
Events that use on_member_join or others related to member events, must require member intents to be enabled. This works to allow these events to run as they are private and should be used carefully.
Intents can be enabled from Discord developer portal, from there you only need to make sure you have enabled Member in intents within the "Bot" category. You'd then need to define and use the intents in your bot code in the section you define your bot or client:
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix='your bot prefix', intents=intents)

Discord.py Enabling Intents for on_member_join Event

I am fixing an old bot from before the intents changes were made and I am trying to just do a simple on_server_join Event but it isn't working, I am not getting any error and assume that this is caused by an issue with intents. I have enabled both privileged intents in the developer portal and tried to implement it into my code however it still isn't functioning correctly.
import discord
from discord.ext import commands
from discord.utils import get
intents = discord.Intents(messages=True)
client = commands.Bot(command_prefix ='-', intents=intents)
#client.event
async def on_member_join(member):
channel = client.get_channel(751225399798923315)
print("Person Joined")
await channel.send("Welcome!")
client.run('Token')
Here is a sample of the code any help is appreciated.
You only enabled intents.messages, you need intents.members for any on_member_* event. Also I suggest you simply enabling default intents + intents.members and everything should be working fine.
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix ='-', intents=intents)
Also make sure to enable them in the developer portal

Discord.py on_member_join not working, no error message

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
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
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.
If you are using discord.py v1.5.0, see the docs for Gateway Intents

Categories