forked from AndersSpear/fartbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fartbot.py
360 lines (311 loc) · 14.3 KB
/
fartbot.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
import discord
import aiosqlite
import config
import asyncio
from datetime import date, datetime, timedelta
from discord.ext import commands
from discord import app_commands
from discord.utils import get
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
intents.messages = True
bot = commands.Bot(command_prefix="/", intents=intents)
##### Events #####
@bot.event
async def on_ready():
# db = await aiosqlite.connect("fartstreak.db")
asyncio.create_task(reset_roles())
async with aiosqlite.connect(config.dbpath) as db:
await db.execute(
"""CREATE TABLE IF NOT EXISTS fartstreak (
userid, INTEGER PRIMARY KEY,
longeststreak_start_date TEXT,
longeststreak_end_date TEXT,
longeststreak_length INTEGER,
currentstreak_start_date TEXT,
currentstreak_end_date TEXT,
currentstreak_length INTEGER,
pfp TEXT,
name TEXT,
total INTEGER
) WITHOUT ROWID;"""
)
await db.commit()
print(f"Logged on as {bot.user}!")
await bot.tree.sync(guild=discord.Object(id=config.guild))
@bot.event
async def on_member_join(member):
# print(f'{member} has joined the server')
await member.edit(nick="fart club")
@bot.event
async def on_raw_message_edit(payload):
# print("EDIT!!!")
try:
channel = await bot.fetch_channel(config.channel)
# print(channel)
message = await channel.fetch_message(payload.message_id)
# print(message)
await message.delete()
# print("success")
except:
pass
@bot.event
async def on_message(message):
print(f"Message from {message.author}: {message.content}")
if message.channel.id == config.channel:
# print(message.author),
if type(message.author) != discord.Member:
return
if (
message.author.get_role(config.poo_clan) != None
and message.content == "poo clan"
):
return
if (
message.content != "fart club"
or message.stickers != []
or message.author.get_role(config.poo_clan) != None
):
await message.delete()
else:
# add role for general chat. the 0 is a placeholder, replace with the ID of the correct role
await message.author.add_roles(
get(message.author.guild.roles, id=config.general)
)
async with aiosqlite.connect(config.dbpath) as db:
async with db.execute(
f"SELECT * FROM fartstreak WHERE userid = {message.author.id};"
) as cursor:
row = await cursor.fetchone()
today = date.today()
if row == None:
print(
f"INSERT INTO fartstreak (userid, longeststreak_start_date, longeststreak_end_date, longeststreak_length, currentstreak_start_date, currentstreak_end_date, currentstreak_length, pfp, name, total) VALUES ({message.author.id}, '{today}', '{today}', 1, '{today}', '{today}', 1, '{message.author.display_avatar.url}', '{message.author.name}', 1);"
)
await db.execute(
f"INSERT INTO fartstreak (userid, longeststreak_start_date, longeststreak_end_date, longeststreak_length, currentstreak_start_date, currentstreak_end_date, currentstreak_length, pfp, name, total) VALUES ({message.author.id}, '{today}', '{today}', 1, '{today}', '{today}', 1, '{message.author.display_avatar.url}', '{message.author.name}', 1);"
)
await db.commit()
else:
# print(f'row: {row}')
# print(f"streak end: {row[4]}\nyesterday date: {today - timedelta(days = 1)}")
if row[4] == str(today - timedelta(days=1)):
# print('last message was yesterday')
if row[6] + 1 > row[5]:
await db.execute(
f"""UPDATE fartstreak
SET longeststreak_start_date = '{row[3]}',
longeststreak_end_date = '{today}',
longeststreak_length = {row[6] + 1},
currentstreak_end_date = '{today}',
currentstreak_length = {row[6] + 1},
total = {row[9] + 1}
WHERE
userid = {message.author.id};"""
)
await db.commit()
# print("updated longest streak")
else:
await db.execute(
f"""UPDATE fartstreak
SET currentstreak_end_date = '{today}',
currentstreak_length = {row[6] + 1},
total = {row[9] + 1}
WHERE
userid = {message.author.id};"""
)
await db.commit()
# print("updated only the current streak")
else:
if row[4] != str(today):
await db.execute(
f"""UPDATE fartstreak
SET currentstreak_end_date = '{today}',
currentstreak_start_date = '{today}',
currentstreak_length = 1,
total = {row[9] + 1}
WHERE
userid = {message.author.id};"""
)
await db.commit()
##### Commands #####
@bot.tree.command(
description="adds pfp links and names to db", guild=discord.Object(id=config.guild)
) # Add the guild ids in which the slash command will appear. If it should be in all, remove the argument, but note that it will take some time (up to an hour) to register the command if it's for all guilds.
async def update(interaction):
await interaction.response.defer(ephemeral=True, thinking=True)
async with aiosqlite.connect(config.dbpath) as db:
async with db.execute("SELECT * FROM fartstreak") as cursor:
rows = await cursor.fetchall()
for row in rows:
user = await bot.fetch_user(row[0])
# print(user)
await db.execute(
f"""UPDATE fartstreak
SET pfp = '{user.display_avatar.url}',
name = '{user.name}',
total = {max(row[9],row[5])}
WHERE
userid = {row[0]};"""
)
await db.commit()
await interaction.followup.send(content="done", ephemeral=True)
@bot.tree.command(
description="gets total number of days participated and updates",
guild=discord.Object(id=config.guild),
) # Add the guild ids in which the slash command will appear. If it should be in all, remove the argument, but note that it will take some time (up to an hour) to register the command if it's for all guilds.
async def totalupdate(interaction):
await interaction.response.defer(ephemeral=True, thinking=True)
channel = await bot.fetch_channel(config.channel)
a = {}
# print( "total messages pulled: " + str(len([message async for message in channel.history(limit = None)])))
async for message in channel.history(limit=None):
dt = message.created_at
date = (dt.year, dt.month, dt.day)
# print(message.author.id)
# print(a.keys())
try:
a[message.author.id].add(date)
except:
a[message.author.id] = {date}
print("printing final vlaue of A:")
print(a)
async with aiosqlite.connect(config.dbpath) as db:
async with db.execute("SELECT * FROM fartstreak") as cursor:
rows = await cursor.fetchall()
for row in rows:
try:
print(row[0])
print(a.keys())
print(a[row[0]])
await db.execute(
f"""UPDATE fartstreak
SET
total = {len(a[row[0]])}
WHERE
userid = {row[0]};"""
)
await db.commit()
except:
print("broke but idk why")
await interaction.followup.send(content="done", ephemeral=True)
@bot.tree.command(
description="for now it fixes the current streak",
guild=discord.Object(id=config.guild),
) # Add the guild ids in which the slash command will appear. If it should be in all, remove the argument, but note that it will take some time (up to an hour) to register the command if it's for all guilds.
async def reset_all(interaction):
await interaction.response.defer(ephemeral=True, thinking=True)
channel = await bot.fetch_channel(config.channel)
a = {}
# print( "total messages pulled: " + str(len([message async for message in channel.history(limit = None)])))
async for message in channel.history(limit=None):
dt = message.created_at
date = dt.date()
# print(message.author.id)
# print(a.keys())
try:
a[message.author.id].add(date)
except:
a[message.author.id] = {date}
print("printing final vlaue of A:")
print(a)
# now need to check for sequential dates
async with aiosqlite.connect(config.dbpath) as db:
async with db.execute("SELECT * FROM fartstreak") as cursor:
rows = await cursor.fetchall()
for row in rows:
try:
number_consecutive = 0
current_date = date.today()
print(a[row[0]])
while current_date in a[row[0]]:
number_consecutive += 1
current_date -= timedelta(days=1)
print(current_date)
# now calculate longest
current_date = date.fromisoformat("2023-01-01")
longest_consecutive = 0
current_consecutive = 0
while current_date <= date.today():
if current_date in a[row[0]]:
current_consecutive += 1
else:
longest_consecutive = max(
longest_consecutive, current_consecutive
)
current_consecutive = 0
current_date += timedelta(days=1)
longest_consecutive = max(longest_consecutive, current_consecutive)
print(row[0], longest_consecutive)
print(row[0], number_consecutive)
# print(row[0])
# print(a.keys())
# print(a[row[0]])
await db.execute(
f"""UPDATE fartstreak
SET
currentstreak_length = {number_consecutive},
longeststreak_length = {longest_consecutive}
WHERE
userid = {row[0]};"""
)
await db.commit()
except:
print("broke but idk why")
await interaction.followup.send(content="done", ephemeral=True)
# crontab
@bot.tree.command(
description="remove fart club role from all", guild=discord.Object(id=config.guild)
)
async def rm_roles(interaction):
await interaction.response.defer(ephemeral=True, thinking=True)
guild = bot.get_guild(config.guild)
role = get(guild.roles, id=config.general)
for member in guild.members:
await member.remove_roles(role)
await interaction.followup.send(content="done", ephemeral=True)
@bot.tree.command(
description="sanitize the last n messages in the fart club channel",
guild=discord.Object(id=config.guild),
)
@app_commands.default_permissions()
async def sanitize(interaction, n: int):
await interaction.response.defer(ephemeral=True)
def not_fart_club(m):
return m.content != "fart club"
channel = bot.get_channel(config.channel)
await channel.purge(limit=n, check=not_fart_club, reason="fart club")
await interaction.followup.send(content="done")
@bot.tree.context_menu(guild=discord.Object(id=config.guild))
async def get_data(interaction, member: discord.Member):
await interaction.response.defer(ephemeral=True)
async with aiosqlite.connect(config.dbpath) as db:
async with db.execute(
f"SELECT total, currentstreak_length, longeststreak_length FROM fartstreak WHERE userid = {member.id};"
) as cursor:
row = await cursor.fetchone()
if row == None:
await interaction.followup.send(
f"hmmm... {member.name} isn't in the database"
)
else:
async with db.execute(
f"SELECT COUNT(*) FROM fartstreak WHERE longeststreak_length > {row[2]};"
) as cursor:
place = await cursor.fetchone()
await interaction.followup.send(
f"{member.name} (#{place[0] + 1}):\n{row[0]} days participated\n{row[1]} day streak\n{row[2]} day longest streak"
)
async def reset_roles():
role = bot.get_guild(config.guild).get_role(config.general)
while True:
now = datetime.now()
await asyncio.sleep(
(
(now + timedelta(1)).replace(hour=0, minute=0, second=0) - now
).total_seconds()
)
for member in role.members:
await member.remove_roles(role)
bot.run(config.token)