简体   繁体   中英

How would I make a reload command in Python for a discord bot?

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?

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}')

( Snippet from cogs/admin.py )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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