gpt4 book ai didi

python - 带前缀和非前缀的命令不能在 python discord bot 上一起工作

转载 作者:太空宇宙 更新时间:2023-11-04 02:37:11 27 4
gpt4 key购买 nike

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 有效,有什么方法可以让它们一起工作吗?

最佳答案

在使用on_message时将@bot.command更改为@bot.event

使用on_message时添加bot.process_commands

Why does on_message make my commands stop working?

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 - 带前缀和非前缀的命令不能在 python discord bot 上一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47610797/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com