-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathbot.py
442 lines (382 loc) · 17 KB
/
bot.py
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
import logging
import time
import traceback
from datetime import datetime
from socket import timeout
import aiohttp
import discord
import pendulum
from celery.utils.time import rate
from discord import ApplicationContext, DiscordException
from discord.ext import commands, tasks
from kombu import Connection, Consumer, Queue
from kombu.utils.limits import TokenBucket
from redis import asyncio as aioredis
import django
import django.db
from django.conf import settings
from django.utils import timezone
from allianceauth import hooks
import aadiscordbot
from aadiscordbot import app_settings
from aadiscordbot.app_settings import (
DISCORD_BOT_ACCESS_DENIED_REACT, DISCORD_BOT_PREFIX,
)
from aadiscordbot.cogs.utils.exceptions import NotAuthenticated, NotManaged
from . import bot_tasks
description = """
AuthBot is watching...
"""
logger = logging.getLogger(__name__)
INVITE_URL = f"https://discord.com/api/oauth2/authorize?client_id={app_settings.DISCORD_APP_ID}&permissions=8&scope=bot%20applications.commands"
queuename = "aadiscordbot"
queue_keys = [f"{queuename}",
f"{queuename}\x06\x161",
f"{queuename}\x06\x162",
f"{queuename}\x06\x163",
f"{queuename}\x06\x164",
f"{queuename}\x06\x165",
f"{queuename}\x06\x166",
f"{queuename}\x06\x167",
f"{queuename}\x06\x168",
f"{queuename}\x06\x169"]
class PendingQueue:
def __init__(self):
self.data = []
def append(self, data):
self.data.append(data)
self.data.sort(key=lambda a: a[0])
def pop_next(self):
next_task = next(
(x for x in self.data if x[0] < timezone.now()), False)
if next_task:
del (self.data[self.data.index(next_task)])
return next_task
def outstanding(self):
return len(self.data)
class RateLimiter:
def __init__(self):
self.rate_buckets = {}
def bucket_for_task(self, task):
limit = rate(
app_settings.DISCORD_BOT_TASK_RATE_LIMITS.get(task, "100/s"))
return TokenBucket(limit, capacity=1)
def check_rate_limit(self, task):
if task not in self.rate_buckets:
self.rate_buckets[task] = self.bucket_for_task(task)
# logger.debug(f" {self.rate_buckets[task].capacity} {self.rate_buckets[task].fill_rate} {self.rate_buckets[task].expected_time()}")
return self.rate_buckets[task].can_consume()
def to_string(self):
"""
Print of Task Limiter Stats
"""
out = ["```"]
for t, d in self.rate_buckets.items():
out.append(
f"{t}\n Rate: {d.fill_rate:.2}\n TTL: {d.expected_time():.2}")
out.append("```")
return "\n".join(out)
class Statistics:
def __init__(self):
self.task_buckets = {}
self.start_time = timezone.now()
def add_task(self, task):
date = timezone.now().strftime("%Y/%m/%d")
if date not in self.task_buckets:
self.task_buckets[date] = {}
if task not in self.task_buckets[date]:
self.task_buckets[date][task] = 0
self.task_buckets[date][task] += 1
def reset(self):
self.task_buckets = {}
def to_string(self):
"""
Print of Run Task Stats
"""
gap = " "
hdr = " Task Count"
out = ["```", "Date"]
for d, ts in self.task_buckets.items():
out.append(f"{d}")
out.append(hdr)
for t, c in ts.items():
out.append(f" {t}{gap[len(str(t)):41]}{c}")
out.append("\n")
out.append("```")
return "\n".join(out)
class AuthBot(commands.Bot):
def __init__(self):
django.setup()
client_id = app_settings.DISCORD_APP_ID
intents = discord.Intents.default()
intents.members = True
intents.message_content = app_settings.DISCORD_BOT_MESSAGE_INTENT
super().__init__(
command_prefix=DISCORD_BOT_PREFIX,
description=description,
intents=intents,
)
print(f"Authbot Started with command prefix {DISCORD_BOT_PREFIX}")
self.redis = None
self.redis = self.loop.run_until_complete(aioredis.from_url(getattr(
settings, "BROKER_URL", "redis://localhost:6379/0"), encoding="utf-8", decode_responses=True))
print('redis pool started', self.redis)
self.client_id = client_id
self.session = aiohttp.ClientSession(loop=self.loop)
self.tasks = []
self.pending_tasks = PendingQueue()
self.rate_limits = RateLimiter()
self.statistics = Statistics()
self.message_connection = Connection(
getattr(settings, "BROKER_URL", 'redis://localhost:6379/0'))
queues = []
for que in queue_keys:
queues.append(Queue(que))
self.message_consumer = Consumer(self.message_connection, queues, callbacks=[
self.on_queue_message], accept=['json'])
self.cog_names_loaded = []
self.cog_names_failed = []
hooks_ = hooks.get_hooks("discord_cogs_hook")
for hook in hooks_:
for cog in hook():
try:
self.load_extension(cog)
self.cog_names_loaded.append(cog)
except Exception:
logger.exception(f"Failed to load cog {cog}")
self.cog_names_failed.append(cog)
def on_queue_message(self, body, message):
logger.debug(f'RECEIVED MESSAGE: {body!r}')
try:
task_headers = message.headers
eta = task_headers.get("eta", False)
_args = body[0]
_kwargs = body[1]
if 'aadiscordbot.tasks.' in task_headers["task"]:
task = task_headers["task"].replace("aadiscordbot.tasks.", '')
task_function = getattr(bot_tasks, task, False)
if task_function:
if eta:
logger.debug(f"ETA RECEIVED: {eta}")
eta_date = datetime.fromisoformat(eta)
if eta_date < timezone.now():
self.tasks.append((task_function, _args, _kwargs))
else:
self.pending_tasks.append(
(eta_date, (task_function, _args, _kwargs)))
else:
self.tasks.append((task_function, _args, _kwargs))
if not bot_tasks.run_tasks.is_running():
bot_tasks.run_tasks.start(self)
else:
logger.debug("No bot_task for that auth_task?")
else:
logger.debug("i got an invalid auth_task")
except Exception as e:
logger.error("Queue Consumer Failed")
logger.error(e, exc_info=1)
message.ack()
async def on_ready(self):
if not hasattr(self, "currentuptime"):
self.currentuptime = pendulum.now(tz="UTC")
if not hasattr(self, "statistics"):
self.statistics.start_time = timezone.now()
activity = discord.Activity(name="Everything!",
application_id=0,
type=discord.ActivityType.watching,
state="Monitoring",
details="Waiting for Shenanigans!",
emoji={"name": ":smiling_imp:"}
)
await self.change_presence(activity=activity)
self.poll_queue.start()
logger.info("Ready")
async def close(self):
# return tasks to queue
# recursive import
from aadiscordbot import tasks as celery_tasks
self.poll_queue.stop()
bot_tasks.run_tasks.stop()
recovered_messages = 0
lost_messages = 0
for task in self.tasks:
task_function = getattr(celery_tasks, task[0].__name__, False)
if task_function:
recovered_messages += 1
task_function.apply_async(args=task[1], kwargs=task[2])
else:
lost_messages += 1
for task in self.pending_tasks.data:
task_function = getattr(celery_tasks, task[1][0].__name__, False)
if task_function:
recovered_messages += 1
task_function.apply_async(
args=task[1][1], kwargs=task[1][2], eta=task[0])
else:
lost_messages += 1
if recovered_messages:
logger.info(
f"Returned {recovered_messages} message pending tasks to the celery queue")
if lost_messages:
logger.warning(
f"Lost {recovered_messages} message pending tasks while trying to return them to the celery queue")
await super().close()
async def process_commands(self, message):
ctx = await self.get_context(message)
if ctx.command is None:
return
django.db.close_old_connections()
await self.invoke(ctx)
django.db.close_old_connections()
async def on_interaction(self, interaction):
try:
django.db.close_old_connections()
await self.process_application_commands(interaction)
except Exception as e:
logger.error(f"Interaction Failed {e}", stack_info=True)
django.db.close_old_connections()
async def on_message(self, message):
if message.author.bot:
return
await self.process_commands(message)
async def sync_commands(self, *args, **kwargs):
try:
return await super(__class__, self).sync_commands(*args, **kwargs)
except discord.Forbidden as e:
logger.error(
"******************************************************")
logger.error("| AuthBot was Unable to Sync Slash Commands!!!!")
logger.error(
"| Please ensure your bot was invited to the server with the correct scopes")
logger.error("|")
logger.error("| To redo your scopes,")
logger.error("| 1. Refresh Scopes with this link:")
logger.error(f"| {INVITE_URL} ")
logger.error(
"| 2. Move the bots role to top of the roles tree if its not there already")
logger.error("| 3. Restart Bot")
logger.error(
"******************************************************")
logger.error(e)
@tasks.loop(seconds=1.0)
async def poll_queue(self):
message_avail = True
while message_avail:
try:
with self.message_consumer:
self.message_connection.drain_events(timeout=0.01)
except timeout:
# This is when there are no messages in the queue.
# logging.exception(e)
message_avail = False
next_task = self.pending_tasks.pop_next()
while next_task:
self.tasks.append(next_task[1])
if not bot_tasks.run_tasks.is_running():
bot_tasks.run_tasks.start(self)
next_task = self.pending_tasks.pop_next()
async def on_resumed(self):
print("Resumed...")
async def on_command_error(self, ctx, error):
if isinstance(error, commands.BadArgument):
logger.error(error)
await ctx.send(error)
elif isinstance(error, commands.MissingRequiredArgument):
logger.error(error)
await ctx.send(error)
elif isinstance(error, commands.NoPrivateMessage):
logger.error(error)
await ctx.send(error)
elif isinstance(error, commands.CommandInvokeError):
logger.error(error)
return await ctx.send(error)
elif isinstance(error, commands.BotMissingPermissions):
await ctx.send(
"Sorry, I don't have the required permissions to do that here:\n{}".format(
error.missing_permissions)
)
elif isinstance(error, commands.MissingPermissions):
await ctx.message.add_reaction(chr(DISCORD_BOT_ACCESS_DENIED_REACT))
await ctx.message.reply("Sorry, you do not have permission to do that here.")
elif isinstance(error, commands.NotOwner):
logger.error(error)
await ctx.send(error)
elif isinstance(error, commands.CommandOnCooldown):
await ctx.message.add_reaction(chr(0x274C))
elif isinstance(error, commands.CheckFailure):
await ctx.send(error)
else:
logger.error(f"Unknown Error {error}", exc_info=True)
await ctx.send("Something Went Wrong, Please try again Later.",)
django.db.close_old_connections()
async def send_resp(self, ctx, exp):
try:
await ctx.send_response(exp, ephemeral=True)
except RuntimeError:
await ctx.send_followup(exp, ephemeral=True)
async def on_application_command_error(self, context: ApplicationContext, exception: DiscordException) -> None:
if isinstance(exception, commands.CheckFailure):
await self.send_resp(context, exception)
elif isinstance(exception, commands.MissingPermissions):
await self.send_resp(context, exception)
elif isinstance(exception, NotAuthenticated):
await self.send_resp(context, exception)
elif isinstance(exception, NotManaged):
await self.send_resp(context, exception)
else: # Catch everything, and close out the interactions gracefully.
logger.error(f"Unknown Error {exception}")
logger.error("\n".join(traceback.format_tb(
exception.original.__traceback__)))
if app_settings.DISCORD_BOT_SEND_FAILURE_MESSAGES and app_settings.DISCORD_BOT_FAILURE_MESSAGES_CHANNEL:
message = [f"`{context.command}` failed for `{context.author}`\n",
f"**Exception:** ```{exception}```",
f"\n**Interaction:** ```{str(context.interaction.data).replace('`', ' `')}```",
"\n**Trace:**```"]
message += traceback.format_tb(
exception.original.__traceback__)
message.append("\n```")
from . import tasks
tasks.send_message(message="\n".join(message),
channel_id=app_settings.DISCORD_BOT_FAILURE_MESSAGES_CHANNEL)
await context.respond("Something Went Wrong, Please try again Later.", ephemeral=True)
django.db.close_old_connections()
def run(self):
# self.load_extension("aadiscordbot.slash.admin")
try:
logger.info(
"******************************************************")
logger.info(" ## Alliance Auth 'AuthBot'")
logger.info(
f" #### Version : {aadiscordbot.__version__}")
logger.info(
f" ####### Branch : {aadiscordbot.__branch__}")
logger.info(
f" ######### Message Intents : {app_settings.DISCORD_BOT_MESSAGE_INTENT}")
logger.info(
f" ######## (( Prefix : {app_settings.DISCORD_BOT_PREFIX}")
logger.info(
f" ###### (((((( Bot Join Link : {INVITE_URL}")
logger.info(" ### (((( Starting up...")
logger.info(" ## ((")
logger.info(" [Cogs Loaded]")
for c in self.cog_names_loaded:
logger.info(f" - {c}")
if len(self.cog_names_failed):
logger.info(" [Cog Failures]")
for c in self.cog_names_failed:
logger.info(f" - {c}")
logger.info(
"******************************************************")
super().run(app_settings.DISCORD_BOT_TOKEN, reconnect=True)
except discord.PrivilegedIntentsRequired as e:
logger.error("Unable to start bot with Messages Intent! Going to Sleep for 2min. "
"Please enable the Message Intent for your bot. "
"https://support-dev.discord.com/hc/en-us/articles/4404772028055"
f"{e}", exc_info=False)
logger.info("If you wish to run without the Message Intent disable it in the local.py. "
"DISCORD_BOT_MESSAGE_INTENT=False", exc_info=False)
time.sleep(120)
except Exception as e:
logger.error("Unable to start bot! going to sleep for 2 min. "
f"{e}", exc_info=True)
time.sleep(120)