2017-12-02 186 views
1
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可以工作,有什么方法可以让它们一起工作吗?前缀与非前缀的命令不是蟒蛇不和谐机器人一起工作

+0

在'hel',是'sot.say'应该是'bot.say'? –

+0

嗯,是的,我只是没有意识到这一点,我把这个普通名称改成了“bot”,使它看起来更简单并错误输入。这是在我的原代码 – Hera

回答

0

尝试用@bot.event

+0

是正确的,不幸的是,没有工作 – Hera

+0

它给了什么错误? – ADug

1

变化@bot.command@bot.event更换上述on_message@bot.command使用on_message

时添加bot.process_commands使用on_message

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

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

@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)