简体   繁体   English

Discord.py:如何在不使用 Cogs 的情况下在 OOP 中编写 discord 机器人? 我在尝试时遇到了错误

[英]Discord.py: How do I write discord bot in OOP without using Cogs? I have an error while I tried it

I have been working on discord bot for a pretty long time and now I decided that it is time that I start using the Bot with OOP as I am getting into it.我已经在 discord 机器人上工作了很长时间,现在我决定是时候开始使用带有 OOP 的机器人了。 I have given it a simple try and tried to run it but it was showing some error which I didn't understand.我给了它一个简单的尝试并尝试运行它,但它显示了一些我不明白的错误。

When I use the following code:当我使用以下代码时:

import discord
from discord.ext import commands

class JervisI(commands.Bot):
    def __init__(self, command_prefix, case_insensitive, botintents):
        self.command_prefix = command_prefix
        self.case_insensitive = case_insensitive
        self.botintents = botintents


J1 = JervisI(command_prefix="!", case_insensitive=True, botintents=discord.Intents.all())
J1.run("---HIDDEN TOKEN---")

When I run the code I get this error:当我运行代码时,我收到此错误:

Traceback (most recent call last):
  File "c:\Users\Bhavyadeep\Desktop\Discord Bot (Python)\PHub-Bot-1\jervis-1.py", line 12, in <module>
    J1.run("---HIDDEN TOKEN---")
  File "c:\Users\Bhavyadeep\Desktop\Discord Bot (Python)\PHub-Bot-1\The-Naval-Historian\lib\site-packages\discord\client.py", line 692, in run
    loop = self.loop
AttributeError: 'JervisI' object has no attribute 'loop'

I have no idea on how to do it.我不知道该怎么做。 Please do let me know where the error is.请让我知道错误在哪里。

Thank You: :D谢谢::D

You're overwriting the __init__ method of commands.Bot , if you take a look at the source code you can see that it's pretty long and important, you're gonna get a ton of AttributeError s if you don't call the "original" __init__ method:您正在覆盖commands.Bot__init__方法,如果您查看源代码,您会发现它非常长且重要,如果您不调用“原始代码”,您将获得大量AttributeError s " __init__方法:

class JervisI(commands.Bot):
    def __init__(self, command_prefix, case_insensitive, botintents):
        super().__init__( # Same arguments that you pass to `commands.Bot(...)`
            command_prefix=command_prefix, 
            case_insensitive=case_insensitive,
            intents=botintents
        )

        self.command_prefix = command_prefix # You don't need to create this, it already exists
        self.case_insensitive = case_insensitive
        self.botintents = botintents

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM