我正在设置一个简单的Python Discord机器人,但它似乎只响应一个事件/命令。它仅在有人说“最高调味料”时做出响应,发送“未加工调味料”,但不响应“ .ping”或“ .clear”之类的其他内容。
我在做错什么吗?
我的代码:
import discord
from discord.ext import commands
import time
client = commands.Bot(command_prefix = '.')
@client.event
async def on_ready():
print(f'{client.user} has successfully connected to Discord!')
@client.event
async def on_message(message):
if 'supreme sauce' in message.content:
await message.channel.send('raw sauce')
@client.command()
async def ping(ctx):
await ctx.send(f'Pong! {round(client.latency * 1000)}ms')
@client.command
async def clear(ctx, amount=10):
await ctx.channel.purge(limit=amount)
client.run('My Token')
答案 0 :(得分:1)
on_message优先于命令。 如果您希望两件事都发生,请执行以下操作:
async def on_message(message):
if message.author == bot.user: return #Makes sure it can't loop itself when making messages
await bot.process_commands(message)
#rest of your code here
这样可以使它在发送消息时将检查该消息是否为命令并从那里发送,然后它将像通常一样执行on_message代码