【问题标题】:How to reload all cog functions如何重新加载所有 cog 函数
【发布时间】:2021-05-12 12:06:15
【问题描述】:

我想在一个命令中重新加载所有带有 cogs 的文件。我有一个带有该功能的命令,用于重新加载文件,但该命令需要一个文件名。是否有默认重新加载所有文件的选项。 我正在考虑这个选项:

import fnmatch
import os

pattern = "*.py
path = "cogs"

list = fnmatch.filter(os.listdir("./extensions"), pattern)

这会输出一个列表:['event.py', 'info.py', 'start.py']

现在如何获取此列表并使用此函数重新加载此脚本?

@client.command()
async def reload(ctx, extension):
    client.unload_extension(f"cogs.{extension}")
    client.load_extension(f"cogs.{extension}")

【问题讨论】:

    标签: python list discord discord.py


    【解决方案1】:

    你听说过 for 循环吗?

    @client.command()
    async def reload_all(ctx):
        for ext in extensions_list: # Idk how you called it
            client.reload_extension(f"cogs.{ext[:3]}")
    

    另外已经有了reload_extension方法,不需要卸载/加载扩展

    【讨论】:

    • 比我已经找到了卸载所有脚本并加载所有新脚本的选项
    猜你喜欢
    • 2021-10-26
    • 2021-06-30
    • 2019-09-13
    • 1970-01-01
    • 1970-01-01
    • 2021-04-21
    • 2020-11-12
    • 1970-01-01
    • 2021-03-20
    相关资源
    最近更新 更多