-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconfiguration.py
More file actions
152 lines (129 loc) · 5.8 KB
/
Copy pathconfiguration.py
File metadata and controls
152 lines (129 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import logging
import sys
import toml
from pathlib import Path
logger = logging.getLogger(__name__)
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class Config(metaclass=Singleton):
def __init__(self):
# Configuration file
logger.debug("Config __init__")
config = None
with open("config.toml") as f:
config = toml.loads(f.read())
if not config:
logger.error("Failed to load the config")
sys.exit(-1)
try:
self.TOKEN = config["bot"]["token"]
self.BOT_ID = config["bot"]["id"]
self.LOG_FILE = config["bot"]["log_file"]
self.MOD_MAIN = config["moderation"]["channel_id"]
self.MOD_ROLE = config["moderation"]["role"]
self.MUTED_ROLE = config["moderation"]["muted_role"]
self.LOG_MOD_FILE = config["moderation"]["log_file"]
self.GUILD = config["server"]["guild"]
self.CHANNELS = config["channels"]
self.FLOOD_LIMIT = 3
self.MENTIONS_LIMIT = 3
# Minimum number of images in a single message, and the time
# window (seconds) in which the same author posting such a
# message in 2+ different channels is treated as a spam burst.
self.IMAGE_ATTACHMENT_LIMIT = 2
self.IMAGE_BURST_WINDOW = 60 * 5
# Data-retention policy (see comandos/retencion.py): personal-data
# logs older than this are deleted daily.
self.RETENTION_DAYS = 30
except KeyError:
logger.error(
"Error while reading the configuration file. "
"Make sure it contains all the required field"
)
sys.exit(-1)
# Public URL to TERMS.md (the bot's terms of use / data policy),
# surfaced via the %terminos command and (separately, outside this
# repo) the Discord Developer Portal's privacy-policy field.
# Optional/`.get()`-based on purpose: older configs without a
# [links] section shouldn't fail to start over this.
self.TERMS_URL = config.get("links", {}).get(
"terms_url", "https://raw.githubusercontent.com/python-discord-es/bot/main/TERMS.md"
)
self.setup_log_files()
def setup_log_files(self):
self.log_file = Path(self.LOG_FILE)
self.log_mod_file = Path(self.LOG_MOD_FILE)
self.log_spam_file = Path("logs/spam_log.csv")
self.log_image_spam_file = Path("logs/image_spam_log.csv")
self.log_main_file = Path("logs/main_log.csv")
# create an '_accepted' file based on the moderation log file
LOG_MOD_ACCEPTED_FILE = f"logs/{self.log_mod_file.stem}_accepted{self.log_mod_file.suffix}"
# create an '_rejected' file based on the moderation log file
LOG_MOD_REJECTED_FILE = f"logs/{self.log_mod_file.stem}_rejected{self.log_mod_file.suffix}"
self.log_accepted_file = Path(LOG_MOD_ACCEPTED_FILE)
self.log_rejected_file = Path(LOG_MOD_REJECTED_FILE)
# Audit trail for right-to-erasure requests (comandos/retencion.py).
# Only ever holds a user id, who requested it, and a row count - no
# personal content - so it's safe to keep indefinitely as evidence
# a request was honored.
self.log_gdpr_file = Path("logs/gdpr_erasure_log.csv")
# Audit trail for right-of-access/export requests (comandos/exportar.py).
# Same shape/rationale as log_gdpr_file above: id, requester and a
# count only, never the exported content itself.
self.log_gdpr_access_file = Path("logs/gdpr_access_log.csv")
# Checking files
self.check_create_file(
self.log_file, "date;command;message_id;channel;author_id;author;message\n"
)
self.check_create_file(
self.log_mod_file, "date;message_id;channel;author_id;author;message\n"
)
self.check_create_file(
self.log_accepted_file,
"date;message_id;channel;author_id;author;message;moderator\n",
)
self.check_create_file(
self.log_rejected_file,
"date;message_id;channel;author_id;author;message;moderator;reason\n",
)
self.check_create_file(self.log_spam_file, "\n")
self.check_create_file(self.log_image_spam_file, "\n")
self.check_create_file(
self.log_main_file,
"date;command;message_id;channel;author_id;author;message\n",
)
self.check_create_file(
self.log_gdpr_file,
"date;user_id;requested_by;requested_by_id;total_removed\n",
)
self.check_create_file(
self.log_gdpr_access_file,
"date;user_id;requested_by;requested_by_id;total_exported\n",
)
def get_spam_messages(self):
# Adding spam messages
d = set()
with open(self.log_spam_file) as f:
for line in f.readlines():
logger.debug("Loaded known spam message: %r", line.strip())
d.add(line.strip())
logger.debug("get_spam_messages: %s", d)
return d
def get_spam_image_hashes(self):
# Adding known scam image hashes (sha256 of the raw attachment bytes)
d = set()
with open(self.log_image_spam_file) as f:
for line in f.readlines():
line = line.strip()
if line:
d.add(line)
logger.debug("get_spam_image_hashes: %s", d)
return d
def check_create_file(self, fname: Path, msg: str) -> None:
if not fname.is_file():
with open(str(fname), "w") as f:
f.write(msg)