【问题标题】:Discord.py not loading commandsDiscord.py 不加载命令
【发布时间】:2021-03-16 13:32:47
【问题描述】:

大家好,我正在使用 python 3.8 在 discord.py 中制作一个不和谐机器人。我做了一些 cogs 并且每当我键入 load cog 命令或 unload cog 命令时它都没有显示任何问题机器人它的显示命令未找到。我的齿轮只有一个在工作,另一个不工作,请帮助我。

我的 Bot.py(主文件)

import discord
import os
from discord.ext import commands
from discord.ext.commands import CommandNotFound


client = commands.Bot(command_prefix = '#')


@client.command
async def load(ctx, extension):
    client.load_extension(f'cogs.{extension}')

@client.command
async def unload(ctx, extension):
    client.unload_extension(f'cogs.{extension}')

for filename in os.listdir('./cogs'):
    if filename.endswith('.py'):
        client.load_extension(f'cogs.{filename[:-3]}')



client.run('BOT TOKEN')

我的第一个 cog 文件 main.py

import discord
from discord.ext import commands


class main(commands.Cog):

    def __init__(self, client):
        self.client = client

    #Events
    @commands.Cog.listener()
    async def on_ready(self):
        print('Bot is online.')

    # Commands
    @commands.command(name='ping',help='Sends the latency of the Bot')
    async def ping(self, ctx):
        await ctx.send(f'**Pong!** Latency: {round(self.client.latency * 1000)}ms')


def setup(client):
    client.add_cog(main(client))

我的 2 cog 文件 moderation.py

import discord
from discord.ext import commands


class moderation(commands.Cog):

    def __init__(self, client):
        self.client = client

        @commands.command(name='clear', help='deletes no. of messages you give it')
        async def clear(ctx, amount = 1000):
            await ctx.channel.purge(limit=amount)

def setup(client):
    client.add_cog(moderation(client))

【问题讨论】:

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


    【解决方案1】:

    您的缩进错误 - 您在 __init__ 中声明了您的命令,而不是在您的类本身中。

    def __init__(self, client):
        self.client = client
    
    @commands.command(name='clear', help='deletes no. of messages you give it')
    async def clear(ctx, amount = 1000):
        await ctx.channel.purge(limit=amount)
    

    【讨论】:

    • 非常感谢我还有一个问题,加载和卸载命令在 Bot.py 中不起作用它显示'''Ignoring exception in command None: discord.ext.commands.errors.CommandNotFound :找不到命令“加载”'''请帮助
    • 你的装饰器错了,命令装饰器后面需要括号。 client.command 应该是 client.command()
    猜你喜欢
    • 2019-05-09
    • 1970-01-01
    • 2021-01-23
    • 1970-01-01
    • 2021-07-06
    • 2021-10-06
    • 2021-11-11
    • 2021-11-14
    • 2021-01-12
    相关资源
    最近更新 更多