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...

The New Era of Space travel has started, and how you can go to space now (for free)

Ever since spaceflight was a thing, it has been used by governments to assert dominance against other countries, even the moon landing project was done solely for this purpose But this is changing. While government agencies like NASA and ISRO are doing all their great things, they are far more boring to the audience, because they are interested in science-based projects like satellites On the other hand, companies like SpaceX, Rocketlab, Virgin Galactic, and Blue origin have been working on something much bigger-  Space tourism.   And its not just a competition on who makes better and safer rockets for billionaires, its a race to push the boundaries of what humanity can do, which will inevitably (albeit slowly) make space available to the common people. Now lets talk about the rockets. Fun stuff! First, lets clear out the confusion between 'space' and 'orbit'  Space is just everything beyond the boundary of about 80 kms. Its a lack of atmosphere, but not a lack of gravi...

Unimaginable "Hacks" in Space which saved the day (pt.1)

Astronauts have a long history of solving problems with equipment and material not designed for the task, but, in space travel, you usually can't get the best tool for a job unless you brought it with you, so improvisation is a necessary part of space exploration. So in today's blog, we will show you, readers, some "Lifehacks" and smart problem-solving skills of the astronauts, which will surely blow your mind... So read it till the end! 1. Astronauts of the first moon mission barely made it back. Turns out, the Apollo missions are full of critical hacks that save the day, or the mission and sometimes, the crew. Apolo 11 had a moment where the crew discovered a circuit breaker cap on the floor of the lunar module after they did their surface EVA. Buzz Aldrin thinks that he probably hit it with the backpack of his EVA suit and he broke off the circuit breaker, which had been popped out after landing, now, that might not sound bad,i mean there was a lot of circuit br...