前缀和非前缀命令不能在python discord bot

时间:2017-12-02 17:54:30

标签: python python-3.x discord discord.py

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)

我正在尝试在我的不和谐测试服务器上完成这项工作,但是当我像这样使用它时,只有第一个“on_ready”和!hel命令有效,ping不会打印任何内容,但是当我删除!hel时命令代码部分,ping工作,有什么方法可以让它们一起工作吗?

2 个答案:

答案 0 :(得分:2)

使用@bot.command

时,将@bot.event更改为on_message

使用bot.process_commands

时添加on_message
  

为什么on_message让我的命令停止工作?

     

覆盖默认提供的on_message禁止运行任何额外命令。要解决此问题,请在on_message的末尾添加bot.process_commands(message)行。例如:

@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

您的代码应如下所示:

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)

答案 1 :(得分:0)

尝试使用@bot.command

替换on_message上方的@bot.event