【问题标题】:discord.py @bot.command() not runningdiscord.py @bot.command() 没有运行
【发布时间】:2021-03-20 07:11:45
【问题描述】:

我有类似的东西。

from flask import Flask
from threading import Thread
import discord
from discord.ext import commands, tasks
from discord.utils import get
import requests
from Moderator.badwords import words
import time
import datetime
from Stats.uptime import data

help_command = commands.DefaultHelpCommand(
    no_category = 'Commands'
)

intents = discord.Intents().all()
bot = commands.Bot(command_prefix='!', description="Hey there, I'm Botty (for example)!", help_command=help_command, intents=intents)

@bot.command()
async def hello(ctx):
  await ctx.send(ctx.author.mention + " hello!")

@bot.event
async def on_ready():
  print('Ready!')

@bot.event
async def on_message(message):
    for word in words:
        if word in message.content.lower():
            await message.delete()
            await message.channel.send("Oops! Seems like " + message.author.mention + " was trying to send a message that was breaking the " + bot.get_channel(783064049859559434).mention + ". Luckily, I deleted it before it caused any more damage. Don't send any more messages like that!\n\nIf you think that I made an error, please report it in " + bot.get_channel(783074030265040916).mention + ", " + bot.get_channel(783074002255478848).mention + " or in " + bot.get_channel(783092164590174251).mention)

@bot.event
async def on_member_join(member):
  print(f"{member} joined the server")

bot.run(TOKEN)

现在,我可以毫无错误地编译和运行此代码,并且当成员加入或用户发送消息时,一切正常。但是当涉及到运行命令时,它甚至没有启动。我有什么遗漏吗?

提前致谢

【问题讨论】:

  • 您正在使用类似于bot = commands.Bot(command_prefix='$') 的内容初始化机器人?
  • 错误发布所有代码
  • @thshea 但是是的,它已经初始化了

标签: python function bots discord.py discord.py-rewrite


【解决方案1】:

看来这段代码没有问题,你是否正确定义了前缀? 要定义前缀,您需要从 discord.ext 导入 commands 您的机器人应如下所示:

from discord.ext import commands
import discord
TOKEN = ...
bot = commands.Bot(command_prefix=".")
#####
async functions
#####
bot.run(TOKEN)

【讨论】:

  • 它已经被定义了,虽然它不在发布的代码中。代码在今天之前运行良好。
【解决方案2】:

The documentation:

覆盖默认提供的 on_message 会禁止运行任何额外的命令。要解决此问题,请在 on_message 末尾添加 bot.process_commands(message) 行。

如果您覆盖on_message,则需要使用await bot.process_commands(message) 以便处理命令。尝试将其添加到您的 on_message 事件中。

【讨论】:

  • 是的!现在它起作用了。请添加await,因为它丢失了,所以其他人不会感到困惑
猜你喜欢
  • 2021-06-05
  • 2022-11-22
  • 1970-01-01
  • 2021-05-20
  • 2018-09-25
  • 2021-09-19
  • 2021-11-08
相关资源
最近更新 更多