Discord.py: await bot.process_commands(message) makes commands run 3 times.

Created on 20 Feb 2019  路  1Comment  路  Source: Rapptz/discord.py

I had a problem with bot.event(on_message), and had added the line await bot.process_commands(message) to fix it - the commands now work. But, instead of not working, they run 3 times.

Code causing trouble:

@bot.event
async def on_message(message): # This detects DDoSer copypasta
    global alertstrings
    for i in alertstrings:
        if i in message.content:
            print(message.author.display_name+" sent fake copypasta.")
            print("triggered at: "+i)
            await bot.send_message(message.channel, message.author.display_name+", this 'warning' is actually fake. Discord is not P2P, nobody can get your IP address through it.")
            await bot.process_commands(message)
        else:
            await bot.process_commands(message)

In case it's actually other parts of my code doing this, here it is:

print("Basic Greg loading...")
import discord
import asyncio
import random
import discord
from discord.ext import commands
from grabj import getjson

###############VARIABLES##############
bot = commands.Bot(command_prefix='!!')
bot.remove_command('help')
code = random.randint(0,7623)
alertstrings = ["friend requests will have their accounts DDoSed", "DO NOT accept his friend request", "send this to as many discord servers as you can"]
############END OF VARIABLES##########

@bot.event
async def on_ready():
        print("Bot is starting,")
        print("running as: " + bot.user.name)
        print("Game code - "+str(code))
        await bot.change_presence(game=discord.Game(name="Prefix is !!", type=1))
        print(":) Bot is now ready.")

@bot.event # problematic event
async def on_message(message): # This detects DDoSer copypasta
    global alertstrings
    for i in alertstrings:
        if i in message.content:
            print(message.author.display_name+" sent fake copypasta.")
            print("triggered at: "+i)
            await bot.send_message(message.channel, message.author.display_name+", this 'warning' is actually fake. Discord is not P2P, nobody can get your IP address through it.")
            await bot.process_commands(message)
        else:
            await bot.process_commands(message)

@bot.command(pass_context = True)
async def help(tx):
        await bot.say("the help message is much to long for this github issue so it's not here.")
@bot.command(pass_context = True)
async def hi(tx):
        await bot.say("Hi!")
        print("said hi")

@bot.command(pass_context = True)
async def about(tx):
        await bot.say("Test Thing Bot V2.5 \"basic greg\".") # this is how the bot says stuff

@bot.command(pass_context = True)
async def noticeme(tx):
        await bot.say("Noticed! :)")

@bot.command(pass_context = True)
async def cat(tx):
        await bot.say("https://loremflickr.com/320/240/cat,cute")

@bot.command(pass_context = True)
async def dadjoke(tx):
        joke = getjson(url="https://icanhazdadjoke.com/slack", call="attachments")
        joke = joke[0]
        joke = joke['fallback']
        await bot.say(joke)

@bot.command(pass_context = True)
async def changegame(tx, gcode, gname, type):
            global code
            if gcode == str(code):
                await bot.change_presence(game=discord.Game(name=str(gname), type=int(type))) # type 1 is 'playing', type 2 'listening', type 3 'watching'.
                code = random.randint(1,7623)
                print("New game change code is: "+str(code))
            else:
                await bot.say("Nice try...")

# Now get it up and running with said special token! OOH!

bot.run("api token")
question

Most helpful comment

This is because you loop through your three alertstrings, and even if each isn't in the message's content, you call process_commands each time.

For future questions like this, you should join either the official discord.py server or the Discord API server for help, as the README recommends.

>All comments

This is because you loop through your three alertstrings, and even if each isn't in the message's content, you call process_commands each time.

For future questions like this, you should join either the official discord.py server or the Discord API server for help, as the README recommends.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

synbitz picture synbitz  路  3Comments

adhoc92 picture adhoc92  路  3Comments

ghost picture ghost  路  3Comments

Nicba1010 picture Nicba1010  路  3Comments

Spyder-exe picture Spyder-exe  路  3Comments