已加载齿轮,但功能不起作用(discord.py)

时间:2019-12-23 16:32:35

标签: python bots discord discord.py

我遇到了问题。我正在用“ Discord.py”编写不和谐的Bot。我在“ Raspberry Pi 3B”上使用“ Python 3.8.1”。

我的主文件中有一个“加载”和“卸载”功能。他们按照应有的方式工作。 但是我有齿轮,例如简单的一个:“ ping”。 我可以加载“ ping”,但是该命令不起作用(在每个齿轮中): “忽略命令None中的异常: discord.ext.commands.errors.CommandNotFound:找不到命令“ ping””

我不知道问题可能在哪里。该守则似乎是正确的-根据其他人。我在上面观看了YouTube视频,但没有答案... 我将尝试使用另一个python版本,例如“ 3.7.x” ...

我的bot.py:

import discord
from discord.ext import commands

import os


client = commands.Bot(command_prefix = "/")



@client.event
async def on_ready():
    print(f'\n\nBot is ready!\nName: {client.user.name}\nID: {client.user.id}\n      ---------\n')

    return


@client.command()
async def load(ctx, extension):
    client.load_extension(f'cogs.{extension}') #loads the extension in the "cogs" folder
    await ctx.send(f'Loaded "{extension}"')
    print(f'Loaded "{extension}"')

    return


@client.command()
async def unload(ctx, extension):
    client.unload_extension(f'cogs.{extension}') #unloads the extension in the "cogs" folder
    await ctx.send(f'Unloaded "{extension}"')
    print(f'Unoaded "{extension}"')

    return



print('\n')

for filename in os.listdir('./cogs'): #loads all files (*.py)
    if filename.endswith('.py'):
        client.load_extension(f'cogs.{filename[:-3]}') #loads the file without ".py" for example: cogs.ping
        print(f'Loaded {filename[:-3]}')


client.run('MY TOKEN')

我的ping.py:

import discord
from discord.ext import commands


class Ping(commands.Cog):
    def __init__(self, client):
        self.client = client


        @commands.command()
        async def ping(self, ctx):
            await ctx.send(f'pong!\n{round(client.latency * 1000)}ms')

        return



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

您发现任何错误吗?

1 个答案:

答案 0 :(得分:0)

您必须将函数ping放在构造函数中: 它应该看起来像这样:

import discord
from discord.ext import commands


class Ping(commands.Cog):
    def __init__(self, client):
        self.client = client


    @commands.command()
    async def ping(self, ctx): # This was inside '__init__' before
        await ctx.send(f'pong!\n{round(client.latency * 1000)}ms')

        return



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