lunarflu HF staff commited on
Commit
38302f8
1 Parent(s): 50eacdf

test (on_reaction_add , condensing add_exp

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