【问题标题】:Discord bot only responds to one commandDiscord bot 只响应一个命令
【发布时间】:2020-11-03 21:59:15
【问题描述】:

我正在设置一个简单的 Python Discord 机器人,但它似乎只响应一个事件/命令。它只在有人说“supreme sauce”时做出响应,它会发送“raw sauce”,但不响应“.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')

【问题讨论】:

    标签: python python-3.x discord discord.py


    【解决方案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 代码

    【讨论】:

      猜你喜欢
      • 2020-12-15
      • 2021-08-28
      • 2021-04-09
      • 2021-02-17
      • 2023-03-09
      • 2021-04-17
      • 2020-02-03
      相关资源
      最近更新 更多