-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
478 lines (405 loc) · 16.2 KB
/
main.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
import interactions
from interactions import *
from interactions.models import *
from interactions.api.events.discord import MessageCreate
import os
import datetime
import time
from dotenv import load_dotenv
import matplotlib.pyplot as plt
from io import BytesIO
from mongo import get_week, write_message, get_day, get_month, update_auto_update_message, get_auto_update_message
from datetime import datetime, timedelta
from motor.motor_asyncio import AsyncIOMotorClient
from pandas import DataFrame
import pandas as pd
import numpy as np
load_dotenv()
username = os.environ["MONGO_USERNAME"]
password = os.environ["MONGO_PASSWORD"]
host = os.environ["MONGO_HOST"]
conn_str = f"mongodb+srv://{username}:{password}@{host}/?retryWrites=true&w=majority"
db = AsyncIOMotorClient(conn_str)
from keep_alive import start as keep_alive
keep_alive()
bot_intents: Intents = Intents.DEFAULT | Intents.GUILD_MEMBERS | Intents.MESSAGE_CONTENT
bot = interactions.Client(
sync_interactions=True, intents=bot_intents, send_command_tracebacks=False
)
meloania_id = 970525263211397171
tyler_id = 966090001668534394
@Task.create(TimeTrigger(hour=0, minute=0))
async def update_message():
server = "meloania"
if (server == "meloania"):
force_update = Button(style=ButtonStyle.BLUE, custom_id="force_update", emoji="🔄")
message = await get_auto_update_message(db, meloania_id)
meloania_auto_update_channel = bot.get_channel(1128402590925865121)
try:
message = await meloania_auto_update_channel.fetch_message(message)
except:
message = await meloania_auto_update_channel.send("No message found, please run /force_update to create one")
return
await message.edit(content="Updating...")
data = await get_week(db, "meloania")
buf = gen_graph(
data,
"Weekly Activity",
"Date",
"Messages",
"red",
"o",
"-",
)
priv_message = await bot.owner.send(file=File(file=buf, file_name="figure.png"))
url = priv_message.attachments[0].url
embed = Embed(
title="Weekly Activity", description="Weekly activity on the server", color=0xFFFFFF
)
embed.set_image(url=url)
await message.edit(content="", embed=embed, components=[force_update])
@listen()
async def on_startup():
print(f"{bot.user} has connected to Discord!")
global startup
startup = time.mktime(datetime.utcnow().timetuple())
# update_message.start(db=db, server="meloania")
def gen_graph(data, title, x_label, y_label, color, marker, linestyle, line_width=1):
data.plot(color=color, marker=marker, linestyle=linestyle, linewidth=line_width)
plt.grid(linewidth=0.5, color="black", axis="y")
plt.title(title)
plt.xlabel(x_label)
plt.ylabel(y_label)
# set the max y value to be +1 of the max value
y_max = plt.ylim()[1]
plt.ylim(0, y_max + 1, y_max / 5)
# set the points to not touch any of the edges
plt.margins(x=0.1, y=0.1)
buf = BytesIO()
plt.savefig(buf)
buf.seek(0)
return buf
@slash_command(name="ping", description="check the bots status")
async def ping(ctx):
btn1 = Button(style=ButtonStyle.RED, custom_id="delete", emoji="🗑️")
message = await ctx.send(
f"""
pong! 🏓
latency: `{round(bot.latency * 1000)}ms`
startup: <t:{round(startup)}:R>
""",
components=[btn1],
)
@slash_command(
name="weekly", description="get the weekly acivity of the server", dm_permission=False
)
async def weekly(ctx):
btn1 = Button(style=ButtonStyle.RED, custom_id="delete", emoji="🗑️")
message = await ctx.send("Generating graph...", components=[btn1])
if ctx.channel.guild.id == meloania_id:
data = await get_week(db, "meloania")
elif ctx.channel.guild.id == tyler_id:
data = await get_week(db, "tyler")
buf = gen_graph(
data,
"Weekly Activity",
"Date",
"Messages",
"red",
"o",
"-",
)
priv_message = await bot.owner.send(file=File(file=buf, file_name="figure.png"))
url = priv_message.attachments[0].url
embed = Embed(
title="Weekly Activity", description="Weekly activity on the server", color=0xFFFFFF
)
embed.set_image(url=url)
await message.edit(content="", embed=embed, components=[btn1])
@slash_command(name="day", description="get the activity in a set date", dm_permission=False)
@slash_option(name="date", description="yyyy-mm-dd", required=True, opt_type=OptionType.STRING)
async def daily(ctx, date: str):
btn1 = Button(style=ButtonStyle.RED, custom_id="delete", emoji="🗑️")
try:
date = datetime.strptime(date, "%Y-%m-%d")
except ValueError:
await ctx.send("Invalid date, please use the format `yyyy-mm-dd`", components=[btn1])
return
message = await ctx.send("Generating graph...", components=[btn1])
if ctx.channel.guild.id == meloania_id:
data = await get_day(db, "meloania", date)
elif ctx.channel.guild.id == tyler_id:
data = await get_day(db, "tyler", date)
buf = gen_graph(
data,
"Daily Activity",
"Time",
"Messages",
"red",
"o",
"-",
)
priv_message = await bot.owner.send(file=File(file=buf, file_name="figure.png"))
url = priv_message.attachments[0].url
embed = Embed(
title=date.strftime("%Y-%m-%d"),
description="Activity on the server on this date",
color=0xFFFFFF,
)
embed.set_image(url=url)
await message.edit(content="", embed=embed, components=[btn1])
@slash_command(name="force_update", description="Force update the automatic stats message", dm_permission=False, scopes=[970525263211397171])
async def force_update(ctx):
if ctx.author.has_permission(Permissions.ADMINISTRATOR) or ctx.author == bot.owner:
force_update = Button(style=ButtonStyle.BLUE, custom_id="force_update", emoji="🔄")
meloania_auto_update_channel = bot.get_channel(1128402590925865121)
message = await get_auto_update_message(db, "meloania")
print(message)
if await meloania_auto_update_channel.fetch_message(message) == None:
message = await meloania_auto_update_channel.send("No message found, creating a new one...")
await update_auto_update_message(db, "meloania", message.id)
else:
message = await meloania_auto_update_channel.fetch_message(message)
client_msg = await ctx.send(content="Updating...")
if ctx.channel.guild.id == meloania_id:
data = await get_week(db, "meloania")
elif ctx.channel.guild.id == tyler_id:
data = await get_week(db, "tyler")
buf = gen_graph(
data,
"Weekly Activity",
"Date",
"Messages",
"red",
"o",
"-",
)
priv_message = await bot.owner.send(file=File(file=buf, file_name="figure.png"))
url = priv_message.attachments[0].url
embed = Embed(
title="Weekly Activity", description="Weekly activity on the server", color=0xFFFFFF
)
embed.set_image(url=url)
await message.edit(content="", embed=embed, components=[force_update])
await client_msg.edit(content="Succesfully updated the automatic stats message.")
else:
await ctx.send("Sorry, can't let you do that.", ephemeral=True)
return
@listen()
async def on_message(ctx: MessageCreate):
message = ctx.message
if message.author == bot.user:
return
elif message.author.bot:
return
elif message.channel.type == ChannelType.DM:
return
else:
attachment_urls = []
if message.channel.guild.id == meloania_id:
server = "meloania"
elif message.channel.guild.id == tyler_id:
server = "tyler"
if message.attachments != []:
for attachment in message.attachments:
attachment_urls.append(attachment.url)
await write_message(
db,
message.id,
message.channel.id,
message.content,
attachment_urls,
message.author.id,
message.timestamp,
(message.timestamp).strftime("%Y-%m-%d"),
server,
)
@slash_command(name="month", description="get the activity in a month", dm_permission=False)
@slash_option(name="month", description="yyyy-mm", required=True, opt_type=OptionType.STRING)
async def monthly(ctx, month: str):
btn1 = Button(style=ButtonStyle.RED, custom_id="delete", emoji="🗑️")
try:
month = datetime.strptime(month, "%Y-%m")
except ValueError:
await ctx.send("Invalid month, please use the format `yyyy-mm`", components=[btn1])
return
message = await ctx.send("Generating graph...", components=[btn1])
data = await get_month(db, "meloania", month)
buf = gen_graph(
data,
"Monthly Activity",
"Date",
"Messages",
"red",
"o",
"-",
)
priv_message = await bot.owner.send(file=File(file=buf, file_name="figure.png"))
url = priv_message.attachments[0].url
embed = Embed(
title=month.strftime("%Y-%m"),
description="Activity of the serveer on this month",
color=0xFFFFFF,
)
embed.set_image(url=url)
await message.edit(content="", embed=embed, components=[btn1])
@slash_command(name="generategraph", description="Generate a graph")
@slash_option(
name="title",
description="Set the title of the graph",
required=True,
opt_type=OptionType.STRING,
)
@slash_option(name="xvalue", description="Set the title of the X value", required=True, opt_type=OptionType.STRING)
@slash_option(name="yvalue", description="Set the title of the Y value", required=True, opt_type=OptionType.STRING)
@slash_option(
name="color",
description="Set the color of the graph",
required=True,
opt_type=OptionType.STRING,
choices=[
SlashCommandChoice(name="red", value="red"),
SlashCommandChoice(name="green", value="green"),
SlashCommandChoice(name="blue", value="blue"),
SlashCommandChoice(name="yellow", value="yellow"),
SlashCommandChoice(name="black", value="black"),
SlashCommandChoice(name="white", value="white"),
SlashCommandChoice(name="purple", value="purple"),
SlashCommandChoice(name="orange", value="orange"),
SlashCommandChoice(name="pink", value="pink"),
SlashCommandChoice(name="brown", value="brown"),
SlashCommandChoice(name="gray", value="gray"),
SlashCommandChoice(name="cyan", value="cyan"),
],
)
@slash_option(
name="marker",
description="Set the marker of the graph",
required=True,
opt_type=OptionType.STRING,
choices=[
SlashCommandChoice(name="o", value="o"),
SlashCommandChoice(name="*", value="*"),
SlashCommandChoice(name=".", value="."),
SlashCommandChoice(name="+", value="+"),
SlashCommandChoice(name="x", value="x"),
SlashCommandChoice(name="s", value="s"),
SlashCommandChoice(name="d", value="d"),
],
)
@slash_option(name="line", description="Set the line of the graph", required=True, opt_type=OptionType.STRING, choices=[
SlashCommandChoice(name="-", value="-"),
SlashCommandChoice(name="--", value="--"),
SlashCommandChoice(name="-.", value="-."),
SlashCommandChoice(name=":", value=":"),
SlashCommandChoice(name="steps", value="steps"),
SlashCommandChoice(name="None", value="None"),
])
@slash_option(name="line_width", description="Set the line width of the graph", required=True, opt_type=OptionType.INTEGER)
# @slash_option(name="graph_type", description="Set the type of the graph", required=True, opt_type=OptionType.STRING, choices=[
# SlashCommandChoice(name="line", value="line"),
# SlashCommandChoice(name="bar", value="bar"),
# ])
async def generate_plot(
ctx, title: str, xvalue: str, yvalue: str, color: str, marker: str, line: str, line_width:str
):
btn1 = Button(style=ButtonStyle.RED, custom_id="delete", emoji="🗑️")
# make data
x = 0
y = 0
modal = Modal(
ShortText(label="X values",required=True, placeholder="First opt, second opt, ...", custom_id="xvalues"),
ShortText(label="Y values",required=True, placeholder="(must be integer) 0, 1, 2, ...", custom_id="yvalues"),
title="Input graph data")
await ctx.send_modal(modal=modal)
modal_ctx: ModalContext = await ctx.bot.wait_for_modal(modal)
xval = modal_ctx.responses["xvalues"]
yval = modal_ctx.responses["yvalues"]
# generate a pandas dataframe from the xval and yval
xval = xval.split(",")
yval = yval.split(",")
if xval == "":
xval = "xval"
if yval == "":
yval = "yval"
# check if an xval has a yval
if len(xval) != len(yval):
await modal_ctx.send("Invalid X and Y values, there must be a Y value for each X value")
return
try:
yval = [int(i) for i in yval]
except ValueError:
await modal_ctx.send("Invalid Y values, must be integers")
return
try:
xval = [int(i) for i in xval]
except ValueError:
pass
# generate a pandas dataframe from the xval and yval that will look like
# xval : yval
data = pd.DataFrame({"xval": xval, "yval": yval})
data.set_index("xval", inplace=True)
print(data)
buf = gen_graph(
data,
title,
xvalue,
yvalue,
color,
marker,
line,
line_width,
)
priv_message = await bot.owner.send(file=File(file=buf, file_name="figure.png"))
url = priv_message.attachments[0].url
embed = Embed(title="Custom graph", description=f"Custom generated graph by {ctx.author.mention}", color=0xFFFFFF)
embed.set_image(url=url)
await modal_ctx.send(content=f"{ctx.author.mention}, heres your graph!",embed=embed, components=[btn1])
@listen()
async def on_component(ctx: ComponentContext):
event = ctx.ctx
if event.custom_id == "delete":
if (
event.message.interaction._user_id == event.author.id
or event.author.has_permission(Permissions.MANAGE_MESSAGES) == True
):
await event.message.delete()
else:
await event.send("Not your interaction.", ephemeral=True)
if event.custom_id == "force_update":
if event.author.has_permission(Permissions.ADMINISTRATOR) or event.author == bot.owner:
btn1 = Button(style=ButtonStyle.RED, custom_id="delete", emoji="🗑️")
force_update = Button(style=ButtonStyle.BLUE, custom_id="force_update", emoji="🔄")
meloania_auto_update_channel = bot.get_channel(1128402590925865121)
message = await get_auto_update_message(db, "meloania")
print(message)
if await meloania_auto_update_channel.fetch_message(message) == None:
message = await meloania_auto_update_channel.send("No message found, creating a new one...")
await update_auto_update_message(db, "meloania", message.id)
else:
message = await meloania_auto_update_channel.fetch_message(message)
if event.channel.guild.id == meloania_id:
data = await get_week(db, "meloania")
elif event.channel.guild.id == tyler_id:
data = await get_week(db, "tyler")
buf = gen_graph(
data,
"Weekly Activity",
"Date",
"Messages",
"red",
"o",
"-",
)
priv_message = await bot.owner.send(file=File(file=buf, file_name="figure.png"))
url = priv_message.attachments[0].url
embed = Embed(
title="Weekly Activity", description="Weekly activity on the server", color=0xFFFFFF
)
embed.set_image(url=url)
await message.edit(content="", embed=embed, components=[force_update])
else:
await event.send("Sorry, can't let you do that.", ephemeral=True)
return
secret_token = os.environ["TOKEN"]
bot.start(secret_token)