gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 19:57:36 25 4
gpt4 key购买 nike

我有一个问题。我正在用“Discord.py”编写一个不和谐的机器人。我在“Raspberry Pi 3B”上使用“Python 3.8.1”。

我的主文件中有一个“加载”和一个“卸载”函数。他们做他们应该做的事。但我有 Cogs,例如最简单的一个:“ping”。我可以加载“ping”,但该命令不起作用(在每个 Cog 中):“忽略命令 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))

你发现什么错误了吗?

最佳答案

您必须将函数 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(self.client.latency * 1000)}ms')
return


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

关于python - 齿轮已加载,但功能不起作用(discord.py),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59458552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com