3
import asyncio
import discord
from discord.ext import commands
from discord.ext.commands import Bot
import chalk


bot = commands.Bot(command_prefix='!')

@bot.event
async def on_ready():
    await bot.change_presence(game=discord.Game(name='Test'))
    print("All systems online and working " + bot.user.name)
    await bot.send_message(discord.Object(id=386518608550952965), "All systems online and working")

@bot.command(pass_context=True)
async def hel(ctx):
    await bot.say("A help message is sent to user")


@bot.command
async def on_message(message):
    if message.content.startswith("ping"):
        await bot.send_message(message.channel, "Pong")




bot.run("TOKEN", bot=True)

I'm trying to get this work on my discord test server but when I use it like this, only the first "on_ready" and !hel command works, ping doesn't print anything, but when I delete the !hel commands code part, ping works, is there any way that I can make them work together?

Hera
  • 31
  • 1
  • 3

2 Answers2

2

Change @bot.command to @bot.event when using on_message

Add bot.process_commands when using on_message

Why does on_message make my commands stop working?

Overriding the default provided on_message forbids any extra commands from running. To fix this, add a bot.process_commands(message) line at the end of your on_message. For example:

@bot.event
async def on_message(message):
    # do some extra stuff here

    await bot.process_commands(message)

http://discordpy.readthedocs.io/en/latest/faq.html#why-does-on-message-make-my-commands-stop-working

Your code should look like this:

import asyncio
import discord
from discord.ext import commands
from discord.ext.commands import Bot
import chalk


bot = commands.Bot(command_prefix='!')

@bot.event
async def on_ready():
    await bot.change_presence(game=discord.Game(name='Test'))
    print("All systems online and working " + bot.user.name)
    await bot.send_message(discord.Object(id=386518608550952965), "All systems online and working")

@bot.command(pass_context=True)
async def hel(ctx):
    await bot.say("A help message is sent to user")


@bot.event
async def on_message(message):
    if message.content.startswith("ping"):
        await bot.send_message(message.channel, "Pong")

    await bot.process_commands(message)


bot.run("TOKEN", bot=True)
Community
  • 1
  • 1
Benjin
  • 3,448
  • 1
  • 10
  • 20
0

Try replacing the @bot.command above on_message with @bot.event

ADug
  • 324
  • 3
  • 10