不和谐机器人文件的组织

organization of discord bot files

我有一个非常大的 discord 机器人,为了便于阅读,我想整理成文件夹。

我有主文件和设置文件。我现在正在接收来自主文件的 HTTP 响应,这在浏览文件时很难阅读。理想情况下,我希望 http 请求和命令位于不同的位置......虽然这主要是个人偏好,但我认为有人可以向我展示他们的组织方式。我还有不适合任何功能的功能,例如嵌入构建器功能

您可以使用 cogs 将命令分隔为 categories/files。

在您的主机器人文件(bot.py 或 main.py)上编写导入 os 并加载您的 cogs 文件的代码。

在你的 bot 的根文件夹中创建一个名为 cogs 的文件夹,然后创建一个你想要的名称的文件,例如 utility 并提供 .py 扩展名,然后是文件名 (utility.py 在这种情况下 ) 是你要放在列表中的那个(列表标记为# list

import discord
import os

# list 
initial_extensions = [
    'cogs.utilty',
]

# the code to load all your cogs
for extension in initial_extensions:
    try:
        client.load_extension(extension)
    except Exception as e:
        print(f'Failed to load extension {extension}.', file=sys.stderr)
        traceback.print_exc()

client.run('token')

现在在您的 cogs 文件 (utility.py) 上编写下一个代码。

import discord
from discord import client
import os
from discord.ext import commands


class Utility(commands.Cog):

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

    @commands.Cog.listener()
    async def on_ready(self):
        print('Bot is ready!')

不要忘记您可能需要重写 most 代码,但这很简单。 希望这可以帮助 : ) 文档:https://discordpy.readthedocs.io/en/stable/ext/commands/cogs.html