Skip to main content

Rewriting Spacebot: Part3 - AUTOMOD, DATABASE GO BRRRRRRRRRRRRRRRRRRRRRRRR

 I'm really proud of this week.

    Over the last week I managed to make the Automod,  work. 

definitely wasnt as easy as I thought, but it works now. Join me through the thinking process and "Making it work" process in this blog.

Maybe follow me on twitter?

To be honest, setting up the database and caching was the hardest part. But this useful extension made everything so so so much easier.

You can follow me on twitter. I often share my experiences and stuff there 

Automod

Figuring out database structure

Some wise man told me, "Once you make your relational database, you regret the structure for your entire life. no matter how good or bad it is". 
Well guess what - i proved the wise man wrong. 
atleast in my opinion, the automod database model is as perfect as it gets

Basically what I did was :
- Make an Automod table with all the settings in it
         stuff like: Guild ID(BIGINT), enabled(BOOL) you know, all the settings.
- Make separate tables for all ignored settings
        Ignored channels, roles, users, banned words, etc.

And while i was at it, i also made a table to store the prefix of every guild.

OK, now we have the tables ready, lets fill them in.
But how? 
I didnt wanna manually put in stuff, so I made a class to handle the Database, with all the functions to work with it. 
I just decided that i should never have to use SQL queries outside of the database class, and i would, i MUST make a function in the Database class.

Now lets start with automod- (oof)

BUT WAIT - I don't wanna make a request to the database on EVERY message, that would be kinda dumb and slow. so now, we have to set up Caching
This is another reason why I'm rewriting Spacebot, I'm figuring out solutions to problems i never thought existed.

I was clueless, asked many people what would be the best way to do it, i had the following Options:
- To gather database cache every x minutes
   - No instant changes
   -  Inefficient, as it may pull the entire database even though there's no change
- To just not cache
    - Yes, that's dumb

But then, CloudedQuartz from my discord server (discord.io/code) recommended me to update the cache only when there's some change in the database. This was the aha moment for me (even though that's the first thing i should have thought of lol)

So I went ahead and made this wild function that converts the entire database to a dictionary

And also a function to only fetch the guilds that have been updated (That should help a lot)

And I made it so that the function is updated every time any function with the @automod_cache decorator is used, very cool.

Now, I'll only read from the cache, not the database.That should save bandwidth+Time

*sigh* lets start with the actual auto-moderation

I already had the settings and cache now, also the guild-specific configurations, now all that's left, is to ACTUALLY make the automod

Spam detection

How do I detect spam? I decided NOT to use any pre-built framework or a cheat or a hack and to figure out a solution myself.
coming up with it was actually kinda difficult, so i put 5 points:

The users can set a Spam_threshold and Spam_interval, which would basically mean that 
spam_threshold messages are allowed every spam_interval.

I got an idea- lets cache messages - no, not the message content, just the timestamp, that's all we need now.
So I made a dictionary that will act as the cache and a cache handler for the messages

where I can store the spam_threshold message timestamps - channel and member specific.

if spam_threshold is reached, I check the first timestamp and calculate the timedelta between first and first+threshold. This works particularly well and is super fast too.

As you can see though, the code does NOT look good hahahahaha

All other stuff, I just did using regex. 

I also learnt quite a bit about regex here, which was really cool too.


That's not even it, but this blog is getting really big haha


How do i end it?

I'm getting really lazy writing these blogs and that was just the first-week progress, in the next blog, join me as I make the SpaceBot website (using Inter-Process-Communication), and code the Action logs (stuff like member join, leave, role create updates).

Want to be updated whenever I post a new blog? 

Follow me on twitter, Join our discord server or message me to get automated email updates (Blogger isnt letting me put the email form here, idk why.)


Dhravya Shah

I'm a (learning) developer who loves to try new things, make new projects, automate everything possible. I'm also a freelancer.
Check out my website and let me know how it is 👀

https://dhravya.me


Comments

Popular posts from this blog

Rewriting Spacebot: Part1 - Deciding features and tech stack

Why Rewrite? The answer is very simple: I'm not really happy with the current state of Spacebot. Its growth is slowing down, I'm less motivated to work on the project because of just how basic it is. But this is gonna change now. Deciding the features I don't just want to make Spacebot multipurpose, I wanna make it a *good* multipurpose bot.  That is, every feature that I add to SpaceBot should also add a considerable amount of value to any discord community, which is what I think Spacebot lacked earlier. It has like 60 utility commands but most of them aren't done well. Spacebot has these features currently:  Very bad moderation  Very bad "Fun" commands  Kinda useful Utility commands but most aren't good  Kinda good Music system, I might just keep the music system as it is in the rewrite (with a few visual changes) And then there's button roles, Discord together and stuff. Which isnt really good tbh What I want to include in the rewrite: Moderation, A...

Rewriting Spacebot: Part2 - Having FUN with VIEWS VIEWS VIEWS

Rewriting SpaceBot p-2 This is just day 1 and I feel very confident, even though it's been a little difficult. I decided to start  with the bot in such a way that I can scale it almost infinitely, so I jotted down 5 things to do for the day: Complete the main file, Set up a good logging system, Decide a file structure for super clean code, Complete the fun cog WITH slash commands, and decide the database schema. All that, while travelling from Mumbai to Deolali  Do you think I managed to do it? I did most of this coding while in the car travelling from Mumbai to Deolali (I'm returning home as I write this blog).  Unpopular opinion: coding while travelling is super fun because no one knows what you are doing and why you are doing it — Dhravya Shah (@DhravyaShah) January 23, 2022   By the way, follow me on twitter A good logging system First, I asked myself: What makes a good logging system? These are some criteria I put for myself Proper log levels On-point log messa...