【问题标题】:Discord py cogs not loading commandsDiscord py cogs 不加载命令
【发布时间】:2021-05-22 21:52:31
【问题描述】:

我制作了一个不和谐的机器人,其中包含一个主文件和另一个加载了 cogs 的命令文件,问题是这些命令没有加载。

我简化了代码来解决错误,但即使这样也不起作用:

bot.py:

import discord
from discord.ext import commands

import config

# declaring intents and launching client
intents = discord.Intents.default()
intents.members = True
intents.presences = True

bot = commands.Bot(command_prefix='.', description="Useful bot", intents=intents)

startup_extensions = ["private_channels"]

# listener
@bot.event
async def on_ready():
    print(f'Connected to Discord as {bot.user} !')

@bot.event
async def on_message(message):
    if message.author == bot.user:
        return
    print("message received")

# starting extensions
for extension in startup_extensions:
    try:
        bot.load_extension(extension)
    except Exception as e:
        exc = '{}: {}'.format(type(e).__name__, e)
        print('Failed to load extension {}\n{}'.format(extension, exc))

# starting bot
bot.run(config.token)

private_channels.py:

from discord.ext import commands

class PrivateChannels(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    # creates a new private channel
    @commands.command(name="test")
    async def test(self, ctx):
        await ctx.send("test")

def setup(bot):
    print("Loading private channels")
    bot.add_cog(PrivateChannels(bot))
    print("Private channels loaded")

【问题讨论】:

    标签: python file discord command bots


    【解决方案1】:

    好的,我遇到的主要问题是 on_message 事件会覆盖命令。为了纠正这个问题,我在 on_message 中添加了await bot.process_commands(message) 来处理命令,现在它可以工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-31
      • 2021-09-04
      • 1970-01-01
      • 1970-01-01
      • 2021-09-20
      • 1970-01-01
      • 2021-09-07
      相关资源
      最近更新 更多