問題描述
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)
我試圖在我的 discord 測試服務器上完成這項工作,但是當我像這樣使用它時,只有第一個on_ready"和 !hel 命令有效,ping 不打印任何內容,但是當我刪除 !hel命令代碼部分,ping 有效,有什么方法可以讓它們一起工作嗎?
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?
推薦答案
使用on_message
@bot.command改為@bot.event
>
Change @bot.command
to @bot.event
when using on_message
在使用on_message
時添加bot.process_commands
為什么 on_message 會讓我的命令停止工作?
覆蓋默認提供的 on_message 會禁止運行任何額外的命令.要解決此問題,請在 on_message 末尾添加 bot.process_commands(message) 行.例如:
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
您的代碼應如下所示:
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)
這篇關于前綴和非前綴命令在 python discord bot 上不能一起工作的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!