|
| 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 |
0 commit comments