【问题标题】:Discord.py - Trying to load specific cogDiscord.py - 尝试加载特定的 cog
【发布时间】:2019-11-11 06:26:49
【问题描述】:

我正在尝试创建一个命令来加载我在“cogs”文件夹中创建的某个 cog

我已经浏览了文档和互联网(像往常一样),但对我没有任何帮助,我可能做错了什么?

代码很简单,但应该仍然可以工作

import discord
from discord.ext import commands
from discord.ext.commands import has_permissions, MissingPermissions
import os

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

@client.event
async def on_ready():
print ("Bot is online!")

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

@client.command()
@has_permissions(administrator=True)
async def unloadchat(ctx, extension):

 client.unload_extension("cogs.chat")

 await ctx.send('Unloaded the "chat" module :thumbsup: ')

client.run('TOKEN')

这当然不是整个机器人,但这是我想要做的,是的,我在令牌中添加了,只是没有公开

【问题讨论】:

  • 您遇到错误了吗?那个循环在on_ready 中吗?尝试在打印所有文件名的循环内添加一个 print,以确保您位于正确的目录中。尝试在cogs.chat 中粘贴setup 以确保它被调用。
  • 这不是一个循环,我对此感到困惑
  • for 开头的行是循环的开始。
  • 对不起,我已经解决了这个问题:)

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


【解决方案1】:

您可以尝试不同的方式。这也很棒

initial_ext = ['cogs.chat']

if __name__ == '__main__':
    for extension in initial_ext:
        try:
            client.load_extension(extension)
        except Exception as e:
            print(f"Failed to load the {extensiom}", file=sys.stderr)
            traceback.print_exc()

【讨论】:

    猜你喜欢
    • 2021-01-23
    • 2021-06-22
    • 1970-01-01
    • 1970-01-01
    • 2021-01-30
    • 2021-05-18
    • 1970-01-01
    • 2018-06-10
    • 2021-06-29
    相关资源
    最近更新 更多