I want to to make some code that will mention the name of the user that sends the message, this is what I have tried:
Im new to making discord bots, and python so any help would be perfect
#client.command()
async def hello(ctx, member):
await ctx.send(f"hello, {member}")
You can do this with user.name
#client.command()
async def hello(ctx):
await ctx.send(f"hello, {ctx.author.name}")
The above can be called by {prefix}hello, if you want to say "hello, {name}" even when user just send "hello" (without prefix), then use on_message event.
#client.event
async def on_message(message):
if message.author.bot: return
if message.content.lower() == "hello":
await message.channel.send(f"Hello, {message.author.name}")
await client.process_commands(message)
Docs: on_message, user.name
Assuming you want to mention the user who used the command. the ctx argument has a lot of attributes one of them is the author
#client.command()
async def hello(ctx, member):
await ctx.send(f"hello, {ctx.author.mention}")
If you want the user to say hello to another user, he can mention or just type the name.
#client.command()
async def hello(ctx, *, user: discord.Member = None):
if user:
await ctx.send(f"hello, {user.mention}")
else:
await ctx.send('You have to say who do you want to say hello to')
All of the above providing you are using discord.ext.commands
This should work:
str(ctx.message.author)
Or:
str(ctx.author)
Related
I'm making a discord bot in python using the discord.py library. I need to know, how I can implement a yes/no confirmation after a command. For example, consider a 'ban' command that bans a user from the server, and can only be used by a moderator. After writing "!ban #user", I want the bot to reply "Ban #user?(y/n)" and if that very moderator replies with anything but a "y", the ban gets cancelled. How can I implement this? Something like this:
#client.command()
#commands.is_owner()
async def ban(ctx, member : discord.Member, *, reason = None):
await ctx.send("Ban #member?(y/n)")
if get_confirmation():
await member.ban(reason = reason)
else:
await ctx.send("Ban Cancelled")
#client.command()
#commands.is_owner()
async def ban(ctx, member : discord.Member, reason = None):
await ctx.send(f"Ban {member.mention}?(yes/no)")
#client.command()
#commands.is_owner()
async def yes(ctx, member : discord.member, reson=None):
await member.ban(reason = reason)
#client.command()
#commands.is_owner()
async def no(ctx):
await ctx.send("Ban Cancelled")
You need to use wait_for.
Below is the revised code:
#client.command()
#commands.is_owner()
async def ban(ctx, member : discord.Member, *, reason = None):
await ctx.send(f"Ban {member.mention}?(y/n)")
msg = await bot.wait_for("message", check=lambda m:m.author==ctx.author and m.channel.id==ctx.channel.id)
if msg.content.lower in ("y", "yes"):
await member.ban(reason = reason)
else:
await ctx.send("Ban Cancelled")
Okay after some digging i found this post. I hope it helps anyone with the same issue
How do I make it so that, in this case, the bot responds with "Hello," and then the name of the person who used the .hello command?
This is what I have so far.
#client.command(pass_context = True)
async def hello(ctx):
author = ctx.message.author.name
await ctx.send("Hello", author, "!")
You should use f strings. What you can do is
#client.command()
async def hello(ctx):
await ctx.send(f'hello {ctx.author.name} !')
also if you want the user who did the command you can just do ctx.author. No need for ctx.message.author.
so, Ive been doing this:
from discord.ext import commands
from discord.utils import get
client = commands.Bot(command_prefix='><')
#client.event
async def on_ready():
print("I am ready Winson or not Winson :D")
#client.event
async def on_member_join(member):
channel = client.get_channel(744440768667844698)
message = await channel.send(f"Welcome to HaveNoFaith {member}, happy to be friends with you")
#client.command()
async def ping(ctx):
await ctx.send(f"Your Ping is {round(client.latency *1000)}ms")
#client.command()
async def Help(ctx2):
await ctx2.send("Hi, Im WelcomeBot v1.0...\n\nPrefix: ><\n\nCommands: ping\n help")
#and then Im trying to do like at the message "Welcome etc" if they react with the "check reaction" in that message, they will get a role in the discord server...
you can make a command named addrr(add reaction role), which will look like this -
#client.command()
#commands.guild_only()
#commands.has_permissions(administrator=True)
async def addrr(self, ctx, channel: discord.TextChannel, message: discord.Message, emoji: discord.Emoji,
role: discord.Role):
await ctx.send(f"Setting up the reaction roles in {channel.mention}.")
await message.add_reaction(emoji)
def check1(reaction, user):
return user.id is not self.client.user.id and str(reaction.emoji) in [f"{emoji}"]
while True:
try:
reaction, user = await self.client.wait_for("reaction_add", check=check1)
if str(reaction.emoji) == f"{emoji}":
await user.add_roles(role)
await message.remove_reaction(reaction, user)
else:
await message.remove_reaction(reaction, user)
except:
await message.delete()
break
It will work like this -
><addrr <#channel mention> <message ID> <Emoji> <#Role Mention>
So you can add a reaction to the messages that was sended and use wait_for to wait for a reaction on that message. I recommend you to add the timeout. If you dont want to have this timeout, simply just send these message, save it into a list and in the raw_reaction_add event check if the emoji is the one and the message is one of the messages in your list
Here is the sendMessage function:
async def sendMessage(color, title, value, should_delete=True, channel=""):
embed = discord.Embed(color=color)
embed.add_field(name=title, value=value, inline=False)
if channel == "":
msg = await client.send_message(message_obj.channel, embed=embed)
else:
msg = await client.send_message(client.get_channel(channel), embed=embed)
if should_delete:
await delete_msg(msg)
The bot can mention anyone but everyone and here. Despite having mention everyone permission.
sendMessage(OK_COLOR_HASH, "title", "Hello #everyone")
Edit: When I converted the message type to the normal one instead of embed one, it worked.
You can try to sending a mention to everyone through the default_role attribute
#bot.command(pass_context=True)
async def evy(msg):
await bot.say(msg.message.server.default_role)
You can try this block code. roles return a list all guild's roles, but first role always a default guild role (default_role) and that's why you must use slicing function.
#bot.command()
async def test(ctx):
await ctx.send(ctx.message.guild.roles[0])
Or you can do something like this.
#bot.command()
async def test(ctx):
await ctx.send(ctx.message.guild.default_role)
I have a discord bot, and I want it to do something like
once a member joins
DM member (message)
if member replies with key
give them this role
Thanks
You need to use the function on_member_join().
#client.event
async def on_member_join(member):
pass
Then put the code message sending/receiving code in there. With your example you would do:
#client.event
async def on_member_join(member):
await client.send_message(member, 'Prompt.')
m = await client.wait_for_message(author=member, channel=member)
if m.content == 'key':
# give the user the role
await client.send_message(member, 'Role added')
else:
await client.send_message(member, 'Incorrect key')
To find out how to give a user a role from a dm to a server, read this question: How To Assign A User A Role In A Server From A Direct Message - Discord.py
#client.event
async def on_member_join(member):
await member.send("hello")
This code sends hello to the user when he/she joins the server.
Hope this is helpful for everybody