0

I have this Discord.py Cog object that contains a bunch of methods that I would like to split into multiple files to browse easier. (most stuff cut out for brevity)

class VoteManager(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.group(pass_context=True, invoke_without_command=True)
    async def vote(self, ctx):
        await ctx.send(f'Command not found.  Use `{BOT_CALL_PREFIX}vote help` for commands.')

    def new(self, channel_id):
        pass  # Actual logic for new_command, separated so it could be called without context

    @vote.command(name='new')
    async def new_command(self):
        pass  # Really long function, etc.

What I want is to be able to split definitions like new and new_command into their own file new.py.

The VoteManager class is in the __init__.py file of discordbot.voting module. So when used, I use bot.add_cog(discordbot.voting.VoteManager(bot)) to add it to the registry of commands for the bot.

The goal is to be able to just do something like from .voting.new import * in the VoteManager class and have it read the functions to be added.

cclloyd
  • 8,171
  • 16
  • 57
  • 104
  • You could define pure function in separate files and call them from your class file by just passing the arguments and potentially the self. Or you actually compose multiple classes which feels maybe more clean. – The Fool Sep 16 '20 at 08:31
  • Does [this](https://stackoverflow.com/questions/47561840/python-how-can-i-separate-functions-of-class-into-multiple-files) help you? – InsertCheesyLine Sep 16 '20 at 10:15

0 Answers0