Skip to content
This repository was archived by the owner on Feb 7, 2026. It is now read-only.

Commit dfb809a

Browse files
authored
Merge pull request #315 from PyBotDevs/automod-library
Migrate all automod db functions to a framework module
2 parents f9d40aa + cf64eec commit dfb809a

3 files changed

Lines changed: 85 additions & 38 deletions

File tree

cogs/automod.py

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,13 @@
22

33
# Imports
44
import discord
5-
import json
65
from discord import option, ApplicationContext
76
from discord.ext import commands
7+
from framework.isobot.db import automod
88

99
# Variables
10-
1110
color = discord.Color.random()
12-
13-
with open('database/automod.json', 'r', encoding="utf-8") as f: automod_config = json.load(f)
14-
15-
def save():
16-
with open('database/automod.json', 'w+', encoding="utf-8") as f: json.dump(automod_config, f, indent=4)
11+
automod = automod.Automod()
1712

1813
# Commands
1914
class Automod(commands.Cog):
@@ -25,7 +20,7 @@ def __init__(self, bot):
2520
description="Shows the current automod configuration for your server"
2621
)
2722
async def automod(self, ctx: ApplicationContext):
28-
loaded_config = automod_config[str(ctx.guild.id)]
23+
loaded_config = automod.fetch_config(ctx.guild.id)
2924
localembed = discord.Embed(title=f"{ctx.guild.name}\'s automod configuration", descripton="Use the `/automod_set` command to change your server's automod configuration.", color=color)
3025
localembed.set_thumbnail(url=ctx.guild.icon_url)
3126
localembed.add_field(name="Swear-filter", value=loaded_config["swear_filter"]["enabled"])
@@ -39,34 +34,30 @@ async def automod(self, ctx: ApplicationContext):
3934
)
4035
@option(name="toggle", description="Do you want to turn it on or off?", type=bool)
4136
async def automod_swearfilter(self, ctx: ApplicationContext, toggle:bool):
42-
loaded_config = automod_config[str(ctx.guild.id)]
4337
if not ctx.author.guild_permissions.administrator: return await ctx.respond("You cannot use this command. If you think this is a mistake, please contact your server owner/administrator.", ephemeral=True)
44-
if loaded_config["swear_filter"]["enabled"] == toggle: return await ctx.respond(f"That automod option is already set to `{toggle}`.", ephemeral=True)
45-
loaded_config["swear_filter"]["enabled"] = toggle
38+
if automod.fetch_config(ctx.guild.id)["swear_filter"]["enabled"] == toggle: return await ctx.respond(f"That automod option is already set to `{toggle}`.", ephemeral=True)
39+
automod.swearfilter_enabled(ctx.guild.id, toggle)
4640
if toggle is True: await ctx.respond("Swear-filter successfully **enabled**.", ephemeral=True)
4741
elif toggle is False: await ctx.respond("Swear-filter successfully **disabled**.", ephemeral=True)
48-
save()
4942

5043
@commands.slash_command(
5144
name="automod_use_default_keywords",
5245
description="Choose whether or not you want to use the default keywords for automod's swear-filter"
5346
)
5447
@option(name="toggle", description="Do you want to turn it on or off?", type=bool)
5548
async def automod_use_default_keywords(self, ctx: ApplicationContext, toggle:bool):
56-
loaded_config = automod_config[str(ctx.guild.id)]
5749
if not ctx.author.guild_permissions.administrator: return await ctx.respond("You cannot use this command. If you think this is a mistake, please contact your server owner/administrator.", ephemeral=True)
58-
if loaded_config["swear_filter"]["keywords"]["use_default"] == toggle: return await ctx.respond(f"That automod option is already set to `{toggle}`.", ephemeral=True)
59-
loaded_config["swear_filter"]["keywords"]["use_default"] = toggle
50+
if automod.fetch_config(ctx.guild.id)["swear_filter"]["keywords"]["use_default"] == toggle: return await ctx.respond(f"That automod option is already set to `{toggle}`.", ephemeral=True)
51+
automod.swearfilter_usedefaultkeywords(ctx.guild.id, toggle)
6052
if toggle is True: await ctx.respond("Using default swear-filter keywords successfully **enabled**.", ephemeral=True)
6153
elif toggle is False: await ctx.respond("Using default swear-filter keywords successfully **disabled**.", ephemeral=True)
62-
save()
6354

6455
@commands.slash_command(
6556
name="automod_view_custom_keywords",
6657
description="Shows a list of the custom automod swear-filter keywords set for your server",
6758
)
6859
async def automod_view_custom_keywords(self, ctx: ApplicationContext):
69-
loaded_config = automod_config[str(ctx.guild.id)]
60+
loaded_config = automod.fetch_config(ctx.guild.id)
7061
out = ""
7162
if loaded_config["swear_filter"]["keywords"]["custom"] != []:
7263
i = 0
@@ -85,10 +76,9 @@ async def automod_view_custom_keywords(self, ctx: ApplicationContext):
8576
@option(name="keyword", description="What keyword do you want to add?", type=str)
8677
async def automod_add_custom_keyword(self, ctx: ApplicationContext, keyword:str):
8778
if not ctx.author.guild_permissions.administrator: return await ctx.respond("You cannot use this command. If you think this is a mistake, please contact your server owner/administrator.", ephemeral=True)
88-
loaded_config = automod_config[str(ctx.guild.id)]
79+
loaded_config = automod.fetch_config(ctx.guild.id)
8980
if keyword not in loaded_config["swear_filter"]["keywords"]["custom"]:
90-
loaded_config["swear_filter"]["keywords"]["custom"].append(keyword)
91-
save()
81+
automod.swearfilter_addkeyword(ctx.guild.id, keyword)
9282
localembed = discord.Embed(description=f"New swear-filter keyword `{keyword}` successfully added to configuration.", color=discord.Color.green())
9383
await ctx.respond(embed=localembed, ephemeral=True)
9484
else: return await ctx.respond("That keyword is already added in your automod configuration.", ephemeral=True)
@@ -98,12 +88,9 @@ async def automod_add_custom_keyword(self, ctx: ApplicationContext, keyword:str)
9888
description="Removes a custom keyword (matching its id) from your server's swear-filter"
9989
)
10090
@option(name="id", description="What's the id of the keyword to remove (can be found in bold through /automod_view_custom_keywords", type=int)
101-
async def automod_remove_custom_keyword(self, ctx: ApplicationContext, id:int):
102-
loaded_config = automod_config[str(ctx.guild.id)]
91+
async def automod_remove_custom_keyword(self, ctx: ApplicationContext, id: int):
10392
try:
104-
data = loaded_config["swear_filter"]["keywords"]["custom"]
105-
data.pop(id-1)
106-
save()
93+
automod.swearfilter_removekeyword(ctx.guild.id, id)
10794
return await ctx.respond(f"Keyword (id: `{id}`) successfully removed from swear-filter configuration.")
10895
except IndexError: await ctx.respond("That keyword id doesn't exist. Please specify a valid id and try again.", ephemeral=True)
10996

framework/isobot/db/automod.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""The framework module library used for managing server automod configurations."""
2+
3+
# Imports
4+
import json
5+
6+
# Functions
7+
class Automod():
8+
"""Initializes the Automod database system."""
9+
def __init__(self):
10+
print("[framework/db/Automod] Automod db library initialized.")
11+
12+
def load(self) -> dict:
13+
"""Fetches and returns the latest data from the items database."""
14+
with open("database/automod.json", 'r', encoding="utf8") as f: db = json.load(f)
15+
return db
16+
17+
def save(self, data: dict) -> int:
18+
"""Dumps all cached data to your local machine."""
19+
with open("database/automod.json", 'w+', encoding="utf8") as f: json.dump(data, f, indent=4)
20+
return 0
21+
22+
def generate(self, server_id: int) -> int:
23+
"""Generates a new database key for the specified server/guild id in the automod database."""
24+
automod_config = self.load()
25+
if str(server_id) not in automod_config:
26+
automod_config[str(server_id)] = {
27+
"swear_filter": {
28+
"enabled": False,
29+
"keywords": {
30+
"use_default": True,
31+
"default": ["fuck", "shit", "pussy", "penis", "cock", "vagina", "sex", "moan", "bitch", "hoe", "nigga", "nigger", "xxx", "porn"],
32+
"custom": []
33+
}
34+
}
35+
}
36+
self.save(automod_config)
37+
38+
def fetch_config(self, server_id: int) -> dict:
39+
"""Fetches and returns the specified server's automod configuration.\n\nReturns in raw `dict` format."""
40+
automod_config = self.load()
41+
return automod_config[str(server_id)]
42+
43+
def swearfilter_enabled(self, server_id: int, value: bool) -> int:
44+
"""Sets a `bool` value to define whether the server's swear-filter is enabled or not."""
45+
automod_config = self.load()
46+
automod_config[str(server_id)]["swear_filter"]["enabled"] = value
47+
self.save(automod_config)
48+
return 0
49+
50+
def swearfilter_usedefaultkeywords(self, server_id: int, enabled: bool) -> int:
51+
"""Sets a `bool` value to define whether the server's swear-filter will use default keywords."""
52+
automod_config = self.load()
53+
automod_config[str(server_id)]["swear_filter"]["keywords"]["use_default"] = enabled
54+
self.save(automod_config)
55+
return 0
56+
57+
def swearfilter_addkeyword(self, server_id: int, keyword: str) -> int:
58+
"""Adds a new custom keyword for the server's automod configuration."""
59+
automod_config = self.load()
60+
automod_config[str(server_id)]["swear_filter"]["keywords"]["custom"].append(keyword)
61+
self.save(automod_config)
62+
return 0
63+
64+
def swearfilter_removekeyword(self, server_id: int, keyword_id: int) -> int:
65+
"""Removes a keyword (using id) from the server's automod configuration."""
66+
automod_config = self.load()
67+
data = automod_config[str(server_id)]["swear_filter"]["keywords"]["custom"]
68+
data.pop(id-1)
69+
self.save(automod_config)
70+
return 0

main.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from math import floor
1212
from random import randint
1313
from framework.isobot import currency, colors, settings
14-
from framework.isobot.db import levelling, items, userdata
14+
from framework.isobot.db import levelling, items, userdata, automod
1515
from discord import ApplicationContext, option
1616
from discord.ext import commands
1717
from cogs.isocoin import create_isocoin_key
@@ -57,6 +57,7 @@ def save():
5757
levelling = levelling.Levelling()
5858
items = items.Items()
5959
userdata = userdata.UserData()
60+
automod = automod.Automod()
6061

6162
# Theme Loader
6263
themes = False # True: enables themes; False: disables themes;
@@ -100,18 +101,7 @@ async def on_message(ctx):
100101
settings.generate(ctx.author.id)
101102
items.generate(ctx.author.id)
102103
levelling.generate(ctx.author.id)
103-
if str(ctx.guild.id) not in automod_config:
104-
automod_config[str(ctx.guild.id)] = {
105-
"swear_filter": {
106-
"enabled": False,
107-
"keywords": {
108-
"use_default": True,
109-
"default": ["fuck", "shit", "pussy", "penis", "cock", "vagina", "sex", "moan", "bitch", "hoe", "nigga", "nigger", "xxx", "porn"],
110-
"custom": []
111-
}
112-
}
113-
}
114-
save()
104+
automod.generate(ctx.guild.id)
115105
uList = list()
116106
if str(ctx.guild.id) in presence:
117107
for userid in presence[str(ctx.guild.id)].keys(): uList.append(userid)

0 commit comments

Comments
 (0)