Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is OOP possible using discord.py without cogs?

These last few days, I've been trying to adapt the structure of a discord bot written in discord.py to a more OOP one (because having functions lying around isn't ideal).

But I have found way more problems that I could have ever expected. The thing is that I want to encapsulate all my commands into a single class, but I don't know what decorators to use and how and which classes I must inherit.

What I've achieved so far is code like the snippet down below. It runs, but at the moment of executing a command it throws errors like

discord.ext.commands.errors.CommandNotFound: Command "status" is not found

I'm using Python 3.6.

from discord.ext import commands


class MyBot(commands.Bot):

    def __init__(self, command_prefix, self_bot):
        commands.Bot.__init__(self, command_prefix=command_prefix, self_bot=self_bot)
        self.message1 = "[INFO]: Bot now online"
        self.message2 = "Bot still online {}"

    async def on_ready(self):
        print(self.message1)

    @commands.command(name="status", pass_context=True)
    async def status(self, ctx):
        print(ctx)
        await ctx.channel.send(self.message2 + ctx.author)


bot = MyBot(command_prefix="!", self_bot=False)
bot.run("token")
like image 228
Axyss Avatar asked Aug 13 '20 21:08

Axyss


People also ask

Is discord.py getting discontinued?

Discord py is getting discontinued because Discord implemented more and more restrictions for Bot Developers, promised easy verification steps but is behind verification processes by months. Yet introducing more restrictions and now requiring even ID copies.

Does making a Discord bot cost money?

You do not need to install anything on your computer, and you do not need to pay anything to host your bot. We are going to use a number of tools, including the Discord API, Python libraries, and a cloud computing platform called Repl.it.

Is discord.py easy?

discord.py is a modern, easy to use, feature-rich, and async ready API wrapper for Discord. Features: Modern Pythonic API using async / await syntax.

Should I use cogs in my bot?

The advantage of using cogs in you bot, is that you can load and unload them without needing to restart the bot itself. So if you want to update a command, you can do so without having to restart the bot completely. Cogs also help with code organization. You can put all your "fun" commands in one cog, then all the "utilities" in another o whatever.

What is a cog in Python?

There comes a point in your bot’s development when you want to organize a collection of commands, listeners, and some state into one class. Cogs allow you to do just that. Each cog is a Python class that subclasses commands.Cog. Every command is marked with the commands.command () decorator.

How do I remove cogs from a bot?

Cogs are subsequently removed with the Bot.remove_cog () call. It should be noted that cogs are typically used alongside with Extensions. This example cog defines a Greetings category for your commands, with a single command named hello as well as a listener to listen to an Event.

What are cogs and how to use them?

Cogs also help with code organization. You can put all your "fun" commands in one cog, then all the "utilities" in another o whatever. Cogs can have their own event listeners, error handlers, checks, etc. To use them, please read the docs and check the examples.


Video Answer


2 Answers

To register the command you should use self.add_command(setup), but you can't have the self argument in the setup method, so you could do something like this:

from discord.ext import commands
    
class MyBot(commands.Bot):
    
    def __init__(self, command_prefix, self_bot):
        commands.Bot.__init__(self, command_prefix=command_prefix, self_bot=self_bot)
        self.message1 = "[INFO]: Bot now online"
        self.message2 = "Bot still online"
        self.add_commands()
    
    async def on_ready(self):
        print(self.message1)
    
    def add_commands(self):
        @self.command(name="status", pass_context=True)
        async def status(ctx):
            print(ctx)
            await ctx.channel.send(self.message2, ctx.author.name)
        
    
bot = MyBot(command_prefix="!", self_bot=False)
bot.run("token")
like image 167
Mandel Warrior Avatar answered Oct 23 '22 12:10

Mandel Warrior


I have the same problem and found this work-around today. But I found a similar solution without using a method to add the commands:

from discord.ext import commands

class DiscordBot(commands.Bot):
    def __init__(self):
        super().__init__(command_prefix="!")

        @self.command(name='test')
        async def custom_command(ctx):
            print("Hello world !")

    async def on_ready(self):
        print(f"Bot {self.user.display_name} is connected to server.")

bot = DiscordBot()
bot.run("token")
like image 33
Sunchock Avatar answered Oct 23 '22 12:10

Sunchock