Skip to content
Advertisement

discord.py some commands simply dont run

after adding these commands my code has stopped working removing some help however adding them back adds makes it not process

@client.event
async def on_message_delete(message):
    if message.author == client.user:
        return
    else:
        t = time.localtime()
        current_time = time.strftime("%c", t)
        embed = discord.Embed(title="{} deleted a message".format(message.author.name),description="", color=0xFF0000)
        embed.add_field(name=message.content, value="This is the message that has been has deleted",inline=True)
        
        channel = client.get_channel(channelid)
        if len(deleted_messages) == 6:
            del deleted_messages[0]

        final_deleted =(message.author.name+": |" + message.content + "| Sent at |" + current_time+"|")
        deleted_messages.append(final_deleted)
        last_deleted = message.content
        
        await message.channel.send(channel, embed=embed)
        await channel.message.send("test")

@client.event
async def on_member_unban(self, guild:discord.Guild, user:discord.User):
    
    response = ("The Following User Has Been Unbanned" + user)
    await client.process_commands(response)

@client.event
async def on_message(message):
    await client.process_commands(message)
    if message.author == client.user:
        return
    else:
        if message.content.endswith("🤓"):
            await message.channel.send("Please shut the fuck up " + str(message.author.name) +".")

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    else:
        if message.content == "last!":
            await client.process_commands(deleted_messages[0])
        else:
            return
@client.event
async def on_message(message):
    if message.author == client.user:
        return
    else:
        if message.content == "random_meme!":
            post = redditeasy.Subreddit(client_id=",            

                            client_secret=  "",        # Your client secret

                            user_agent=""            # Your user agent (ex: ClientName/0.1 by YourUsername")

                            )
            postoutput = post.get_post(subreddit="dankmemes")
            f = open('meme.jpg','wb')
            response = requests.get(postoutput.content)
            f.write(response.content)
            f.close()
            channel = client.get_channel(channelid)
            await channel.message.send(file=discord.File('meme.jpg'))
            os.remove("meme.jpg") 
            

any commands i try don’t run besides the deleted message one i have tried using the await client.commands.process and it also dosent work. it dosent give an error it just sits there and does nothing

Advertisement

Answer

Are you sure any “command” you try don’t work?

You are overriding the same event 3 times (on_message()), this means that the code you wrote in the first two overrides is discarded so it won’t execute.

Look at this example (the code is below). As you can see in this screenshot, the bot isn’t answering me when there is an “a” or a “b” in my messages (first two on_message() overrides). Only the last override (the one that checks if there is a “c” in the messages) is applied.

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if "a" in message.content:
        await message.channel.send("a")

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if "b" in message.content:
        await message.channel.send("b")

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if "c" in message.content:
        await message.channel.send("c")

In your case, your bot should answer to a message which content is equal to “random_meme!” (also to both on_message_delete() and on_member_unban()).

You should not override the same event (or function, or whatever) more than once. Join the content of the three overrides in one and delete the others:

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if "a" in message.content:
        await message.channel.send("a")
    if "b" in message.content:
        await message.channel.send("b")
    if "c" in message.content:
        await message.channel.send("c")

So now the bot will answer to “a”, “b” and “c”: screenshot

Note: on_member_unban() may look like it isn’t working as you didn’t tell the bot to send the message to any channel, but it is working (try adding a print(), for instance).

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement