How do I get a bot to mention someone? - python

I've been trying to make my bot mention someone for a command for a while, I was looking around Google and other websites and most of them where specific people like using <#(User ID)> but I want to make it mention whoever, like, when I use a command like "?hit" I would like it to say "(bot pings who used the command) punches (person pinged in command)
(Random message idk)"
I can't find a website or video of how to do it I need a little help.

Every Member object is mentionable (using the mention function), read up on the docs here. Using that, we can create a command which takes in a member to hit. Then, we can ping the ctx.author and member using the function mention. It would look something like:
#client.command()
async def hit(ctx, member: discord.Member):
await ctx.send(f"{ctx.author.mention} punches {member.mention}")
Output:

I don't mean to be rude but this question was easily answered by looking at the discord API docs, as it explains everything in the API and what it does. My solution is below. You didn't provide any code or problem you've had with code so this is all I can do.
#client.command() # here, we make the client command.
async def hit(ctx, member : discord.Member): # we are making the function that actually hits the person here.
await ctx.send(f'{ctx.message.author.mention} hits {member.mention}.') # here, we send the message that pings both the user who triggered the command, and the person who the you want to ping.
If you wanted to have it select from a list, for example, here's how that'd work.
#client.command()
async def hit(ctx, member : discord.Member):
# this is a list of random responses that could happen.
hit_responses = [f'{ctx.message.author.mention} hits {member.mention}.',
f'{ctx.message.author.mention} swings and hits {member.mention}.',
f'{ctx.message.author.mention} slaps {member.mention} across the face.']
hit_message = random.choice(hit_respones) # this chooses a random response.
await ctx.send(hit_message) # this sends the random response
An example output would be as follows:
#goose.mp4 slaps #user across the face.

Related

Discord.py bot command to search channel for keywords and copy message to another channel

Edit for follow up Question -
Using the previous suggestion I got it to work. Then I decided I needed variables with all of the channel ids rather than everyone having to know the IDs. Is it possible to have the user input be a variable, to where the bot will then use the ID stored in that variable? I believe it'll work by using a converter.
I've got some code I've pieced together from various sources on here. My goal is to have a bot command that will search the given channel for a keyword. Upon finding a message with the keyword in the channel, it will copy that message into another channel then delete the original message.
I'm also trying to do this with user given arguments, so I don't have to change the code for every keyword.
This is what I have so far.
#bot.command()
async def move_information(ctx,Keyword: discord.Message,ChannelFrom: int,ChannelTo: int):
for message in ChannelFrom:
msg = await ctx.fetch_mesage(Keyword)
await ChannelTo.send(msg)
discord.message is an object, if you are searching for messages in a specific textchannel then it would be something like this
#bot.command()
async def move_information(ctx,Keyword: str, ChannelFrom: discord.TextChannel, ChannelTo: discord.TextChannel):
async for message in ChannelFrom.history:
if Keyword in message.content:
await ChannelTo.send(Keyword)

Programming a Discord bot in Python- Why can't the bot send custom emojis?

Here's what I have:
#client.command()
async def say(ctx, *, text):
catjam = client.get_emoji(799738295520591873)
await ctx.send(f"{catjam}{text}{catjam}")
The result I want is to send the user's message, with the emoji on either side. Instead it does this:
And yes, there are questions like this that have been asked, but I looked at those and none of them helped with my problem. Any tips?
When you want to make a bot sending a certain emote you need to do like this <:emote_name:emote_ID> ( if it's animated add "a" after < ) And, in order to work, you must have that emote in your server or to be in a server that contains that emote, otherwise it will not work.
To do a custom emoji, you would do <:name_of_emoji:id_of_emoji>. e.g <:pepe:1262321934>
In Code:
await ctx.send("<:pepe:1262321934>")

Discord.py: Is there a way to get arguments for a command using the on_message() function?

I'm just programming my own discord bot with python, and I have a kill command. All it does is that when you type *kill and mention someone (*kill #goose.mp4), it will give you a random scenario on how you kill them similar to the Dank Memer bot. I'm trying to get that ID of the user that was mentioned, which will be the second argument to the function. But I'm stuck. After reading through the API and searching this up multiple times, I've only been given how to get the ID of the author and ping them with the bot, not the person the author mentioned.
This is the code that I am currently using. One of the variables is given a value only for testing purposes.
if message.content.startswith('*kill'):
print("kill command recieved")
kill_mention_killer = message.author.mention
kill_mention_victm = 'some guy'
print(kill_mention_killer)
kill_responses = [kill_mention_killer + ' kills ' + kill_mention_victim]
kill_message = kill_responses[random.randint(-1, len(kill_responses) -1)]
await message.channel.send(kill_message)
The way you are currently making this command will not allow you to get the arguments. If you're trying to make a command like this: *kill #user, then you will need to be able to get the user that was mentioned (which is your question). Here's how you do it:
First Step
import discord, random
from discord.ext import commands
These imports are very important. They will be needed.
Second Step
client = commands.Bot(command_prefix='*')
This will initialize the client, which is used throughout the code. Now onto the part where you will actually make the command.
#client.command()
async def kill(ctx, member: discord.Member): # This command will be named kill and will take two arguments: ctx (which is always needed) and the user that was mentioned
kill_messages = [
f'{ctx.message.author.mention} killed {member.mention} with a baseball bat',
f'{ctx.message.author.mention} killed {member.mention} with a frying pan'
] # This is where you will have your kill messages. Make sure to add the mentioning of the author (ctx.message.author.mention) and the member mentioning (member.mention) to it
await ctx.send(random.choice(kill_messages))
That's it! That's how you make a standard kill command. Just make sure to change the kill_messages array to whatever messages you would like.
if message.content.startswith('*kill'):
#Mentioned or not
if len(message.mentions) == 0:
#no one is mentioned
return
pinged_user = message.mentions[0]
killer_user = message.author
kill_messages = [
...
]
await ctx.send(random.choice(kill_messages))
here I just used message.mentions to find if any valid user is mentioned in the message or not and if mentioned! all the mentions will be in message.mentions list so i took the first mention of the message by message.mentions[0]. then you can do anything with the mentioned user object.

how can I make my python discord bot detect a certain user being mentionned in a command?

I'm trying to get a command to have an exception where, when the bot itself is pinged, a different message is sent, instead of the generic normal one. And I cant seem to find a certain command that works to my favor in discord.py, any suggestions?
#bot.command()
async def fight(ctx):
if (insert the scanned mention == 'insert discord bot ping'):
await ctx.send('message')
await ctx.send('another message')```
You can get a list of all the mentioned users by using ctx.message.mentions. In case you have the Member/User instance of that person, you can just do member.mentioned_in(message), or in your case:
if client.user.mentioned_in(message):
# do something here
In case you don't have the Member instance, you can iterate over ctx.message.mentions, and check each one to see if they're the one you needed to check for.
id = 12345678910 # The Discord id of the member you want to check
for user in ctx.message.mentions:
if user.id == id:
# do something here

Discord Bot Python Deleting Author's Message

I made a small discord bot for some fun:
I want the author's messages to be deleted when Author run this command. But this does not work.
if message.content.startswith('Delete me.'):
async for msg in client.logs_from(message.channel, message.author):
await client.delete_message(msg)
Looking at their docs, logs_from takes a channel, along with additional arguments allowing you to
filter which messages to retrieve.
So if you just wanted to delete all messages, you can do something like
async def on_message(message):
NONSENSE_LIMIT = 999999999999
if message.content.startswith('!delete'):
async for log in client.logs_from(message.channel, limit=NONSENSE_LIMIT):
if log.author == message.author:
await client.delete_message(log)
Which will delete all messages for a user that types in that command. Your bot must have the manage messages permission for that channel.
Note that the number of messages returned is limited, and here I just write down some arbitrary nonsense limit. If you want to be accurate, you would have to figure out how many messages there are in the channel somehow, and then use that as the limit.
I just looked up discord API in the last 5 minutes so I don't know if there's a more efficient way to do it.
Your problem is in the client.logs_from(message.channel, message.author)statement. logs_from accepts the following arguments: logs_from(channel, limit=100, *, before=None, after=None, around=None, reverse=False). See the API reference. So you have two channel arguments where logs_from only accepts 1. Perhaps you intended client.logs_from(message.author) instead?

Categories

Resources