client - on_message 使我的所有 bot.commands 停止工作
问题描述
我有一个翻转命令,它返回正面或反面,在我添加该on_message
函数之前它工作正常。我对 SO 做了一些研究并阅读了文档,这里说要包含await bot.process_commands(message)
在on_message
函数中,但这并没有解决问题,并且翻转命令仍然不起作用。如果我删除 on_message 函数,一切都会按预期工作。
僵尸软件
import discord
from discord.ext import commands
import random
# from app bot user
# click to reveal
TOKEN = 'token_here'
client = commands.Bot(command_prefix = '!')
@client.event
async def on_ready():
print('Bot ready')
@client.command()
async def flip():
flip = random.choice(['heads', 'tails'])
await client.say(flip)
@client.event
async def on_message(message):
if message.content.upper().startswith('N****'):
await client.send_message(message.channel, "Please do not use that word here.")
client.process_commands(message)
@client.event
async def on_member_join(member):
await client.change_presence(game=discord.Game(name='Hi %s' % (member)))
await client.send_message(member, "Hi %s, Welcome to Carson's Discord Server! This server is fairly NSFW at times; you've been warned! Enjoy your stay :)" % (member))
@client.event
async def on_member_remove(member):
await client.change_presence(game=discord.Game(name='Bye %s' % (member)))
client.run(TOKEN)
当我运行脚本时,其他一切都正常工作,没有错误。只是命令不起作用。任何见解将不胜感激。
解决方案
您需要await
将最后一行on_message
作为process_commands
协程。
await client.process_commands(message)
如果您发送!flip
不和谐,您应该能够收到适当的响应(heads
或tails
)。
推荐阅读
- javascript - 未调用 TypeScript 装饰器和私有类方法
- python - 无法从“froala_editor”导入名称“S3”
- javascript - 通过 Python 请求登录银行
- javascript - 无法在 javascript onclick 事件中提供链接
- json - 需要在 JavaScript 中渲染之前替换转义字符
- google-contacts-api - 如何使用 GTLRPeopleServiceQuery_PeopleGet?
- javascript - 在Javascript中过滤两个数组以将值匹配到新数组中
- android-activity - 有来电/去电时,应用程序的最近通话列表视图不会立即更新
- rest - Swagger codegen - api 和模型
- swift - 为什么使用 Self.self 打印 Self