I am making a discord bot that changes a voice channels' name when a user joins or leaves to the amount of members on the server. My issue is that when a user leaves, it doesn't update. Any help is appreciated.
#bot.event
async def on_raw_member_remove(member):
channel = discord.utils.get(member.guild.channels, id=973603264639668248)
await channel.edit(name=f'Member Count: {member.guild.member_count}')
The right event for this would be on_member_remove.
You can also get the channel in a much easier way and edit it.
See a possible new code:
#bot.event
async def on_member_remove(member: discord.Member):
channel = bot.get_channel(Channel_ID_here)
await channel.edit(name=f"Member Count: {len(member.guild.members)}")
The reason it doesn't work is because you are using on_raw_member_remove
You should be using on_member_leave as the member is not getting removed.
Related
So I was wondering if it is possible for a bot to send a message when the bot has been removed from a server, so far I have this and it hasn't worked
async def on_guild_leave(guild):
channel = client.get_channel(993919891902042197)
await channel.send(f"bot has left name: {guild.name}, owner: {guild.owner}, guild owner ID: {guild.owner.id}, guild ID:{guild.id} and we have {len(client.guilds)} servers")```
The correct event is on_guild_remove().
Please read https://discordpy.readthedocs.io/en/stable/api.html#discord.on_guild_remove for more information on the event.
The working code would be like this:
async def on_guild_remove(guild):
# do something...
Take note that Intents.guilds must be enabled in order for this event to be detected, as stated in the documentation linked above.
My bot sends an embed every time a new member joins. Then the bot adds the little 👋🏽 reaction to it. I want members to be able to welcome the new member by reacting. If they react, then they will be rewarded in some way. So onto my question, how would I make my bot watch for reactions for 60 seconds after the embed is sent, and what event would I use? I've found a few things in the documentation but none of which seem to make sense to me. A code example and explanation of how it works would be amazing. Thanks in advance!
In order to do this, you'd need to make use of the on_reaction_add event, assuming you already have all your imports:
#bot.event
async def on_member_join(member): # when a member joins
channel = discord.utils.get(member.guild.channels, name="welcome") #getting the welcome channel
embed = discord.Embed(title=f"Welcome {member.mention}!", color=member.color) #creating our embed
msgg1 = await channel.send(embed=embed) # sending our embed
await msgg1.add_reaction("👋🏽") # adding a reaction
#bot.event
async def on_reaction_add(reaction, user):
message = reaction.message # our embed
channel = discord.utils.get(message.guild.channels, name="welcome") #our channel
if message.channel.id == channel.id: # checking if it's the same channel
if message.author == bot.user: #checking if it's sent by the bot
if reaction.emoji.name == "👋🏽": #checking the emoji
# enter code here, user is person that reacted
I think this would work. I might have done the indentations wrong since I'm on another device. Let me know if there are any errors.
You can use the on_reaction_add() event for that. The 60 second feature might be complicated.
If you can get the user object from the joined user the on_reaction_add() event, you could check if the user joined less than 60 seconds ago.
You can check the time a user joined with user.joined_at, and then subtract this from the current time.
Here is how to check how many seconds a user is already on the server.
from datetime import datetime
seconds_on_server = (datetime.now() - member.joined_at).total_seconds()
A rather hacky solution is to retrieve the original user who joined through the message on which the reaction is added. Members have the joined_at attribute, which is a datetime object, with it you can just snap current datetime and subtract the former from it. The resulting is a timedelta object which you can use to calculate the time difference.
I am a beginner to this programming stuff, and I have a quick question. I am trying to make a logs channel for multiple servers, and I want to be able to look for a channel that has the word “logs” in it, so can you help me?
Code:
#client.event
async def on_command_completion(ctx):
channel = client.get_channel('829067962316750898')
embed = discord.Embed(colour=discord.Color.green(),
title="Command Executed")
embed.add_field(name="Command:", value=f"`,{ctx.command}`")
embed.add_field(name="User:", value=f"{ctx.author.mention}", inline=False)
embed.add_field(name="Channel:",
value=f"{ctx.channel} **( <#{ctx.channel.id}> )**")
await channel.send(embed=embed)
You need to get the channel. I suggest using discord.utils.get.
Also, try using {ctx.channel.mention} to mention the channel instead.
#client.event
async def on_command_completion(ctx):
channel = discord.utils.get(ctx.guild.text_channels, name='logs')
if channel is None:
return # If there is no logs channel, do nothing (or what you want to do)
embed = discord.Embed(colour=discord.Color.green(),
title="Command Executed")
embed.add_field(name="Command:", value=f"`,{ctx.command}`")
embed.add_field(name="User:", value=f"{ctx.author.mention}", inline=False)
embed.add_field(name="Channel:",
value=f"{channel.name} **( {channel.mention} )**")
await channel.send(embed=embed)
See the discord.TextChannel docs and discord.utils.get()
I don't have much experience in discord.py, but I'm making a bot. Right now I added the welcoming message:
#client.event
async def on_member_join(member):
channel=client.get_channel(channel_ID)
emb=discord.Embed(title="NEW MEMBER",description=f"Thanks {member.name} for joining!")
await channel.send(embed=emb)
but I want to mention the user who joined as this picture
May you help me doing this? Thanks!
Use member.mention
This returns a string which allows you to mention the member.
#client.event
async def on_member_join(member):
channel=client.get_channel(channel_ID)
emb=discord.Embed(title="NEW MEMBER",description=f"Thanks {member.mention} for joining!")
await channel.send(embed=emb)
You should however keep in mind that because of caching, the mention will most likely look something like this <#!123456789>.
You need the code to look like this:
#client.event
async def on_member_join(member):
if member.guild.id !=Guild.id:
return
channel = client.get_channel(channel_id) # replace id with the welcome channel's id
await channel.send(f"{member.mention} has arrived!, check out our announcments channel for server and bot announcements!")
await member.send(f"Thank you for joining {member.guild.name}!")
This line right here is you only want people that join YOUR server to send a msg, if you don't have any server with your bot in it, will say the msg still
if member.guild.id !=Guild.id:
return
{member.mention} is what mentions the user if you are using {} in the sending part you need the f function
Next code will make the code call the {}
await channel.send(f"{member.mention}")
This calls the guild name:
{member.guild.name}
I am not sure about client.get_channel, but you sure can try this:
#client.event
async def on_member_join(member):
guild = client.get_guild(serverID)
channel = guild.get_channel(channelID)
emb = discord.Embed(title="NEW MEMBER",description=f"Thanks {member.name} for joining!")
await channel.send(member.mention, embed=emb)
I have a discord bot that handles with alts, i'm looking for a way that my bot knows if he dmed the person already before (explaining why he was kicked) and it wont dm them again. My function is like this:
#client.event
async def on_member_join(member):
channel = member.guild.text_channels[0]
if something
await channel.send(f"**{member.display_name}** was kicked")
await member.send("**Hi, your account was kicked due to reason** \n"
"**please try again later!**\n"
f"**{member.guild.name}.**")
await member.kick(reason=None)
else:
pass
My problem is that every time someone is kicked my bot dms them and I want it to dm the user kicked only once in their lifetime (without saving which user was dmed before).
would like to get help :)
You could have a look at this but you should at least save their id's to a text file.