lukaqueres / Frequency

Discord bot with features including: planning, administration, and music playing.
http://theplanbot.com
GNU Affero General Public License v3.0
2 stars 0 forks source link

[ BOT ] - New slash command - `debug`; #34

Closed lukaqueres closed 2 years ago

lukaqueres commented 2 years ago

Background

Because of refactoring and change in database schema, there are guilds without database records.

It wouldn't be so important ( especially right now with literally no feature using it ) but every message needs a prefix, which is retrieved from the database, and recorded with guild's id.

It was solved by adding an exception in case of no prefix being retrieved.

Function in bot/modules/discord.py has gained exception handling:

def prefix(client, message):
    with open('./configuration.json', 'r') as c: # - Open 'configuration.json' json file. Getting logging details. -
        configuration = json.load(c); 
        log = configuration['developer']['log'];
        defaults = configuration['values']['defaults'];
    try:
        database = Database(); # - TODO: Check how to get database object from bot.py main file, for now this will do -
        properties = database.select(table = 'guilds.properties', 
            columns = ['prefix'],
            condition = {"id": message.guild.id}
            );
        prefix = prefix;
    except Exception as e:
        if log['exceptions']:
            prefix = defaults['prefix'];
            print(f'Error while getting prefix: {getattr(e, "message", repr(e))}');
    return prefix;

It is worth noting that this code may change during development.

About command(s)

With slash commands there is the possibility to group multiple subcommands under one command ( it is necessary as there is a limit of 50 slash commands per bot, so these 50 slots are quite precious ).

It is not certain for now how many and with what names/uses subcommands there will be, bot for now for shure there'll be:

As mentioned before, I will think about names, but especially for this one command one. For now it is debug, with possible syntax: /debug show or /debug refresh.

Solutions

I am not sure about these subcommands, only I know that these exist and may be used ( this may help: https://github.com/lukaqueres/plan-it/issues/33). Because of no arguments to pass ( for now, but it is good to look ahead ), these subcommands can be made with string input, and case inside calling proper functions.

It is worth noting that with subcommands there will be easy ways to edit names/add new or something.

It will also require thinking about file scheme and location. Probably will make, like bot/cogs/debug.py file with debug functions/commands.

Summary

Nice and easy summary in form of TODO list:

Note that it is the first issue in the new semester and refactor milestone!

lukaqueres commented 2 years ago

First point in TODO list checked. Created a file with code based on these examples.

Thought about names. Decided to keep debug names for more... well for debug purposes, with configuration name better convey command purpose. I'll submit the proper issue later.

lukaqueres commented 2 years ago

There is problem with import of app_commands from discord. There is a request for @kocielnik to look into poetry and check it. It may help: https://stackoverflow.com/a/73340597.

Error code:

ImportError: cannot import name 'app_commands' from 'discord'

Keep in mind that there is special branch for this feature.

lukaqueres commented 2 years ago

A little rectification here. In issue I pointed put that there is 50 slash commands slots. Now I've figoured out, that they has had to increase them number. Now it looks like as follows:

More about it, and registering slash commands can be read in the discord docs.

lukaqueres commented 2 years ago

There is problem with import of app_commands from discord. There is a request for @kocielnik to look into poetry and check it. It may help: https://stackoverflow.com/a/73340597.

Error code:

ImportError: cannot import name 'app_commands' from 'discord'

Keep in mind that there is special branch for this feature.

Just fixed it by changing discord.py version in poetry file. Weird tho, was shure tried that already. Must've been another branch.

Next thing to solve is to sync slash commands with discord. Right now it doesn't seem to work.

lukaqueres commented 2 years ago

Update:

I made a slash command group a while ago, but at this point i am struggling to sync it with discord.

Now there is ping command awailabe to use ( code ), which, even though it was made for one guild ( at least synchronization was made with one guild sync in mind ) can be used in every guild.

Will keep trying to sync that group. Cheers!

lukaqueres commented 2 years ago

Another update:

Made a workaround ( DAMN, I really hoped code, at least, in main file would be left alone ) that is working for now ( but ping has doubled, like there are now two ping commands )

https://github.com/lukaqueres/plan-it/blob/d46e0d2f257034607a0636d039e7f18470ca683f/bot/bot.py#L147

lukaqueres commented 2 years ago

Added commands group (As I see, they are used as separate commands ), with only command body left to develop. Commands are now restricted in configuration.json file.

Added quality of life update for activities set allowing for multiple f. ex. 'watching' lists.

lukaqueres commented 2 years ago

As a part of commands development process, basic class PIEmbed was created in packets.discord.py ( feel free to have a look ).

It will be used for Embeds generation, and for now it's most usefull functionality, is setting default values.

So as from today

It is necessary to use PIEmbed as a subsitute of discord.Embed PIEmbed has default attributes:

With these information handed out, please do not assign them manually, as it would require more work in case of f. ex. color change.

It is still possible to assign these values, but do it in emergency-only.

In fact, if there will be many cases where one-scheme embed will be used, it is possible to make sub-classes of PIEmbed with different colors or footers ( or anything else ).

lukaqueres commented 2 years ago

Aye, didn't thought that there is no usable background functions ( like error handling, or even custom errors ).

So now, there is a shit tone of functions to add, classes to define and many things to think about.

Currently working on custom errors, next will make whole-bot-comes errors handling cog, for better quality work in later parts.

lukaqueres commented 2 years ago

Command is working, there are still more tests to perform, currently working on custom PIEmbed class. Added custom commands for adding fields.

PIEmbed.add.field(self, index: Optional[int] = None, name: Optional[str] = None, value: Optional[str] = None, inline: Optional[bool] = False)

and

PIEmbed.add.emptyField(self, index: Optional[int] = None, inline: Optional[bool] = False)

These will handle long field content, dividing it into multiple fields.

lukaqueres commented 2 years ago

I hope it is last comment here. As of today, this issue has 14 days, and in this same time it may be resolved.

Commands are pretty much ready now, there is only some cleaning left ( or to leave for next issue, probably this ).

Will probably close it later today.

lukaqueres commented 2 years ago

As an dot over an i, events cog was extended with roles-related events ( create, edit, delete ), and will automaticly update DB record with freshest changes.