2017-12-02 185 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)