Search code examples
pythondiscord.pybots

on_message returns to if message while inside on bot.command


First post and I'm trying to upgrade my Discord Bot!

At the moment the Bot is able to execute the !decklist fine, but when I occur in the if not message.attachments and I try to execute the !boh command, the bot responds with the if clause and also execute the !boh command.

Posting the code down below, many thanks for the help!

import discord
from discord.ext import commands
from io import BytesIO

TOKEN = 'disc_token'

def run():
    intents = discord.Intents.default()
    intents.message_content = True
    intents.members = True
    bot = commands.Bot(command_prefix="!", intents = intents)
    
    #ON_READY
    @bot.event
    async def on_ready():
        #SERVER = bot.get_guild(int(1156534000890953748))
        bot_name = await bot.fetch_user(int(1155959555876991098))
        await bot.tree.sync()
        print(f'{bot_name}si è connesso a Discord')

    #SENDS USER EMBED TO RESPOND WITH IMAGE.
    @bot.command()
    async def decklist (ctx):
        """Riceve l'immagine della Decklist e la invia per la conferma"""
        embed = discord.Embed(
        colour = discord.Colour.dark_magenta(),
        title = ("Dreamborn"),
        url = ("https://dreamborn.ink/")
        )
        embed.set_image(url="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQKCp5PneGn0VgoP-Hncj4AxiN-Ql2tqaKd6w&usqp=CAU")
        await ctx.send(f"Mandami l'immagine della Decklist esportata da **Dreamborn!**", ephemeral=True)
        await ctx.send(embed=embed)
        @bot.event
        async def on_message(message):
            await bot.process_commands(message)
            if message.author == bot.user:
                return
            if message.author.bot:
                return
            if not message.attachments:
                #ignore (message=bot.command())
                await message.author.send('Ciao! La tua lista non è **idonea**.\nMandami una **immagine!**')
                pass
            image_file = message.attachments[0]
            image_bytes = await image_file.read()
            liste = bot.get_channel(int(1156534533907288064))
            istruzioni = bot.get_channel(int(1156626680794845234))
            await liste.send('**' + message.author.display_name + '**')
            await liste.send(file=discord.File(fp=BytesIO(image_bytes), filename=image_file.filename))
            await message.author.send('Oki doki, Decklist inviata! \nMetti la reazione al messaggio su' + istruzioni.mention + 'per essere Confermato! \nSe invece avevi già messo la reazione a Confermati, ignora!')
            await bot.process_commands(message)

    #REPLY WITH "HELLO"
    @bot.command()
    async def boh(ctx):
        await ctx.send(f"ciao!")

   bot.run(TOKEN)
run()

problem view from discord

I'm just wondering how can I "exit" properly from the !decklist command in order to not fall again in the if clause in case I'm using other commands.


Solution

  • I've solved it, just replacing the on_message with message = await bot.wait_for('message')!

    Full code in case you need! You can close this topic!

    `import discord
    from discord.ext import commands
    from io import BytesIO
    
    TOKEN = 'disc.token'
    
    def run():
        intents = discord.Intents.default()
        intents.message_content = True
        intents.members = True
        bot = commands.Bot(command_prefix="!", intents = intents)
        
        #ON_READY
        @bot.event
        async def on_ready():
            bot_name = await bot.fetch_user(int(bot_id))
            await bot.tree.sync()
            print(f'{bot_name}si è connesso a Discord')
    
        #SENDS USER EMBED TO RESPOND WITH IMAGE.
        @bot.command()
        async def decklist (ctx):
            """Riceve l'immagine della Decklist e la invia per la conferma"""
            embed = discord.Embed(
            colour = discord.Colour.dark_magenta(),
            title = ("Dreamborn"),
            url = ("https://dreamborn.ink/")
            )
            embed.set_image(url="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQKCp5PneGn0VgoP-Hncj4AxiN-Ql2tqaKd6w&usqp=CAU")
            await ctx.send(f"Mandami l'immagine della Decklist esportata da **Dreamborn!**", ephemeral=True)
            await ctx.send(embed=embed)
            #WAITS FOR USER INTERACTIONS, IF ATTACHMENTS IS TRUE, POSTS THE ATTACHMENT INTO A DEDICATED CHANNEL
            message = await bot.wait_for('message')
            if message.author == bot.user:
                    return
            if message.author.bot:
                    return
            if not message.attachments:
                    #ignore (message=bot.command())
                await message.author.send('Ciao! La tua lista non è **idonea**.\nMandami una **immagine!**')
                message = await bot.wait_for('message')
                pass
            image_file = message.attachments[0]
            image_bytes = await image_file.read()
            liste = bot.get_channel(int(1156534533907288064))
            istruzioni = bot.get_channel(int(1156626680794845234))
            await liste.send('**' + message.author.display_name + '**')
            await liste.send(file=discord.File(fp=BytesIO(image_bytes), filename=image_file.filename))
            await message.author.send('Oki doki, Decklist inviata! \nMetti la reazione al messaggio su' + istruzioni.mention + 'per essere Confermato! \nSe invece avevi già messo la reazione a Confermati, ignora!')
    
        bot.run(TOKEN)
    run()`