Prefixed and non prefix commands are not working together on python discord bot(前缀和非前缀命令在 python discord bot 上不能一起工作)
问题描述
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 上不能一起工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:前缀和非前缀命令在 python discord bot 上不能一起工作


基础教程推荐
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 筛选NumPy数组 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01