前缀与非前缀的命令不是蟒蛇不和谐机器人一起工作

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

回答:

尝试用@bot.event

回答:

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

以上是 前缀与非前缀的命令不是蟒蛇不和谐机器人一起工作 的全部内容, 来源链接: utcz.com/qa/257570.html

回到顶部