Reputation: 13
I am trying to make a discord bot in python that will send a simple welcome message when someone joins the server. I looked into a number of youtube tutorials and tried many different ways to make it work but for some reason it is not working.
There is no error in the code. I have an on_message(message)
function in my code just to test the bot and that function works perfectly.
On the other hand, the on_member_join
function is the one that is not working. I have the member intents function enabled on my discord developer portal and the bot has admin permissions. I tried sending the message as an intent and it still doesn't work.
Edit 1 : As per the comments I have deleted the second definition of client. Still cant get the bot to show the welcome message. The on_message(message)
works.
Edit 2: I solved the error. The error was due to me overwriting the definition of client and also due to a typo. Thanks to everyone that helped!
import discord
from discord.ext import commands
token = "I have my token here"
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents = intents)
@client.event
async def on_ready():
print("Bot is ready")
#respond hello to hi (for testing if the bot works)
@client.event
async def on_message(message):
if message.content == 'hi':
await message.channel.send('Hello')
#main function
@client.event
async def on_member_join(member):
guild = client.get_guild(I have my server ID here) #server id
channel = guild.get_channel(I have my channel ID here) #channel id
await channel.send(f'Welcome to the server {member.mention}! ') #edit this line to edit message
client.run(token)
Upvotes: 1
Views: 588