Discord.py on_member_join event - python

I need help with my code, i wanted to make my bot sends a message on the first channel from the top when a particular member joins, but i got error with if member.id == *id*
#bot.event
async def on_member_join(member):
if member.id == *my id here, i just dont want to show it*
await message.channel.send('Nice guy joined the server!')
#bot.event
async def on_member_join(member):
if member.id == 660959014691143691:
await member.guild.text_channels[0].send('Nice guy joined the server!')

You haven't defined the message so you can't use message.channel.send. If you just want to send the first channel of the guild, that's simple to do with guild.text_channels.
#bot.event
async def on_member_join(member):
if member.id == *my id here, i just dont want to show it*
await member.guild.text_channels[0].send('Nice guy joined the server!')

Related

How can my discord bot send a welcome message and give a role

I want that my discord bot sends a message on a specific channel and give a specific role everytime a new user joins the server
But my bot is doing nothing and I'm getting no errors
import discord
from keep_alive import keep_alive
class MyClient(discord.Client):
#Beim Einloggen
async def on_ready(self):
print("BOT is online")
async def on_member_join(member):
role = discord.utils.get(member.server.roles, id=<role_id>)
channel = MyClient.get_channel(<channel_id>)
await MyClient.add_roles(member, role)
await channel.send(f"Hello {member} nice to see you!")
Code for the user greeting (in a cog):
#commands.Cog.listener()
async def on_member_join(self, member):
channel = member.guild.system_channel
if channel is not None:
await channel.send(f"Welcome to the server {member.mention}!")
Basically, you are missing theese #commands.Cog.listener()'s
Try this! (This isn't used in a cog)
#client.event
async def on_member_join(member):
if member.guild.id !=<YOUR_GUILD_ID>:
return
welcomerole = discord.utils.get(guild.roles, name="<ROLE_NAME>")
await member.add_roles(welcomerole)
channel = client.get_channel(<WELCOME_CHANNEL_ID)
await channel.send("<WHAT_YOU_WANT>")
This makes it so when someone joins YOUR server it will send a custom message of your choice to the welcome channel
MAKE SURE YOU ADD YOUR GUILD ID IN <YOUR_GUILD_ID>
MAKE SURE YOU ADD A CUSTOM MESSAGE WHEN A MEMBER JOINS IN <WHAT_YOU_WANT>
MAKE SURE YOU ADD YOUR WELCOME CHANNEL ID IN <WELCOME_CHANNEL_ID>
MAKE SURE YOU ADD THE ROLE NAME THAT YOU GET WHEN A MEMBER JOINS IN <ROLE_NAME>

I want my discord bot to respond if a specific message is coming from a specific user

The best solution I've managed to find so far is to check the user id.
#client.event
async def on_message(message):
if(message.author == client.user): #if message is coming from the bot itself
return
if(message.author.id == 103001222035981612 and message.content.startswith == 'Yo'):
await message.channel.send('Hello {}'.format(message.author.name))
#await message.channel.send(message.content)
client.run(TOKEN)
What ends up happening is the bot responds regardless of what it is I say, which is not what I want it to do. I've tried some other ways of doing it but none of them were of any help. I appreciate any help I can get. Thanks!
Make sure to add this at the very end
*await client.process_commands(message)*
Example:
#client.event
async def on_message(message):
if message.author == client.user:
return
if client.user.mentioned_in(message):
await message.channel.send("hey!")
await client.process_commands(message)

Is there a way for a discord bot to respond to a mention of a specific user using Discord.py?

I would like my bot to be able to reply to a mention of a specific user (ex.. one person mentions my personal account and the bot responds saying that I'm currently not here)
Is there a way to do this using a format similar to this?
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('#user_id'):
await message.channel.send('Im not here leave a message!')
You would need to use the specific format that discord bots receive mentions. The format is <#!user_id>.
#client.event
async def on_message(message):
if ("<#!put user id here>" in message.content):
await message.channel.send("Im not here leave a message!")
Example for how applied this and it worked for me
#client.event
async def on_message(message):
if ("<#!348256959671173120>" in message.content):
await message.channel.send("Im not here leave a message!")
Discord Member objects have a .mentioned_in(message) method.
WIZARD_ID = 123456 # <- replace with user ID you want
async def on_message(message):
wizard_of_oz = message.guild.get_member(WIZARD_ID)
if wizard_of_oz.mentioned_in(message):
await message.channel.send("Who dares summon the great Wizard of OZ?!")
If you also want to condition on if the user is mentioned by role you need to check the role_mentions in the message, too. So a more complete example is as follows:
def was_mentioned_in(message: discord.Message, member: discord.Member) -> bool:
"""
Whether or not the member (or a role the member has) was mentioned in a message.
"""
if member.mentioned_in(message):
return True
for role in message.role_mentions:
if role in member.roles:
return True
return False
#client.event
async def on_message(message):
wizard_of_oz = message.guild.get_member(WIZARD_ID)
if was_mentioned_in(message, wizard_of_oz):
await message.channel.send("Who dares summon the great Wizard of OZ?!")

Discord Python Rewrite - Slice Display Name

I made a working AFK code, but i need it to slice (delete) '[AFK]' if the user sends a message. The code is:
#client.command()
async def afk(ctx, *, message="Dind't specify a message."):
global afk_dict
if ctx.author in afk_dict:
afkdict.pop(ctx.author)
await ctx.send('Welcome back! You are no longer afk.')
await ctx.author.edit(nick=ctx.author.display_name(slice[-6]))
else:
afk_dict[ctx.author] = message
await ctx.author.edit(nick=f'[AFK] {ctx.author.display_name}')
await ctx.send(f"{ctx.author.mention}, You're now AFK.")
I got no errors. i only need await ctx.author.edit(nick=ctx.author.display_name(slice[-6])) to be working and i'll be happy.
You need to have a on_message event. Something like this should work.
#client.event
async def on_message(message):
if message.author in afk_dict.keys():
afk_dict.pop(message.author.id, None)
nick = message.author.display_name.split('[AFK]')[1].strip()
await message.author.edit(nick=nick)
I also recomend you to store user id's instead of usernames.
To do that you just have to use member.id instead of member.

Sending a message on a user join event - discord.py

I've been programming a bot that welcomes a user when they join but it doesn't seem to be working.
async def on_member_join(member):
channel = member.channel
await channel.send(f'{member} has arrived')
You haven't specified where the channel is that you want to send the message to, and discord.Member doesn't have an attribute called channel.
You'll have to get the channel via its ID, like so:
async def on_member_join(member):
channel = bot.get_channel(112233445566778899) # replace id with the welcome channel's id
await channel.send(f"{member} has arrived!")
If you want to, you can also get it via its name:
async def on_member_join(member):
channel = discord.utils.get(member.guild.text_channels, name="welcome")
await channel.send(f"{member} has arrived!")
References:
on_member_join()
Client.get_channel()
Guild.text_channels
Member.guild
utils.get()
#client.event
async def on_member_join(member):
for channel in member.guild.channels:
if str(channel) == "member-log":
await channel.send(f"""Welcome {member.mention}!""")
This might help too. Here, we are checking if the channel name is member-log and then sending a welcome message to that channel.

Categories