I'm trying to make a "!welcome (mention member)" command, which will then send a predefined message, tagging the member I mentioned. I want it to look like this, if for example, I typed "!welcome #Elastic_Disaster" --> https://gyazo.com/8e055cd380f0dd1e608c77f590ce631c
This is the code I've tried, but it does nothing.
#bot.command()
#commands.has_permissions(administrator=True)
async def welcome (ctx, user = discord.Member):
member = str(ctx.member)
await ctx.send(f'{member} welcome to the server (and more welcome stuff here).')
If you want to specify a argument's class, you have to use : instead of =. Then, you can use discord.Member.mention to mention the member.
#bot.command()
#commands.has_permissions(administrator=True)
async def welcome (ctx, user: discord.Member):
await ctx.send(f'{user.mention} welcome to the server (and more welcome stuff here).')
Related
I'm writing a discord bot for my discord-server and the people on the server change their nicknames like their underwear. Can i get the user id from their changing nicknames?
I want to code a command like this:
!command #mention
I need the user id of the mention. Thanks in advance!
Hope this helps :)
#client.command()
async def id(ctx, member: discord.Member = None):
if not member:
await ctx.send('Please mention a user')
else:
await ctx.send(member.id)
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)
#f spam
async def on_message(message):
if message.author.bot:
return
elif message.content.lower() == 'f':
await message.channel.send('f')
await bot.process_commands(message)
this is my current code for an f spam or sends an f each time a user does.
I was wanting to make this command toggleable and also server/guild limited if possible.
for example someone says !fspam and it gets toggled and switched off and when done the same again it gets turned on. OR it could be !fspam on/ !fspam off
You can use Command.enable, where you can use command.update to. This will raise the DisabledCommand error.
Also please dont just copy and paste the code, try understanding what I did.This will disable it for all the guilds to. If you want it per server then you will need to use a database.
For example:
#client.command()
async def enable(ctx,*,command):
command = client.get_command(command)
command.update(enabled=True)
await ctx.send(f"{command} enabled!")
#client.command()
async def disable(ctx,*,command):
command = client.get_command(command)
command.update(enabled=False)
await ctx.send(f"{command} disabled!")
Alright, what I need is to put autorole code to my bot but only for specific server.
So please, help me!
Here is the code so far but I made it only beacuse stack overflow can't accept my text body:
#client.event
async def on_member_join(member : discord.Member):
server = ctx.message.server
rolelol = client.get_role('770262439937048577')
if server == server.id:('753667215710224574'):
await client.add_roles(member, rolelol)
else:
await client.say('')
All I need is code for autorole this doesn't code i made doesn't really matter...
You have a few things wrong :
add_roles is a Member object method, so you have to use member.add_roles()
ctx isn't defined since you're not in a command, to get the discord guild object, you have to write member.guild
get_role() is a Guild object method so you need to write member.guild.get_role(id)
Your IDs need to be passed as int, not str.
Client objects don't have any say() method
With all thoses changes, your code would look like this :
#client.event
async def on_member_join(member : discord.Member):
role = member.guild.get_role(770262439937048577)
if member.guild.id == 753667215710224574:
await member.add_roles(role)
Reference : https://discordpy.readthedocs.io/en/latest/
I want to use regex to look at and accept literally ANY incoming message from a user and assign them a role. But ONLY if that user does not have a role already assigned to them. I don't get any errors when running the code, but it does not work.
Here is what I have:
#client.event
async def on_message(message):
match = re.search(r'(.*?)', message.content)
member = message.author
role = discord.utils.get(member.guild.roles, name="Creators")
if message.author == client.user:
return
if role not in member.roles:
if match and message.channel.id == target_channel:
# add member to role
# send message to to users
await message.channel.send(
f'Hi {message.author}, welcome to the server! Don\'t forget to choose your #roles'
)
await discord.Member.add_roles(member, role)
You wrote:
await discord.Member.add_roles(member, role)
https://discordpy.readthedocs.io/en/latest/api.html?highlight=add_role#discord.Member.add_roles
As you can see from the documentation the passed arguments is *roles, reason=None, atomic=True. roles is something you have to specify, reason and atomic are optional.
You tried passing member which is not a valid argument.
discord.Member is a class. You need to get an instance of that class. ctx.author is an instance of discord.Member.
So the final call should be:
await ctx.author.add_roles(role)
To get the Member object from on_message, you can use member = message.author.
Then simply do member.add_roles(role).
Thanks for the help guys, it turns out it was something very minor (and dumb on my part) that prevented it from working.
when comparing message.channel.id.id to target_channel, I forgot that the channel id object does not come in as a string, but target_channel was set up as a string, so it failed to see them as equal. Simply converting message.channel.id to string did the trick.