Subclassing Bots
About
Subclassing is another popular way of creating Discord Bots. Here, you create a bot by extending a bot class. To some advanced users, this is preferable to creating a bot instance with bot = discord.Bot()
.
Subclassing is an intermediate python concept, so we recommend you learn about it before continuing. Some good resources are:
Why Subclassing?
Many developers prefer to subclass their bots for many reasons, some of them being:
Skeletal Frameworks for Bots
Developers often share their bots code on open-source platforms, mostly GitHub, and allow users to self-host the bot (running their own instance of the bot with the code provided by the developer. Permissions vary in accordance to licences). Here, users find it easier and more convenient to have a small file that imports the custom bot class and runs it, rather than having to go through the code of the bot and find out which file to run. This makes it easier for people not familiar with programming to run and customize the bot, since it often brings down the code to:
from src import Bot
bot = Bot()
bot.run("TOKEN")
Here, all the commands are inside the /src
folder that the user need not bother with.
Running Multiple Instances
Some developers may need to run multiple instances of their bot (perhaps on different bot accounts). For example, a developer might have a second bot for testing alpha features. This system makes it simpler for the developer, and allows them to maintain multiple versions of the bot in the same directory:
from src import Bot
class AlphaBot(Bot): # subclasses Bot
... # insert any custom configuration
bot = AlphaBot()
@bot.slash_command() # adds a new slash command to this subclassed bot
async def alpha_feature(ctx):
await ctx.respond("Alpha Feature!")
bot.run("TOKEN")
There are many more reasons to subclass! It's not required, and won't affect the speed of the bot, but it may affect your development process, for the good or for the worse. You don't miss out on any features when you subclass, either. Some developers want the OOP feel, while some just prefer that method and find it easier.
Basic Example
import discord
class MyBot(discord.Bot): # subclass discord.Bot
async def on_ready(self): # override the on_ready event
print('Logged in as')
print(self.user.name)
print(self.user.id)
print('------')
bot = MyBot()
@bot.slash_command() # create a slash command
async def ping(ctx):
await ctx.respond('Pong!')
bot.run('token')
As you can see, instead of creating a bot object with bot = discord.Bot()
, we subclass discord.Bot
. We then create an instance of our new bot class and run it. Notice how we don't need to use the @event
decorator.
Here's another example:
import discord
class MyBot(discord.Bot): # subclass discord.Bot
async def on_ready(self): # override on_ready
print('Logged in as')
print(self.user.name)
print(self.user.id)
print('------')
from .bot import MyBot # import the MyBot class from the bot.py file
from src import MyBot # import MyBot from /src
bot = MyBot() # create an instance of MyBot
@bot.slash_command()
async def ping(ctx):
await ctx.respond('Pong!')
bot.run("TOKEN") # run the bot
These are just two ways you could do it. There are ton of other structures you can implement. It's up to you.
So, should you subclass? There are no limitations you face if you decide to subclass your bot, but, once again, it's up to you.