client - on_message 停止我所有的 bot.commands 工作

标签 client bots discord.py

我有一个翻转命令,可以返回正面或反面,并且在我添加 on_message 函数之前它工作正常。我对 SO 做了一些研究并阅读了文档和 here它说要在 on_message 函数中包含 await bot.process_commands(message) ,但这并没有解决问题,翻转命令仍然不起作用。如果我删除 on_message 函数,一切都会按预期进行。

机器人.py

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在不和谐中,您应该能够收到适当的响应( headstails )。

关于client - on_message 停止我所有的 bot.commands 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51391964/

相关文章:

javascript - 隐藏不和谐机器人消息上的链接

python - Discord.py - 如何制定角色特定命令?

http-headers - HEAD 请求收到 "403 forbidden"而 GET "200 ok"?

java - 如何使用jwbf上传文件?

python - 如何在 Discord.py 中获取用户的私有(private)消息 channel ?

带有 async def 的 Python [无效语法]

ruby SSL_read : tlsv1 alert protocol version

networking - boost::asio tcp async_accept 处理程序未被调用但同步连接工作正常

perl - 在 Perl 聊天服务器中同时传输数据?

C TCP 客户端无法连接到服务器