gpt4 book ai didi

python - 加载扩展的 discord.py 问题

转载 作者:行者123 更新时间:2023-12-04 15:28:58 31 4
gpt4 key购买 nike

我的 discord 机器人已连接到我的 discord 服务器,所有命令似乎都按预期运行。当我尝试使用加载或卸载命令时,它给了我一条错误消息“命令引发异常:ExtensionNotFound:无法加载扩展‘cogs.commands’。”我不知道为什么它说它们尚未加载,但扩展中的命令和事件仍在运行。我已经尝试重写加载和卸载命令的内容,我尝试将扩展名重命名为“事件”和“命令”。我只是一个初学者,我想我写错了。这是加载、卸载、重新加载和设置命令。

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

# cog unloader command
@client.command()
async def unload(ctx, extension):
client.unload_extension(f'cogs.{extension}')
print(f'{extension} successfully unloaded')

# cog reloader command, unload then load extenion
@client.command()
async def reload(ctx, extension):
client.unload_extension(f'cogs.{extension}')
client.load_extension(f'cogs.{extension}')
print(f'{extension} successfully re-loaded')

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

这是另一个文件,其中包含我目前作为扩展编写的事件和命令。

from discord.ext import commands

# --EVENTS--
class Events(commands.Cog):

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

# Bot online event
@commands.Cog.listener()
async def on_ready(self):
print('GuhBot v3 is online and ready! C:')

# Member joined Event
@commands.Cog.listener()
async def on_member_join(self, member):
print(f'{member} joined the server. C:')

# Member left Event
@commands.Cog.listener()
async def on_member_remove(self, member):
print(f'{member} left the server. :C')

# --MODERATION--
class Moderation(commands.Cog):

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

# clear command. default 5 messages, can be changed by user.
@commands.command()
async def clear(self, ctx, amount=5):
await ctx.channel.purge(limit=amount+1)

# Cog Setup
def setup(client):
client.add_cog(Events(client))
client.add_cog(Moderation(client))```

最佳答案

我知道已经晚了,但现在回答总比不回答好。 :) 如果您没有解决问题,这里可能会有帮助。

问题是找不到 cog,因为你给了他一个无效的路径(这就是你得到 ExtensionNotFound 异常的原因)。

我试过你的代码是这样的:

  1. 我制作了这个文件层次结构:photo在“cogs”文件夹中,我添加了一个名为 commands.py 的文件;
  1. main.py 中,我将您的第一个代码这样放置:
from discord.ext import commands
import os

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

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

# cog unloader command
@client.command()
async def unload(ctx, extension):
client.unload_extension(f'cogs.{extension}')
print(f'{extension} successfully unloaded')

# cog reloader command, unload then load extenion
@client.command()
async def reload(ctx, extension):
client.unload_extension(f'cogs.{extension}')
client.load_extension(f'cogs.{extension}')
print(f'{extension} successfully re-loaded')

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

client.run(TOKEN)
  1. cogs/commands.py 文件中,我编写了这样的 cog 代码:
from discord.ext import commands

# --EVENTS--
class Events(commands.Cog):

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

# Bot online event
@commands.Cog.listener()
async def on_ready(self):
print('GuhBot v3 is online and ready! C:')

# Member joined Event
@commands.Cog.listener()
async def on_member_join(self, member):
print(f'{member} joined the server. C:')

# Member left Event
@commands.Cog.listener()
async def on_member_remove(self, member):
print(f'{member} left the server. :C')

# --MODERATION--
class Moderation(commands.Cog):

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

# clear command. default 5 messages, can be changed by user.
@commands.command()
async def clear(self, ctx, amount=5):
await ctx.channel.purge(limit=amount+1)

# Cog Setup
def setup(client):
client.add_cog(Events(client))
client.add_cog(Moderation(client))
  1. 我运行机器人,当它在线时,我首先输入 Discord !unload commands,因为命令扩展已加载到此处的 main.py 文件中:
# for loop to find cogs folder
for filename in os.listdir('./cogs'):
if filename.endswith('.py'):
client.load_extension(f'cogs.{filename[:-3]}')
  1. 现在该扩展已卸载,我输入了 !load commands,它成功加载了。

因此请确保您为 load_extensionunload_extension 函数提供的路径是正确的。

关于python - 加载扩展的 discord.py 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61671671/

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