goose.mp4
goose.mp4

Reputation: 272

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)

Upvotes: 2

Views: 2343

Answers (2)

IPSDSILVA
IPSDSILVA

Reputation: 1829

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.

Upvotes: 4

Manoj A
Manoj A

Reputation: 432

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.

Upvotes: 0

Related Questions