lunarflu HF staff commited on
Commit
0040dad
1 Parent(s): 88c0b0c

Synced repo using 'sync_with_huggingface' Github Action

Browse files
Files changed (1) hide show
  1. app.py +333 -77
app.py CHANGED
@@ -27,7 +27,15 @@ with open(file_path, 'w') as json_file:
27
  gspread_bot = gspread.service_account(filename='service_account.json')
28
  worksheet = gspread_bot.open("levelbot").sheet1
29
  """"""
30
- bot_ids = [1136614989411655780, 1166392942387265536, 1158038249835610123, 1130774761031610388, 1155489509518098565, 1155169841276260546, 1152238037355474964, 1154395078735953930]
 
 
 
 
 
 
 
 
31
  """"""
32
 
33
  @bot.event
@@ -36,10 +44,6 @@ async def on_ready():
36
  print(f"XP_PER_MESSAGE: {XP_PER_MESSAGE}")
37
 
38
 
39
-
40
-
41
-
42
-
43
  def calculate_level(xp):
44
  return int(xp ** (1.0 / 3.0))
45
 
@@ -48,84 +52,336 @@ def calculate_xp(level):
48
  return (int(level ** 3))
49
 
50
 
51
- async def add_exp(member):
52
- try:
53
- guild = bot.get_guild(879548962464493619)
54
- print(guild)
55
- lvl1 = guild.get_role(1171861537699397733)
56
- lvl2 = guild.get_role(1171861595115245699)
57
- lvl3 = guild.get_role(1171861626715115591)
58
- lvl4 = guild.get_role(1171861657975259206)
59
- lvl5 = guild.get_role(1171861686580412497)
60
- lvl6 = guild.get_role(1171861900301172736)
61
- lvl7 = guild.get_role(1171861936258941018)
62
- lvl8 = guild.get_role(1171861968597024868)
63
- lvl9 = guild.get_role(1171862009982242836)
64
- lvl10 = guild.get_role(1164188093713223721)
65
- lvl11 = guild.get_role(1171524944354607104)
66
- lvl12 = guild.get_role(1171524990257082458)
67
- lvl13 = guild.get_role(1171525021928263791)
68
- lvl14 = guild.get_role(1171525062201966724)
69
- lvl15 = guild.get_role(1171525098465918996)
70
- lvls = {
71
- 1: lvl1, 2: lvl2, 3: lvl3, 4: lvl4, 5: lvl5, 6: lvl6, 7: lvl7, 8: lvl8, 9: lvl9, 10: lvl10,
72
- 11: lvl11, 12: lvl12, 13: lvl13, 14: lvl14, 15: lvl15,
73
- }
74
- #if member.id == 811235357663297546:
75
- # does a record already exist?
76
- cell = worksheet.find(str(member.id))
77
- length = len(worksheet.col_values(1))
78
- if cell is None:
79
- print(f"creating new record for {member}")
80
- # if not, create new record
81
- string_member_id = str(member.id)
82
- xp = 10
83
- current_level = calculate_level(xp)
84
- member_name = member.name
85
- worksheet.update(values=[[string_member_id, member_name, xp, current_level]], range_name=f'A{length+1}:D{length+1}')
86
- # initial role assignment
87
- if current_level == 1:
88
- if lvl1 not in member.roles:
89
- await member.add_roles(lvl1)
90
- print(f"Gave {member} {lvl1}")
91
- else:
92
- if cell:
93
- # if so, update that row...
94
- xp = worksheet.cell(cell.row, cell.col+2).value
95
- xp = int(xp) + XP_PER_MESSAGE
96
- current_level = calculate_level(xp)
97
- print(f"updating record for {member}: {xp} xp")
98
- # write with added xp
99
- worksheet.update(values=[[xp, current_level]], range_name=f'C{cell.row}:D{cell.row}')
100
- # level up
101
- if current_level >= 2 and current_level <=15:
102
- current_role = lvls[current_level]
103
- if current_role not in member.roles:
104
- await member.add_roles(current_role)
105
- print(f"Gave {member} {current_role}")
106
- await member.remove_roles(lvls[current_level-1])
107
- print(f"Removed {lvls[current_level-1]} from {member}")
108
- except Exception as e:
109
- print(f"add_exp Error: {e}")
110
-
111
-
112
  @bot.event
113
  async def on_message(message):
114
  try:
115
- if message.author.id not in bot_ids:
116
- await add_exp(message.author)
117
- await bot.process_commands(message)
118
- except Exception as e:
119
- print(f"on_message Error: {e}")
120
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
 
122
- @bot.event
123
- async def on_reaction_add(reaction, user):
124
- try:
125
- if user.id not in bot_ids:
126
- await add_exp(user)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  except Exception as e:
128
- print(f"on_reaction_add Error: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
 
130
 
131
  """"""
 
27
  gspread_bot = gspread.service_account(filename='service_account.json')
28
  worksheet = gspread_bot.open("levelbot").sheet1
29
  """"""
30
+ API_URL = "https://api-inference.huggingface.co/models/mariagrandury/roberta-base-finetuned-sms-spam-detection"
31
+ HF_TOKEN = os.environ.get("HF_TOKEN", None)
32
+ headers = {"Authorization": f"Bearer {HF_TOKEN}"}
33
+ """
34
+ def query(payload):
35
+ response = requests.post(API_URL, headers=headers, json=payload)
36
+ return response.json()
37
+ """
38
+
39
  """"""
40
 
41
  @bot.event
 
44
  print(f"XP_PER_MESSAGE: {XP_PER_MESSAGE}")
45
 
46
 
 
 
 
 
47
  def calculate_level(xp):
48
  return int(xp ** (1.0 / 3.0))
49
 
 
52
  return (int(level ** 3))
53
 
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  @bot.event
56
  async def on_message(message):
57
  try:
58
+ if message.author != bot.user:
59
+
60
+ #output = query({"inputs": f"{message.content}",})
61
+ #print(output)
62
+ bot_ids = [1166392942387265536, 1158038249835610123, 1130774761031610388, 1155489509518098565, 1155169841276260546, 1152238037355474964, 1154395078735953930]
63
 
64
+ try:
65
+ if message.author.id not in bot_ids:
66
+ #if message.author.id == 811235357663297546:
67
+ # get level
68
+ guild = bot.get_guild(879548962464493619)
69
+ lvl1 = guild.get_role(1171861537699397733)
70
+ lvl2 = guild.get_role(1171861595115245699)
71
+ lvl3 = guild.get_role(1171861626715115591)
72
+ lvl4 = guild.get_role(1171861657975259206)
73
+ lvl5 = guild.get_role(1171861686580412497)
74
+ lvl6 = guild.get_role(1171861900301172736)
75
+ lvl7 = guild.get_role(1171861936258941018)
76
+ lvl8 = guild.get_role(1171861968597024868)
77
+ lvl9 = guild.get_role(1171862009982242836)
78
+ lvl10 = guild.get_role(1164188093713223721)
79
+ lvl11 = guild.get_role(1171524944354607104)
80
+ lvl12 = guild.get_role(1171524990257082458)
81
+ lvl13 = guild.get_role(1171525021928263791)
82
+ lvl14 = guild.get_role(1171525062201966724)
83
+ lvl15 = guild.get_role(1171525098465918996)
84
+
85
+ lvls = {
86
+ 1: lvl1,
87
+ 2: lvl2,
88
+ 3: lvl3,
89
+ 4: lvl4,
90
+ 5: lvl5,
91
+ 6: lvl6,
92
+ 7: lvl7,
93
+ 8: lvl8,
94
+ 9: lvl9,
95
+ 10: lvl10,
96
+ 11: lvl11,
97
+ 12: lvl12,
98
+ 13: lvl13,
99
+ 14: lvl14,
100
+ 15: lvl15,
101
+ }
102
 
103
+ # does a record already exist?
104
+ cell = worksheet.find(str(message.author.id))
105
+ length = len(worksheet.col_values(1))
106
+ if cell is None:
107
+ print(f"creating new record for {message.author}")
108
+ # if not, create new record
109
+ string_member_id = str(message.author.id)
110
+ xp = 10
111
+ new_level = calculate_level(xp)
112
+ message_author_name = message.author.name
113
+ worksheet.update(values=[[string_member_id, message_author_name, xp, new_level]], range_name=f'A{length+1}:D{length+1}')
114
+ else:
115
+ if cell:
116
+ # if so, update that row...
117
+ # update exp, can only be in a positive direction
118
+ # read
119
+ xp = worksheet.cell(cell.row, cell.col+2).value
120
+ xp = int(xp) + XP_PER_MESSAGE
121
+ current_level = calculate_level(xp)
122
+ print(f"updating record for {message.author}: {xp} xp")
123
+ # write with added xp
124
+ worksheet.update(values=[[xp, current_level]], range_name=f'C{cell.row}:D{cell.row}')
125
+ # initial role assignment
126
+ if current_level == 1:
127
+ if lvl1 not in message.author.roles:
128
+ await message.author.add_roles(lvl1)
129
+ print(f"Gave {message.author} {lvl1}")
130
+ # level up
131
+ elif current_level >= 2 and current_level <=15:
132
+ current_role = lvls[current_level]
133
+ if current_role not in message.author.roles:
134
+ await message.author.add_roles(current_role)
135
+ print(f"Gave {message.author} {current_role}")
136
+ await message.author.remove_roles(lvls[current_level -1])
137
+ print(f"Removed {lvls[current_level -1]} from {message.author}")
138
+
139
+
140
+ """
141
+ value = cell.value
142
+ row_number = cell.row
143
+ column_number = cell.col
144
+ """
145
+ except Exception as e:
146
+ print(f"Error: {e}")
147
+
148
+ await bot.process_commands(message)
149
+
150
  except Exception as e:
151
+ print(f"Error: {e}")
152
+
153
+
154
+ @bot.command()
155
+ async def level(ctx):
156
+ if ctx.author.id == 811235357663297546:
157
+
158
+ try:
159
+ user_data = []
160
+
161
+ for author_id_str, xp_value in xp_data.items():
162
+ try:
163
+ current_level = calculate_level(xp_value)
164
+ author_id = int(author_id_str)
165
+ user = bot.get_user(author_id)
166
+ user_data.append((user, xp_value, current_level))
167
+
168
+ except Exception as e:
169
+ print(f"Error for user {author_id}: {e}")
170
+
171
+ sorted_user_data = sorted(user_data, key=lambda x: x[1], reverse=True)
172
+
173
+ for user, xp, level in sorted_user_data:
174
+ print(f"user: {user} | xp: {xp} | level: {level}")
175
+
176
+ except Exception as e:
177
+ print(f"Error: {e}")
178
+ """
179
+ if author_id in xp_data:
180
+ xp = xp_data[author_id]
181
+ level = calculate_level(xp)
182
+ await ctx.send(f'You are at level {level} with {xp} XP.')
183
+ else:
184
+ await ctx.send('You have not earned any XP yet.')
185
+ # show top users by level / exp
186
+ """
187
+
188
+
189
+
190
+ @bot.command()
191
+ async def count(ctx):
192
+ """Count total messages per user in all channels."""
193
+ if ctx.author.id == 811235357663297546:
194
+ message_counts = {}
195
+
196
+ for channel in ctx.guild.text_channels:
197
+ try:
198
+ async for message in channel.history(limit=None):
199
+ message_counts[message.author] = message_counts.get(message.author, 0) + 1
200
+ except discord.Forbidden:
201
+ # Handle the Forbidden error
202
+ #await ctx.send(f"Missing access to read messages in {channel.name}")
203
+ print(f"Missing access to read messages in {channel.name}")
204
+
205
+ sorted_users = sorted(message_counts.items(), key=lambda x: x[1], reverse=True)
206
+ top_list = "\n".join([f"{member.name}: {count}" for member, count in sorted_users[:50]])
207
+ #await ctx.send(f"Message count per user in all text channels:\n{top_list}")
208
+ print(f"Message count per user in all text channels:\n{top_list}")
209
+
210
+
211
+ @bot.command()
212
+ async def count_messages1(ctx):
213
+ if ctx.author.id == 811235357663297546:
214
+ end_date = datetime.datetime.utcnow() # Current date and time
215
+ start_date = end_date - datetime.timedelta(days=1)
216
+ message_count = 0
217
+ for channel in ctx.guild.text_channels:
218
+ try:
219
+ async for message in channel.history(limit=None, after=start_date, before=end_date):
220
+ message_count += 1
221
+ except discord.Forbidden:
222
+ print(f"Missing access to read messages in {channel.name}")
223
+ print(f'Total messages between {start_date} and {end_date}: {message_count}')
224
+
225
+
226
+ @bot.command()
227
+ async def count_messages7(ctx):
228
+ if ctx.author.id == 811235357663297546:
229
+ end_date = datetime.datetime.utcnow() # Current date and time
230
+ start_date = end_date - datetime.timedelta(days=7)
231
+ message_count = 0
232
+ for channel in ctx.guild.text_channels:
233
+ try:
234
+ async for message in channel.history(limit=None, after=start_date, before=end_date):
235
+ message_count += 1
236
+ except discord.Forbidden:
237
+ print(f"Missing access to read messages in {channel.name}")
238
+ print(f'Total messages between {start_date} and {end_date}: {message_count}')
239
+
240
+
241
+ @bot.command()
242
+ async def count_messages14(ctx):
243
+ if ctx.author.id == 811235357663297546:
244
+ end_date = datetime.datetime.utcnow() # Current date and time
245
+ start_date = end_date - datetime.timedelta(days=14)
246
+ message_count = 0
247
+ for channel in ctx.guild.text_channels:
248
+ try:
249
+ async for message in channel.history(limit=None, after=start_date, before=end_date):
250
+ message_count += 1
251
+ except discord.Forbidden:
252
+ print(f"Missing access to read messages in {channel.name}")
253
+ print(f'Total messages between {start_date} and {end_date}: {message_count}')
254
+
255
+
256
+ @bot.command()
257
+ async def count_messages30(ctx):
258
+ if ctx.author.id == 811235357663297546:
259
+ end_date = datetime.datetime.utcnow() # Current date and time
260
+ start_date = end_date - datetime.timedelta(days=30)
261
+ message_count = 0
262
+ for channel in ctx.guild.text_channels:
263
+ try:
264
+ async for message in channel.history(limit=None, after=start_date, before=end_date):
265
+ message_count += 1
266
+ except discord.Forbidden:
267
+ print(f"Missing access to read messages in {channel.name}")
268
+ print(f'Total messages between {start_date} and {end_date}: {message_count}')
269
+
270
+
271
+ @bot.command()
272
+ async def count_messages60(ctx):
273
+ if ctx.author.id == 811235357663297546:
274
+ end_date = datetime.datetime.utcnow() # Current date and time
275
+ start_date = end_date - datetime.timedelta(days=60)
276
+ message_count = 0
277
+ for channel in ctx.guild.text_channels:
278
+ try:
279
+ async for message in channel.history(limit=None, after=start_date, before=end_date):
280
+ message_count += 1
281
+ except discord.Forbidden:
282
+ print(f"Missing access to read messages in {channel.name}")
283
+ print(f'Total messages between {start_date} and {end_date}: {message_count}')
284
+
285
+
286
+ @bot.command()
287
+ async def count_messages(ctx, time: int):
288
+ if ctx.author.id == 811235357663297546:
289
+ end_date = datetime.datetime.utcnow() # Current date and time
290
+ start_date = end_date - datetime.timedelta(days=time)
291
+ message_count = 0
292
+ for channel in ctx.guild.text_channels:
293
+ print(channel.name)
294
+ try:
295
+ async for message in channel.history(limit=None, after=start_date, before=end_date):
296
+ message_count += 1
297
+ except discord.Forbidden:
298
+ print(f"Missing access to read messages in {channel.name}")
299
+ print(f'Total messages between {start_date} and {end_date}: {message_count}')
300
+
301
+
302
+ @bot.command()
303
+ async def count_threads(ctx, time: int):
304
+ if ctx.author.id == 811235357663297546:
305
+
306
+ end_date = datetime.datetime.utcnow() # Current date and time
307
+ start_date = end_date - datetime.timedelta(days=time)
308
+ thread_message_count = 0
309
+
310
+ for channel in ctx.guild.text_channels:
311
+ print(f"CHANNELNAME: {channel.name}")
312
+ for thread in channel.threads:
313
+ print(channel.name)
314
+ print(thread.name)
315
+ async for message in thread.history(limit=None, after=start_date, before=end_date):
316
+ thread_message_count += 1
317
+ print(f'Total thread_messages between {start_date} and {end_date}: {thread_message_count}')
318
+
319
+
320
+ @bot.command()
321
+ async def count_forum_threads(ctx, time: int):
322
+ if ctx.author.id == 811235357663297546:
323
+ end_date = datetime.datetime.utcnow() # Current date and time
324
+ start_date = end_date - datetime.timedelta(days=time)
325
+ forum_thread_message_count = 0
326
+
327
+ for forum in ctx.guild.forums:
328
+ print(f"FORUMNAME: {forum.name}")
329
+ for thread in forum.threads:
330
+ print(forum.name)
331
+ print(thread.name)
332
+ async for message in thread.history(limit=None, after=start_date, before=end_date):
333
+ print(f"THREAD NAME: {thread.name}")
334
+ forum_thread_message_count += 1
335
+ """
336
+ async for thread in forum.archived_threads(limit=None, before=end_date):
337
+ print(f"ARCHIVED THREAD NAME: {thread.name}")
338
+ forum_thread_message_count += 1
339
+ """
340
+
341
+ print(f'Total forum_thread_messages between {start_date} and {end_date}: {forum_thread_message_count}')
342
+
343
+
344
+ @bot.command()
345
+ async def top_gradio(ctx, channel_id):
346
+ if ctx.author.id == 811235357663297546:
347
+ message_counts = {}
348
+ try:
349
+ channel = await bot.fetch_channel(channel_id)
350
+
351
+ async for message in channel.history(limit=None):
352
+ message_counts[message.author] = message_counts.get(message.author, 0) + 1
353
+ except discord.Forbidden:
354
+ print(f"Missing access to read messages in {channel.name}")
355
+
356
+ sorted_users = sorted(message_counts.items(), key=lambda x: x[1], reverse=True)
357
+ top_list = "\n".join([f"{member.name}: {count}" for member, count in sorted_users[:50]])
358
+ print(f"Message count per user in the channel:\n{top_list}")
359
+
360
+
361
+ @bot.command()
362
+ async def top_gradio_threads(ctx, channel_id):
363
+ if ctx.author.id == 811235357663297546:
364
+ message_counts = {}
365
+
366
+ channel = await bot.fetch_channel(channel_id)
367
+ print(channel)
368
+ threads = channel.threads
369
+ print(threads)
370
+ for thread in threads:
371
+ print(f"Thread Name: {thread.name}, Thread ID: {thread.id}, Parent ID: {thread.parent_id}")
372
+ async for message in thread.history(limit=None):
373
+ message_counts[message.author] = message_counts.get(message.author, 0) + 1
374
+
375
+ async for thread in channel.archived_threads(limit=None):
376
+ print(f"ARCHIVED Thread Name: {thread.name}, ARCHIVED Thread ID: {thread.id}, Parent ID: {thread.parent_id}")
377
+ async for message in thread.history(limit=None):
378
+ message_counts[message.author] = message_counts.get(message.author, 0) + 1
379
+
380
+
381
+
382
+ sorted_users = sorted(message_counts.items(), key=lambda x: x[1], reverse=True)
383
+ top_list = "\n".join([f"[{member.id}]{member.name}: {count}" for member, count in sorted_users[:50]])
384
+ print(f"Message count per user in the channel:\n{top_list}")
385
 
386
 
387
  """"""