laggron42 / Laggrons-Dumb-Cogs

Cogs for Red-DiscordBot made by Laggron
https://discord.gg/WsTGeQM
GNU General Public License v3.0
53 stars 51 forks source link

[Tournaments] message template #130

Open leElvyn opened 3 years ago

leElvyn commented 3 years ago

The plan is to completely change the way messages are handled in tournaments. Currently messages are just stored how you would expect, they're supposed to be used.

Due to the huge number of strings present in this cog, it has became almost impossible to allow user customizable messages. The plan is in two part :

Quick overview

First part : message handling

A new file containing a "Templates" class would be created. The class would be created at the bot's init, and would then be passed to every other part of the cog that requires messages. The class would contain multiple methods, each one would represent a message, like this :

class Templates():
    def __init__(self, bot, data):
        self.data = data

    def tournamentsinfo(self, ctxSelf):
        return _(
                "Laggron's Dumb Cogs V3 - tournaments\n\n"
                "Version: {0.__version__}\n"
                "Authors: {0.__author__[0]}, {0.__author__[1]} and {0.__author__[2]}\n\n"
                "Github repository: https://github.com/retke/Laggrons-Dumb-Cogs/tree/v3\n"
                "Discord server: https://discord.gg/AVzjfpR\n"
                "Documentation: http://laggrons-dumb-cogs.readthedocs.io/\n"
                "Help translating the cog: https://crowdin.com/project/laggrons-dumb-cogs/\n\n"
                "Support my work on Patreon: https://www.patreon.com/retke"
            ).format(ctxSelf)

And in the actual code :

    @commands.command(hidden=True)
    async def tournamentsinfo(self, ctx: commands.Context):
        """
        Get informations about the cog.
        """
        await ctx.send(self.templates.tournamentsinfo(self))

here, we just call a previously created templates objects, and then the arguments is what's used to format the message. That way, every messages are easily accessible.

Second part : message customization

A new command / subcommand would be created to allow server admins to use their own messages for the bot. The command will set an entry in the config (or be reset by setting it blank). When the code will call a method of the templates class, the method will first check if the key for this message template is blank in the config. If it is, it returns the default message. If it isn't, it will format the message with the context, and return it.

If the command is [p]custommessage, the base command with no arguments will simply return a tree of message categories ( announcments, staff messages, streams ...), and the user will be able to select the right message inside the categories. Each command's help will contain :

The context

When a message method is called, the context will be used to format the message. Context is thing like : {tournament}, which refers to the name of the tournament, {prefix}, or {participant} is the participant object, that can be used to mention the role : {participant.mention}, like Red's custom command system.

Major problem :

How does a user chose between a text message and an embed ?

since the send command takes either an embed, or a message, we can't just return an embed object or a string and send it.

leElvyn commented 3 years ago

Update :

I discovered that passing the Templates object in ever class won't be necessary. Instead, the object will simply be in the templates.py file.

simply : templates.py

class Templates():
    def __init__(self, bot, data):
        self.data = data
    def other_messages():
        pass

tournaments.py

from .objects import templates

... 

    def __init__(self, bot):
        templates.object = templates.Templates(data)

then, we just need to import the templates file to use the object : base.py

from .objects import templates

    def some_commands():
        templates.object.base_some_commands(context)