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

How Starship will change the world

By Dhravya Shah and Sushant Pulipati We all have dreamt of seeing humans on Mars one day. Well, there is one man who will surely get us there. Elon musk.  Elon Musk                         What would you do if you are a millionaire and a vision to change the world?  Of course, you would make a space company with only 1 dream - GET TO MARS .  This is how Starship will change humanity - FOREVE R Starship is a rocket, designed by space company SpaceX. If it becomes successful, it will be by far the most efficient rocket ever designed. Why? Well,  Imagine if you have a paper aeroplane, you throw it once, and then you have to throw it in the dustbin, Waste of resources, RIGHT? You use your energy, time and paper to make an aeroplane, just to throw it once? That's where reusability comes in.  Normally, rockets are flown once and then thrown away in the ocean. All space companies do this.  EXCEPT for  SpaceX.  SpaceX has been reusing the first stage of their rockets (Falcon 9 and Falcon

Nokia - Connecting..... Earth to Moon ?

Nokia has always been nostalgic for the general population, but, after it got defeated by the iPhone in 2007, it has been almost dead, Until now. Nokia is starting a revolution and going to the moon to establish a strong 4g network to boost up the speed and communications between the astronauts   About the mission Nokia has been given a second life by Nasa - in a recent $14.1m contract, awarded to Nokia’s US subsidiary, which is part of Nasa’s Artemis programme which aims to send the first woman, and next man, to the moon by 2024. OK, let's talk about the Artemis mission now.   NASA plans to send the next man and first woman to the lunar surface . The last time NASA sent humans to the Moon was in 1972, during the Apollo lunar mission. And I am very happy to be a p art of this space era.   The Finnish equipment manufacturer said it was selected by Nasa to deploy an “ultra-compact, low-power, space-hardened” wireless 4G network on the lunar surface, as part of the US space agenc