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