Reputation: 51
I am trying to figure out how to make a command that 'reloads' a Discord Bot's commands and allows me to keep the bot running while I am adding new commands.
This just makes life easier for me so I don't have to reboot the bot.
I'm using the discord.py library to interact with the discord API.
How can I achieve this?
Upvotes: 4
Views: 20238
Reputation: 36
You can just use the basic reload built into discord.py
Here is an example of how my reload
command is done.
@bot.command()
@commands.is_owner()
async def reload(ctx, extension):
bot.reload_extension(f"cogs.{extension}")
embed = discord.Embed(title='Reload', description=f'{extension} successfully reloaded', color=0xff00c8)
await ctx.send(embed=embed)
Sends an embeded message when the cog is reload but you can always just do ctx.send(f'{extension} reloaded)
Upvotes: 1
Reputation: 773
Probably late to this questions, but I will post it anyways
You should checkout how so called "Cogs" work in Discord.py. The bot from Rapptz (the guy who main-maintain Discord.py) has some good examples how to organize your bot into Cogs and how to load/unload/reload them (see cogs/admin.py
for that).
@commands.command(hidden=True)
@checks.is_owner()
async def load(self, *, module : str):
"""Loads a module."""
try:
self.bot.load_extension(module)
except Exception as e:
await self.bot.say('\N{PISTOL}')
await self.bot.say('{}: {}'.format(type(e).__name__, e))
else:
await self.bot.say('\N{OK HAND SIGN}')
@commands.command(hidden=True)
@checks.is_owner()
async def unload(self, *, module : str):
"""Unloads a module."""
try:
self.bot.unload_extension(module)
except Exception as e:
await self.bot.say('\N{PISTOL}')
await self.bot.say('{}: {}'.format(type(e).__name__, e))
else:
await self.bot.say('\N{OK HAND SIGN}')
@commands.command(name='reload', hidden=True)
@checks.is_owner()
async def _reload(self, *, module : str):
"""Reloads a module."""
try:
self.bot.unload_extension(module)
self.bot.load_extension(module)
except Exception as e:
await self.bot.say('\N{PISTOL}')
await self.bot.say('{}: {}'.format(type(e).__name__, e))
else:
await self.bot.say('\N{OK HAND SIGN}')
Upvotes: 7