Pajbot structure
Tags: opensource
Published: 2022-01-07 01:45:31

## Make it run! ##
First step of every new repo - make it work in your environment. I was suggested to not bother with windows and use a VM or some cloud. I knew my Windows version would have problems running WSL, I tried that before. 

I followed repo's [instructions](https://github.com/pajbot/pajbot/tree/master/install-docs). 

The reason to choose cloud over other solutions is how problematic it would be to run nginx through localhost. Having a domain solves those issues. 

There were some details to set up properly, but luckily I wasn't the first with the issue.
<img src="https://cdn.discordapp.com/attachments/811366168949817414/928803366971244574/unknown.png" />


As soon as I ran the command to start the bot, the bot would say `up` in my channel chat. 
<img src="https://cdn.discordapp.com/attachments/353432193730871296/928800802343108608/Screenshot_331.png" />
I stop it and it says `down`. 
Cool, cool. So it works. 

<hr />

## About Pajbot structure ##

*I will not explain every step, but some examples of the main steps I did.*

We start from here:
[`pajbot/`](https://github.com/pajbot/pajbot/tree/master/pajbot) folder.

[`pajbot/web`](https://github.com/pajbot/pajbot/tree/master/pajbot/web) handles the web controller of the bot. You can find stuff like the list of all existing commands and their descriptions in here.
<img src="https://cdn.discordapp.com/attachments/353432193730871296/928824182203121684/Screenshot_337.png" />

[`pajbot/web/routes/api/commands.py`](https://github.com/pajbot/pajbot/blob/master/pajbot/web/routes/api/commands.py) is where we put data to display on that website. 

A description that I add [here](https://github.com/sirinoks/pajbot/commit/e8accfd6610b2040d2ddc73a33b80c2d2d4bffd7#diff-422df9a79c9a27c7c20d092ff3129c46cb58785b7ef46cb72a6a89226ddd468fR230-R235) will later be displayed on that web version like so:
<img src="https://cdn.discordapp.com/attachments/353432193730871296/928824000539418644/Screenshot_336.png" />


### Important files ###
[`pajbot/managers/command.py`](https://github.com/pajbot/pajbot/blob/master/pajbot/managers/command.py)
This file is the main one for commands. Here is where they are stored and they have their description.

[In here](https://github.com/pajbot/pajbot/blob/master/pajbot/managers/command.py#L432-L448) you can see all the arguments. This is the place where I would have to put mine. 

[`pajbot/models/command.py`](https://github.com/pajbot/pajbot/blob/master/pajbot/models/command.py)
Is the file with command classes. 

I started with adding my command to various places I noticed other commands go into.

Such as to this [class of base commands](https://github.com/sirinoks/pajbot/commit/e8accfd6610b2040d2ddc73a33b80c2d2d4bffd7#diff-a23ebbfa5495b1f6e6323d752eb7a26cb1c5c6724b333ecd0c81b13d52acaec6R205),

To this [list of options](https://github.com/sirinoks/pajbot/commit/e8accfd6610b2040d2ddc73a33b80c2d2d4bffd7#diff-a23ebbfa5495b1f6e6323d752eb7a26cb1c5c6724b333ecd0c81b13d52acaec6R270),

To the [argument list](https://github.com/sirinoks/pajbot/commit/e8accfd6610b2040d2ddc73a33b80c2d2d4bffd7#diff-422df9a79c9a27c7c20d092ff3129c46cb58785b7ef46cb72a6a89226ddd468fR451)...

I have had a problem with that one, actually. The bot would not parse the command properly. Guess why? Because a chat reply is a string! Not a boolean. So I had to parse it to a boolean before putting it in options. That's what this [piece of code](https://github.com/sirinoks/pajbot/commit/e8accfd6610b2040d2ddc73a33b80c2d2d4bffd7#diff-422df9a79c9a27c7c20d092ff3129c46cb58785b7ef46cb72a6a89226ddd468fR476-R484) does. Or just look at it here:
```py
        if "command_state" in options:
            state = options["command_state"].lower()
            if state == "true":
                options["command_state"] = True
            elif state == "false":
                options["command_state"] = False
            else:
                options["command_state"] = None
```

Another thing that was also mentioned in a [different issue](https://github.com/pajbot/pajbot/issues/838). The problem is that disabling a command actually deleted the command from the database. That hardcoded solution obviously created problems when you wanted to toggle those commands. To solve this, I added a new `chat_enabled` property to the database. [In here](https://github.com/sirinoks/pajbot/commit/e8accfd6610b2040d2ddc73a33b80c2d2d4bffd7#diff-0f9cf6ae238e2014ff05647bddfc9398e855f20ab7eac3e97d35b105c8ec13a2R200).

That was probably the most complex part of all this. Solution to that issue took more than that. I also needed to connect the logic of the command. It took some time fo find all places to connect my logic into (including the things I already mentioned above).