-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
230 lines (182 loc) · 7.82 KB
/
main.py
File metadata and controls
230 lines (182 loc) · 7.82 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
import asyncio
import time
import aiohttp
import tomllib
import aiofiles
import json
import random
import signal
import shutil
from pathlib import Path
from utils.logging import create_logger
from utils.time import format_duration
logger = create_logger("Forget")
Path("cache/").mkdir(exist_ok=True)
settings_path = Path("settings.toml")
if not settings_path.exists():
shutil.copy(Path("settings.default.toml"), settings_path)
with open(settings_path) as f:
config = tomllib.loads(f.read())
if not config["token"]:
logger.critical("Please set your Discord token in settings.toml")
exit()
if not config["channel_id"]:
logger.critical("Please set the channel id in settings.toml")
exit()
shutdown_now = False
async def delete_message(session: aiohttp.ClientSession, message_id):
async with session.delete(
f"https://discord.com/api/v9/channels/{config["channel_id"]}/messages/{message_id}",
) as res:
if not res.ok:
if res.status == 429:
retry_after = float(res.headers.get("retry-after"))
human_delay = random.randrange(25, 100) / 100
logger.warning(f"Rate-limited, Discord has requested we wait {retry_after} second(s). Adding human delay of {human_delay} second(s).")
await asyncio.sleep(retry_after + human_delay)
await delete_message(session, message_id)
return
if res.status == 404:
logger.warning(f"Message {message_id} not found, it must already be deleted.")
return
logger.critical(f"Failed to delete message {message_id}, quitting early. (status {res.status})")
exit()
async def save_cache(cache, path):
try:
async with aiofiles.open(path, "w") as f:
await f.write(json.dumps(cache, indent=4))
except Exception:
logger.critical("SAVE WAS INTERRUPTED, DATA MAY BE LOST :(")
async def main(session: aiohttp.ClientSession):
async with session.get("https://discord.com/api/v9/users/@me") as res:
if not res.ok:
logger.critical("Failed to request information about current user.")
return
current_user: dict = await res.json()
params = {
"limit": 50,
}
cache = {}
lock_file = Path(f"cache/{config["channel_id"]}.json.lock")
if lock_file.exists():
logger.critical("Lock file is present, this usually means another instance is running already.")
logger.critical(f"If you are SURE there is not another instance running, please delete {lock_file.absolute()}")
return
lock_file.touch()
cache_file = Path(f"cache/{config["channel_id"]}.json")
if cache_file.exists():
logger.info("Loading previous state from cache file..")
async with aiofiles.open(cache_file) as f:
cache = json.loads(await f.read())
params["before"] = cache["last_before"]
logger.info(f"Loaded previous save state from cache, at message {params["before"]}")
start = time.time()
while not shutdown_now:
try:
async with session.get(
f"https://discord.com/api/v9/channels/{config["channel_id"]}/messages",
params=params,
) as res:
if not res.ok:
logger.critical(f"Failed to query messages with error {res.status}")
return
messages = await res.json()
if not messages:
logger.info("Empty message list, assuming we're done. Moving to deletion step.")
break
if not cache.get("messages"):
cache["messages"] = []
cache["messages"].extend(messages)
cache["last_before"] = messages[-1]["id"]
params["before"] = messages[-1]["id"]
except Exception as e:
logger.exception(e)
return cache, cache_file
logger.info(f"Discovered {len(cache["messages"])} message(s), {round(time.time() - start)} second(s) elapsed. Saving..")
# await save_cache(cache, cache_file)
await asyncio.sleep(config["discover_delay"])
top_to_bottom_deletion = True
logger.info("User input required!!")
answer = input("Delete messages from top to bottom? [Y/n]: ")
if answer.lower() not in ("", "y", "yes", "n", "no"):
logger.critical("Response not clear, quitting.")
return
if answer.lower() in ("n", "no"):
top_to_bottom_deletion = False
if not cache.get("deleted_messages"):
cache["deleted_messages"] = []
valid_message_types = (0, 19)
messages = list(cache["messages"].__reversed__() if top_to_bottom_deletion else cache["mesages"])
messages_to_delete = [
msg for msg in messages
if (
msg["author"]["id"] == current_user["id"]
and msg["type"] in valid_message_types
)
]
length = len(messages_to_delete) + len(cache["deleted_messages"])
avg_message_delete_times = []
for i, message in enumerate(messages_to_delete):
if shutdown_now:
break
if message["author"]["id"] != current_user["id"]:
continue
if message["type"] not in valid_message_types:
continue
average_start = time.time()
try:
await delete_message(session, message["id"])
except Exception as e:
logger.exception(e)
return cache, cache_file
cache["messages"].remove(message)
cache["deleted_messages"].append(message)
# await save_cache(cache, cache_file)
avg_message_delete_times.append((time.time() - average_start) + config["delete_delay"])
if len(avg_message_delete_times) > 20:
avg_message_delete_times.pop(0)
average_delete_time = sum(avg_message_delete_times) / len(avg_message_delete_times)
remaining_messages = length - len(cache["deleted_messages"])
estimated_remaining_time = average_delete_time * remaining_messages
logger.info(f"Deleted {len(cache["deleted_messages"])} of {length}.. (estimated {format_duration(estimated_remaining_time)} left) (avg {round(average_delete_time, 2)}s per delete)")
await asyncio.sleep(config["delete_delay"])
if not shutdown_now:
logger.info("Finished. All of your messages are now deleted!")
logger.info(f"To restart this process, delete cache/{config['channel_id']}.json and re-run this script!")
else:
logger.info("Terminated early due to a termination signal being fired.")
lock_file.unlink()
return cache, cache_file
async def main_wrapper():
session = aiohttp.ClientSession(
headers={
"Authorization": config["token"],
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) discord/0.0.102 Chrome/134.0.6998.205 Electron/35.3.0 Safari/537.36"
},
timeout=aiohttp.ClientTimeout(20.0)
)
cache = {}
cache_file = None
try:
res = await main(session)
if res:
cache, cache_file = res
except KeyboardInterrupt:
logger.warning("Handling Ctrl+C gracefully..")
finally:
if cache and cache_file:
logger.info("Saving...")
await save_cache(cache, cache_file)
lock_file = Path(f"cache/{config["channel_id"]}.json.lock")
if lock_file.exists():
lock_file.unlink()
await session.close()
def handle_shutdown(*_):
logger.warning("Received closed signal!")
logger.warning("Please wait for the next deletion task to complete.")
global shutdown_now
shutdown_now = True
if __name__ == "__main__":
signal.signal(signal.SIGINT, handle_shutdown) # ctrl+c
signal.signal(signal.SIGTERM, handle_shutdown) # systemd stop
asyncio.run(main_wrapper())