-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
350 lines (300 loc) · 13.1 KB
/
bot.py
File metadata and controls
350 lines (300 loc) · 13.1 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
from typing import Dict, List, Union
import discord
import asyncio
import os
import exceptions
# TODO: Bot should verify that the proper numbers of arguments are added
# TODO: Bot should check .watchfiles to see if it's updated too
try:
with open(".env", "r") as file:
env_vars: Dict[str, str] = {}
for line in file.read().splitlines():
[key, value] = line.split("=")
env_vars[key] = value
except FileNotFoundError:
raise exceptions.EnvNotProvided("Provide .env file for BOT_TOKEN")
STUDENT_ID = env_vars.get("STUDENT_ID")
MENTOR_ID = env_vars.get("MENTOR_ID")
LOGGING_CHANNEL_ID = env_vars.get("LOGGING_CHANNEL_ID")
FILE_CHECK_INTERVAL = env_vars.get("FILE_CHECK_INTERVAL")
WATCHFILE_CHECK_INTERVAL = env_vars.get("WATCHFILE_CHECK_INTERVAL")
BOT_TOKEN = env_vars["BOT_TOKEN"]
if FILE_CHECK_INTERVAL is None:
FILE_CHECK_INTERVAL = 3
else:
FILE_CHECK_INTERVAL = int(FILE_CHECK_INTERVAL)
if WATCHFILE_CHECK_INTERVAL is None:
WATCHFILE_CHECK_INTERVAL = 7
else:
WATCHFILE_CHECK_INTERVAL = int(WATCHFILE_CHECK_INTERVAL)
MessageableChannel = Union[
discord.TextChannel,
discord.StageChannel,
discord.VoiceChannel,
discord.Thread,
discord.DMChannel,
discord.GroupChannel,
discord.PartialMessageable,
]
def prepare_message(filepath: str, extension: str, code: str) -> str:
if STUDENT_ID is None:
return f"## {filepath} Changed \n```{extension}\n{code}\n```\n"
return f"## {filepath} Changed \n```{extension}\n{code}\n```\n<@&{STUDENT_ID}>"
def escapeable_split(string: str, splitter: str = " ", include_splitter: bool = False):
split_list = []
word = ""
ESCAPE = "\\"
escaped = False
for letter in string:
if letter == ESCAPE:
escaped = True
continue
if letter == splitter and not escaped:
split_list.append(word)
word = ""
if not include_splitter:
continue
elif escaped:
escaped = False
if letter != splitter:
word += ESCAPE
word += letter
if word != "":
split_list.append(word)
return split_list
def has_file_updated(file, current_stamp):
new_stamp = os.stat(file).st_mtime
return new_stamp != current_stamp
async def send_denial(message: discord.Message):
await message.channel.send("Only mentors can watch for file changes")
class FileUpdateBot(discord.Client):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.background_tasks: List[asyncio.Task] = []
self.default_background_tasks: Dict[str, asyncio.Task] = {}
self.default_background_task_running: Dict[str, bool] = {}
self.logging_channel: None | MessageableChannel = None
async def log(self, message: str):
if self.logging_channel is None:
print(message)
else:
await self.logging_channel.send(message)
async def on_ready(self):
if LOGGING_CHANNEL_ID is not None:
logging_channel = await self.fetch_channel(int(LOGGING_CHANNEL_ID))
if not isinstance(logging_channel, MessageableChannel):
print(f"I cannot send message in {LOGGING_CHANNEL_ID}")
self.logging_channel = logging_channel
else:
print(
"LOGGING_CHANNEL_ID is not provided in .env, Logging will be done in terminal now."
)
if MENTOR_ID is None:
await self.log(
"## WARNING \n Mentor_id is not provided, program needs mentor id to verify the person who is commanding it, now anyone can command it, thus anyone can access your files"
)
try:
await self.watch_default_files()
except FileNotFoundError:
print("No default files to watch...")
print("Bot is ready...")
async def on_message(self, message):
if message.content.startswith("$"):
await self.handle_commands(message)
async def watch_file(self, filepath: str, channel: MessageableChannel):
await self.wait_until_ready()
stamp = os.stat(filepath).st_mtime
dot_index = filepath.find(".")
extension = filepath[dot_index + 1 :]
key = f"forum-watch|{channel.id}|{filepath}"
while not self.is_closed() and self.default_background_task_running[key]:
if has_file_updated(filepath, stamp):
stamp = os.stat(filepath).st_mtime
code = ""
with open(filepath, "r") as file:
code = file.read()
message = prepare_message(filepath, extension, code)
await channel.send(message)
await asyncio.sleep(FILE_CHECK_INTERVAL)
async def forum_watch_file(
self,
filepath,
channel: discord.ForumChannel,
) -> bool:
await self.wait_until_ready()
stamp = os.stat(filepath).st_mtime
dot_index = filepath.find(".")
extension = filepath[dot_index + 1 :]
file_thread: discord.Thread | None = None
for loop_thread in channel.threads:
if loop_thread.name == filepath:
file_thread = loop_thread
if file_thread is None:
code = ""
with open(filepath, "r") as file:
code = file.read()
forum_content = f"```{extension}\n{code}\n```\n Current code"
message = prepare_message(filepath, extension, code)
file_thread_with_message = await channel.create_thread(
name=filepath, content=forum_content
)
file_thread = file_thread_with_message.thread
await file_thread.send(message)
key = f"forum-watch|{channel.id}|{filepath}"
while not self.is_closed() and self.default_background_task_running[key]:
code = ""
if has_file_updated(filepath, stamp):
stamp = os.stat(filepath).st_mtime
with open(filepath, "r") as file:
code = file.read()
message = prepare_message(filepath, extension, code)
forum_content = f"```{extension}\n{code}\n```\n Current code"
[first_message] = [
message
async for message in file_thread.history(limit=1, oldest_first=True)
]
if first_message is None:
raise exceptions.StarterMessageNotFound("starter_message is None")
await first_message.edit(content=forum_content)
await file_thread.send(message)
await asyncio.sleep(FILE_CHECK_INTERVAL)
return True
async def handle_watchfile_command(
self, line: str | None = None, key: str | None = None
) -> None:
if line is not None:
split_command = escapeable_split(line)
elif key is not None:
split_command = escapeable_split(key, "|")
else:
raise exceptions.NeitherLineNorKeyProvided(
"Provide a line, or a key to handle_watchfile_command()"
)
command = split_command[0]
match command:
case "watch":
[channel_id, filepath] = split_command[1:]
if not os.path.exists(filepath):
await self.log(f"{filepath} does not exist")
return
channel = await self.fetch_channel(channel_id)
if not isinstance(channel, MessageableChannel):
await self.log(f"I cannot send message in <#{channel_id}>")
return
key = f"{command}|{channel_id}|{filepath}"
self.default_background_task_running[key] = True
self.default_background_tasks[key] = self.loop.create_task(
self.watch_file(filepath, channel)
)
await self.log(f"Watching {filepath} 🤖 in <#{channel_id}>")
case "forum-watch":
[channel_id, filepath] = split_command[1:]
if not os.path.exists(filepath):
await self.log(f"{filepath} does not exist")
return
channel = await self.fetch_channel(channel_id)
if not isinstance(channel, discord.ForumChannel):
await self.log(f"<#{channel_id}> is not a forum channel")
return
key = f"{command}|{channel_id}|{filepath}"
self.default_background_task_running[key] = True
self.default_background_tasks[key] = self.loop.create_task(
self.forum_watch_file(filepath, channel)
)
await self.log(f"Watching {filepath} 🤖 in <#{channel_id}>")
async def watch_default_files(self):
filepath = ".watchfiles"
while not os.path.exists(filepath) and not self.is_closed():
await asyncio.sleep(WATCHFILE_CHECK_INTERVAL)
commands = ""
with open(filepath, "r") as file:
commands = file.read().splitlines()
for command in commands:
await self.handle_watchfile_command(command)
stamp = os.stat(filepath).st_mtime
while not self.is_closed():
if has_file_updated(filepath, stamp):
stamp = os.stat(filepath).st_mtime
# key is command|channel_name|filename
# Conditions, if key in new tasks and running, keep running
# if key in new taks, but not running, then start running
# if new tasks does not exist in key, then add key and start running
# if key not exists in new tasks, then stop runnig
with open(filepath, "r") as file:
commands = file.read()
new_tasks: set[str] = {
command.replace(" ", "|") for command in commands.splitlines()
}
keys = set(self.default_background_task_running.keys())
for key in new_tasks.intersection(keys):
running = self.default_background_task_running[key]
if not running:
await self.handle_watchfile_command(key)
for key in new_tasks.difference(keys):
await self.handle_watchfile_command(key)
for key in keys.difference(new_tasks):
self.default_background_task_running[key] = False
await asyncio.sleep(WATCHFILE_CHECK_INTERVAL)
async def handle_commands(self, message: discord.Message):
isMentor = await self.find_is_mentor(message)
if not isMentor:
await send_denial(message)
return
content = message.content
split = escapeable_split(content)
match split[0]:
case "$hack":
filepath = split[1]
if not os.path.exists(filepath):
await message.channel.send(f"{filepath} does not exist")
return
self.background_tasks.append(
self.loop.create_task(self.watch_file(filepath, message.channel))
)
with open("hacker.webp", "rb") as file:
gif = discord.File(file)
await message.channel.send("# Got it", file=gif)
case "$watch":
filepath = split[1]
if not os.path.exists(filepath):
await message.channel.send(f"{filepath} does not exist")
return
self.background_tasks.append(
self.loop.create_task(self.watch_file(filepath, message.channel))
)
await message.channel.send(f"Watching {filepath} 🤖")
case "$forum-watch":
forum_id = split[1]
filepath = split[2]
if not os.path.exists(filepath):
await message.channel.send(f"{filepath} does not exist")
return
forum_channel = await self.fetch_channel(forum_id)
if not isinstance(forum_channel, discord.ForumChannel):
await message.channel.send(
"Channel provided is not a forum channel"
)
return
self.background_tasks.append(
self.loop.create_task(
self.forum_watch_file(filepath, forum_channel)
)
)
await message.channel.send(f"Watching {filepath} in <#{forum_id}> 🤖")
async def find_is_mentor(self, message: discord.Message):
if MENTOR_ID is None:
return True
isMentor = False
author = message.author
if not isinstance(author, discord.Member):
return False
for role in author.roles:
if role.id == int(MENTOR_ID):
isMentor = True
return isMentor
if __name__ == "__main__":
intents = discord.Intents.default()
intents.guilds = True
intents.message_content = True
client = FileUpdateBot(intents=intents)
client.run(BOT_TOKEN)