Alex Taylor
Alex Taylor

Reputation: 11

Discord.py Bot Executing commands Twice

My bot executes these commands that include await bot.process_commands(ctx) & await bot.process_commands(message). How may I fix?

#playing a game status

@bot.command()
@commands.has_permissions(manage_guild=True)
async def g_status(ctx, *, status):
    embed = discord.Embed(description =f"✅ Status changed to Playing a game `{status}`", color= 0x303136)
    await bot.change_presence(activity=discord.Game(name=f"{status}"))
    await ctx.send(embed=embed)
    await bot.process_commands(ctx)


#watching status

@bot.command()
@commands.has_permissions(manage_guild=True)
async def w_status(ctx, *, status):
    embed = discord.Embed(description =f"✅ Status changed to Watching `{status}`", color= 0x303136)
    await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=f"{status}"))
    await ctx.send(embed=embed)
    await bot.process_commands(ctx)

#listen status

@bot.command()
@commands.has_permissions(manage_guild=True)
async def l_status(ctx, *, status):
    embed = discord.Embed(description =f"✅ Status changed to Listening to `{status}`", color= 0x303136)
    await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name=f"{status}"))
    await ctx.send(embed=embed)
    await bot.process_commands(ctx)

#clear status
@bot.command()
@commands.has_permissions(manage_guild=True)
async def clear_status(c):
    await bot.change_presence(status=None)
    embed = discord.Embed(description =f"✅ Status cleared", color= 0x303136)
    await c.send(embed=embed) 
    await bot.process_commands(c)
#boost
@bot.event
async def on_message(message):
    if "MessageType.premium_guild" in str(message.type):
        if message.guild.id == 928443083660607549:
                if "|" in message.author.display_name:
                    name0 = message.author.display.split("|")
                    display = name0[0]
                else:
                    display = message.author.display_name
                embed = discord.Embed(title=f"thanks for boosting, {display}!", description="thanks for boosting ikari <3", color=0x303136)
                await message.channel.send(f"{message.author.mention}")
                await message.channel.send(embed=embed)
        else:
            return
    await bot.process_commands(message)

#send msgs via dms
@bot.command()
@commands.has_permissions(manage_guild = True)
async def dm(ctx, member: discord.User):
    await ctx.send('Message?')
    def check(m):
        return m.author.id == ctx.author.id

    message = await bot.wait_for('message', check = check)
    embed = discord.Embed(title="**Message sent!**", color=0x303136)
    embed.set_author(name=f"sent by: {message.author}", icon_url= message.author.avatar_url)
    embed.add_field(name="**Message:**", value=f"{message.content}", inline = False)
    msg = discord.Embed(description=f'**{message.content}**', color=0x303136)
    await ctx.send(embed=embed)
    await member.send(embed=msg)
    await bot.process_commands(message)


#typing 

@bot.command()
async def helloworld(ctx):
    async with ctx.typing():
        await asyncio.sleep(0.5)
    
    await ctx.send('Hello World!')

#recieve dm test

@bot.event
async def on_message(message: discord.Message):
    if message.guild is None and not message.author.bot:
        print(message.content)
    await bot.process_commands(message)

msg_dump_channel = 932252699657908225
@bot.event
async def on_message(message: discord.Message):
    channel = bot.get_channel(msg_dump_channel)
    if message.guild is None and not message.author.bot:
        embed = discord.Embed(description=f"**{message.content}**\n sent by{message.author.mention}", color=0x303136)
        embed.set_author(name=f"{message.author}", icon_url= message.author.avatar_url)
        await channel.send(embed=embed)
    await bot.process_commands(message)

#send text

@bot.command()
@commands.has_permissions(manage_guild = True)
async def send(ctx, *, channel: discord.TextChannel):
    msg_chan = channel
    await ctx.send('?')
    def check(m):
        return m.author.id == ctx.author.id

    message = await bot.wait_for('message', check = check)
    embed = discord.Embed(title="✅ **Message sent!**", color=0x303136)
    embed.set_author(name=f"sent by: {message.author}", icon_url= message.author.avatar_url)
    embed.add_field(name="**Message:**", value=f"{message.content}", inline = False)
    await ctx.send(embed=embed)
    async with msg_chan.typing():
        await asyncio.sleep(0.5)
    await msg_chan.send(f'{message.content}')
    await bot.process_commands(message)

#rep give role

@bot.listen('on_message')
async def on_message(message):
    if message.channel.id == 928443084205867038:
        role_name = "rep 1"
        lvl5 = "lvl 5"
        if "rep 1" in message.content:
            role =  discord.utils.get(message.guild.roles, name=role_name)
            await message.author.add_roles(role)
            await message.add_reaction("<:true:928510447169196082>")
            
        if "rep 2" in message.content:
            rep1 = discord.utils.get(message.guild.roles, name=role_name)
            if rep1 in message.author.roles:
                rep1 = discord.utils.get(message.guild.roles, name=role_name)
                rep = discord.utils.get(message.guild.roles, name=lvl5)
                await message.author.add_roles(rep)
                await message.author.remove_roles(rep1)
                await message.add_reaction("<:true:928510447169196082>")
                
            else:
                await message.channel.send('You must rep once')

    await bot.process_commands(message)

Upvotes: 0

Views: 404

Answers (2)

Nyghl
Nyghl

Reputation: 166

The problem you are having is bot.process_commands. What that does is, well, it processes commands from a message. And internally it is used in the default on_message to make commands going. When you think about it, it makes sense. But, when you override the default on_message it overrides that bot.process_commands so your commands stops working.

(Actually, Discord.py's official F.A.Q covered this: Why does on_message make my commands stop working?)

To make our commands work again, we are adding only one bot.process_commands to end of our on_message. But in your code, you have two on_message events running and two bot.process_commands so we are processing our commands twice. This is why your commands works twice. Just delete one from your custom on_message event or not use it at all.

Upvotes: 0

Nhat Micls
Nhat Micls

Reputation: 80

on_message will be execute when you have a message in discord channel or DM chat(link). So you need just one on_message function

I don't know why your code have multi on_message so there are some way to fix it.

  • Combine everything related in one on_message
@bot.event
async def on_message(message: discord.Message):
    #Do everything related in here
  • Use dedicated API for dedicated case, you can read for more information Discord API

If you need to make another function that want to execute with on_message so you need to do this.

@bot.listen('on_message')
async def my_message(message):
    print('two')

Upvotes: 1

Related Questions