I want to use return under a command function, what do I as the variable?

I want to prevent my bot from answering itself, so I've attempted to set up an if statement, obviously it's not the right one.. What I replace message.author with in this situation?

@client.command()
async def amy(ctx):
    if message.author == client.user:
        return
    else:
        gif = discord.Embed(color = 0x83B5E3)
        gif.set_image(url = 'https://media.giphy.com/media/2yyKE8ZEgrAXPx5gwI/giphy.gif')
        await ctx.channel.send(embed=gif)

I know how to do it with events, I'm just not sure what to use under commands...

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    else:
        gif = discord.Embed(color = 0x83B5E3)
        gif.set_image(url = 'https://media.giphy.com/media/2yyKE8ZEgrAXPx5gwI/giphy.gif')
        await ctx.channel.send(embed=gif)

4 answers

  • answered 2020-07-09 11:44 flynn1973

    @client.event
    async def on_message(message):
        if message.author == client.user:
            return
    

    you are already doing it right. the if needs to be on top of the function. seems there are are some other problems.

  • answered 2020-07-09 12:03 SkyRacerQ_

    If I understand correctly, You don't want your program to respond to commands from itself. If this is the case, use ctx.author instead of message.author

    Try using this code:

    @client.command()
    async def amy(ctx):
        if ctx.author == client.user:
            return
        else:
            gif = discord.Embed(color = 0x83B5E3)
            gif.set_image(url = 'https://media.giphy.com/media/2yyKE8ZEgrAXPx5gwI/giphy.gif')
            await ctx.channel.send(embed=gif)
    

  • answered 2020-07-09 12:08 PEACH

    Yo! I found that you've misspelled the ctx and message! A command does'nt have a message variable!

    @client.command()
    async def amy(ctx):
        if message.author == client.user: # <-- RIGHT HERE
            return
        else:
            gif = discord.Embed(color = 0x83B5E3)
            gif.set_image(url = 'https://media.giphy.com/media/2yyKE8ZEgrAXPx5gwI/giphy.gif')
            await ctx.channel.send(embed=gif) 
    

    so basically you just need to change the code to:

    @client.command()
    async def amy(ctx):
        if ctx.author == client.user:
            return
        else:
            gif = discord.Embed(color = 0x83B5E3)
            gif.set_image(url = 'https://media.giphy.com/media/2yyKE8ZEgrAXPx5gwI/giphy.gif')
            await ctx.channel.send(embed=gif)
    

    hope it helps!

  • answered 2020-07-09 14:34 Deru

    So far I have only seen answers that told what you needed to do. But not why you need to do what they told you. This answer will give you a strategy and solution so you wont need to ask these questions again. (Not that it is wrong, but you can easily find out it yourself, thus saving a lot of time).

    Step 1: Search the event (or in this case how to create a command) in the documentation

    As we want to understand how we can make commands. We first search how we can make commands. For example this link helps you make commands. Getting to this link can be done by typing in "How to make commands in discord py", and taking the first link that is from the official documentation: https://discordpy.readthedocs.io/.

    Step 2: Look what parameters (objects) you get from the event

    When we read the documentation you will see in the first part that it uses a context object. And that you can use it through the input from the function (The variable ctx). We now want to investigate what we can do with the ctx/context object.

    Step 3: Understand what the objects can be used for

    As stated before we want to understand how we can use the objects. In this case we are focussing on the "ctx" object. The page describing the context object tells everything the ctx object contains. As you can see the first parameter that appears is the message object. Which was part of what you wanted. Thus accessing it would simply have been: ctx.message.author. But how do other people say use: ctx.author? Well if you read the context page further we also see an Author member. It also states that it is a shorthand for Message.Author. Meaning that we can also use: ctx.author, to get exactly the same result.

    Step 4: Integrate the objects in your code

    Now we know how what the context object is, and what we can do with it. But how do we integrate it?

    Well first it is usefull to remind us of what we wanted to achieve. We want to block the bot from responding to itself. Thus simply ignoring messages that may come from your bot. We need to compare wether or not the author of the incoming message is the bot, if it comes from the bot we need to ignore it.

    As stated before in step 3. We can either use ctx.message.author or ctx.author to get the author. If this is equal to the bot (client.user) then we should return early.

    This results in an if statement that checks both values:

    @client.command()
    async def amy(ctx):
        if ctx.author == client.user:
            return
        else:
            # the rest of your code.
    

    or

    @client.command()
    async def amy(ctx):
        if ctx.message.author == client.user:
            return
        else:
            # the rest of your code.
    

    _________________________________________________________________

    I recommend reading the documentation. As it helps a lot. Often the input you get from events/commands are all you need to get the job done. Also it is handy if you know exactly what you need. For example you could have searched all objects you get from the function, and searched it for a specific variable and description, in this case you needed "Author". This can make the search faster.

    Anyway it is usefull to know what common objects can be used for. As this helps a lot in writing code for your bot.