【问题标题】:Extentions and Cogs not working in discord.py v2扩展和 Cogs 在 discord.py v2 中不起作用
【发布时间】:2022-08-24 05:17:22
【问题描述】:

我正在尝试使用 cogs 和扩展名将我运行的不和谐机器人中的巨魔命令导出到新文件。但是,这些命令没有注册,我不确定为什么。我会在这里发送代码。 (忽略奇怪的功能启动,当你完全看机器人时它更有意义,但它的传播思想是 2k 行。)

#bot.py
import os
import sys
import time
import datetime

import discord
from discord.ext.commands import Bot
from discord import Intents
import assets
import role_counter
from discord.ext import commands
import discord.ext.commands
from dotenv import load_dotenv

import git_push
import merit_config
import trolls

load_dotenv()
TOKEN = os.getenv(\'DISCORD_TOKEN\')
TOKEN_TEST = os.getenv(\'DISCORD_TOKEN_TEST\')
GUILD = os.getenv(\'DISCORD_GUILD\')
cogs = [\'cogs.trolls\']


def startup(START):
    global LAUNCH
    global bot

    if START == TOKEN:
        intents = Intents.all()
        bot = commands.Bot(intents=intents, command_prefix=\'.\')
        bot.remove_command(\'help\')
        LAUNCH = TOKEN

startup(TOKEN)

initial_extensions = [\'cogs.trolls\']

if __name__ == \'__main__\':
    for extension in initial_extensions:
        bot.load_extension(extension)


def main():
    while True:
        bot.run(LAUNCH)
#trolls.py
import assets
from discord.ext import commands


class troll_commands(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command()
    async def troll(self, ctx):
        if ctx.channel.id == \'936902313589764146\' or \'939028644175699968\':
            await ctx.send(f\"```{assets.troll_command()}```\")
        await self.bot.process_commands()


def setup(bot):
    bot.add_cog(troll_commands(bot))

我已经反映了我能找到的每一个例子,它只是不工作,我知道为什么。如果我缺少一些要报告的代码,请告诉我,我会编辑这篇文章。

  • 您从哪里“镜像”第一个代码 sn-p?
  • 您是否在开发控制台中启用了意图?
  • 加载扩展现在是异步的,请参阅文档:discordpy.readthedocs.io/en/master/…
  • 为什么你有__name__ == \'__main__\',然后在它下面创建另一个从未调用过的主函数?另见上述评论。创建一个名为 start 的协程(使用 await client.start),然后在其中添加 cogs,使用 asyncio.get_event_loop() 运行它

标签: discord discord.py


【解决方案1】:

您必须使用 async 和 await 才能添加 cog。

您必须异步并等待扩展加载和运行机器人

等待 bot.load_extension(扩展)

(idk 在哪里使用异步)

并运行机器人

异步定义主(): 而真: 等待 bot.run(启动)

【讨论】:

    猜你喜欢
    • 2021-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-30
    • 2021-04-19
    • 2020-12-12
    • 2021-04-19
    • 1970-01-01
    相关资源
    最近更新 更多