File size: 21,523 Bytes
cf1e692
7187a61
 
cf1e692
 
5cb8bba
11fb06f
5086fc0
 
6d949e0
7187a61
 
 
a5603d3
7187a61
cf1e692
 
a5603d3
6c5f294
cf1e692
80a1e02
cf1e692
5086fc0
 
 
 
 
 
 
11fb06f
 
 
ab6239f
11fb06f
 
 
ab6239f
 
ac98ac7
0aa83ad
6b5ae25
 
 
 
0aa83ad
 
ac98ac7
 
 
 
 
 
ce27741
 
cf1e692
6c5f294
6d949e0
cf1e692
11fb06f
6971a6a
 
5e6f2d9
b5e3666
3ad4cda
015591e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac98ac7
 
 
 
 
5a9d361
ac98ac7
5a9d361
ac98ac7
a2a259d
8a36c89
 
 
a2a259d
8a36c89
 
a2a259d
ac98ac7
 
5a9d361
ac98ac7
 
 
 
0ca9f8f
ac98ac7
0ca9f8f
ac98ac7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
015591e
ac98ac7
 
 
 
 
 
015591e
ac98ac7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5e3666
 
 
cf1e692
ac98ac7
6d949e0
 
abcbe65
 
cf1e692
6c5f294
3c44710
32959ef
52ccbe4
 
 
 
 
 
 
 
 
32959ef
52ccbe4
 
 
b9d4588
52ccbe4
 
 
 
 
 
32959ef
3c44710
 
 
 
 
 
32959ef
 
 
c7ed7f7
bb61616
cf1e692
8a13292
 
3c44710
 
 
 
 
 
 
 
 
b46ad23
 
3c44710
 
b46ad23
 
 
c7ed7f7
5cb8bba
 
c3e50f2
5cb8bba
 
 
 
c3e50f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5cb8bba
 
c3e50f2
a393a37
c3e50f2
 
 
 
5cb8bba
 
 
 
c3e50f2
 
 
5cb8bba
c3e50f2
 
 
 
 
 
 
 
 
 
 
5cb8bba
 
c3e50f2
 
5cb8bba
c3e50f2
7c6c962
c3e50f2
 
7c6c962
c3e50f2
 
7c6c962
c3e50f2
 
 
 
 
 
5cb8bba
7c6c962
7e7c9c4
 
 
 
 
 
 
 
 
31fc3e6
7e7c9c4
31fc3e6
7e7c9c4
f6d2498
7e7c9c4
 
7c6c962
31fc3e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01381bb
 
31fc3e6
01381bb
 
 
31fc3e6
 
bb61616
 
 
 
 
 
41f6d43
 
bb61616
 
 
 
 
 
 
 
 
680ad08
 
9b2d35f
680ad08
 
c76f5d6
e32943a
9b2d35f
 
d8acdb0
9b2d35f
56cc113
 
 
9b2d35f
 
319ee89
 
 
 
9b2d35f
680ad08
 
96097d8
680ad08
 
cf1e692
 
 
6d949e0
cf1e692
7187a61
cf1e692
 
 
40b2a54
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
import discord
import os
import threading
from discord.ext import commands
import json
import datetime
import requests
import os.path
import gspread

import gradio_client
import gradio as gr
from gradio_client import Client

DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
intents = discord.Intents.all() 
bot = commands.Bot(command_prefix='!', intents=intents)


""""""
XP_PER_MESSAGE = 10 # 100k messages = 1M exp = lvl 100
""""""
service_account = json.loads(os.environ.get('KEY'))
file_path = 'service_account.json'
with open(file_path, 'w') as json_file:
    json.dump(service_account, json_file)
gspread_bot = gspread.service_account(filename='service_account.json')
worksheet = gspread_bot.open("levelbot").sheet1
""""""
API_URL = "https://api-inference.huggingface.co/models/mariagrandury/roberta-base-finetuned-sms-spam-detection"
HF_TOKEN = os.environ.get("HF_TOKEN", None)
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
"""
def query(payload):
	response = requests.post(API_URL, headers=headers, json=payload)
	return response.json()
"""

""""""

@bot.event
async def on_ready():
    print(f'Logged in as {bot.user.name}')
    print(f"XP_PER_MESSAGE: {XP_PER_MESSAGE}")


def calculate_level(xp):
    return int(xp ** (1.0 / 3.0))


def calculate_xp(level):
    return (int(level ** 3))


@bot.event
async def on_message(message):
    try:
        if message.author != bot.user:

            #output = query({"inputs": f"{message.content}",})
            #print(output)
            bot_ids = [1166392942387265536, 1158038249835610123, 1130774761031610388, 1155489509518098565, 1155169841276260546, 1152238037355474964, 1154395078735953930]

            try:
                if message.author.id not in bot_ids:
                    #if message.author.id == 811235357663297546:
                    # get level
                    guild = bot.get_guild(879548962464493619)
                    lvl1 = guild.get_role(1171861537699397733)
                    lvl2 = guild.get_role(1171861595115245699)
                    lvl3 = guild.get_role(1171861626715115591)
                    lvl4 = guild.get_role(1171861657975259206)
                    lvl5 = guild.get_role(1171861686580412497)
                    lvl6 = guild.get_role(1171861900301172736)
                    lvl7 = guild.get_role(1171861936258941018)
                    lvl8 = guild.get_role(1171861968597024868)
                    lvl9 = guild.get_role(1171862009982242836)
                    lvl10 = guild.get_role(1164188093713223721)
                    lvl11 = guild.get_role(1171524944354607104)
                    lvl12 = guild.get_role(1171524990257082458)
                    lvl13 = guild.get_role(1171525021928263791)
                    lvl14 = guild.get_role(1171525062201966724)
                    lvl15 = guild.get_role(1171525098465918996)
        
                    # does a record already exist?
                    cell = worksheet.find(str(message.author.id))
                    length = len(worksheet.col_values(1))
                    if cell is None:
                        print(f"creating new record for {message.author}")            
                        # if not, create new record
                        string_member_id = str(message.author.id)
                        xp = 10
                        new_level = calcular_level(xp)
                        print(string_member_id)
                        print(message.author.name)
                        print(xp)
                        print(new_level)
                        
                        message_author_name = message.author.name
                        worksheet.update(values=[[string_member_id, message_author_name, xp, new_level]], range_name=f'A{length+1}:D{length+1}')
                    else:
                        if cell:
                            print(f"updating record for {message.author}")
                            # if so, update that row...
                            # update exp, can only be in a positive direction
                            # read
                            xp = worksheet.cell(cell.row, cell.col+2).value
                            xp = int(xp) + XP_PER_MESSAGE
                            current_level = calculate_level(xp)
                            print(current_level)
                            # write with added xp
                            worksheet.update(values=[[xp, current_level]], range_name=f'C{cell.row}:D{cell.row}')   
                            if current_level == 2:
                                if lvl2 not in message.author.roles:
                                    await message.author.add_roles(lvl2)  
                                    print(f"Gave {message.author} {lvl2}")
                                    await message.author.remove_roles(lvl1)
                                    print(f"Removed {lvl1} from {message.author}")
            
                            if current_level == 3:
                                if lvl3 not in message.author.roles:
                                    await message.author.add_roles(lvl3)  
                                    print(f"Gave {message.author} {lvl3}")
                                    await message.author.remove_roles(lvl2)
                                    print(f"Removed {lvl2} from {message.author}")
            
                            if current_level == 4:
                                if lvl4 not in message.author.roles:
                                    await message.author.add_roles(lvl4)  
                                    print(f"Gave {message.author} {lvl4}")
                                    await message.author.remove_roles(lvl3)
                                    print(f"Removed {lvl3} from {message.author}")
            
                            if current_level == 5:
                                if lvl5 not in message.author.roles:
                                    await message.author.add_roles(lvl5)  
                                    print(f"Gave {message.author} {lvl5}")
                                    await message.author.remove_roles(lvl4)
                                    print(f"Removed {lvl4} from {message.author}")
            
                            if current_level == 6:
                                if lvl6 not in message.author.roles:
                                    await message.author.add_roles(lvl6)  
                                    print(f"Gave {message.author} {lvl6}")
                                    await message.author.remove_roles(lvl5)
                                    print(f"Removed {lvl5} from {message.author}")
                                    
                            if current_level == 7:
                                if lvl7 not in message.author.roles:
                                    await message.author.add_roles(lvl7)  
                                    print(f"Gave {message.author} {lvl7}")
                                    await message.author.remove_roles(lvl6)
                                    print(f"Removed {lvl6} from {message.author}")
            
                            if current_level == 8:
                                if lvl8 not in message.author.roles:
                                    await message.author.add_roles(lvl8)  
                                    print(f"Gave {message.author} {lvl8}")
                                    await message.author.remove_roles(lvl7)
                                    print(f"Removed {lvl7} from {message.author}")
                            
                            if current_level == 9:
                                if lvl9 not in message.author.roles:
                                    await message.author.add_roles(lvl9)  
                                    print(f"Gave {message.author} {lvl9}")
                                    await message.author.remove_roles(lvl8)
                                    print(f"Removed {lvl8} from {message.author}")                    
                            
                            if current_level == 10:
                                if lvl10 not in message.author.roles:
                                    await message.author.add_roles(lvl10)
                                    print(f"Gave {message.author} {lvl10}")
                                    await message.author.remove_roles(lvl9)
                                    print(f"Removed {lvl9} from {message.author}") 
            
                            if current_level == 11:
                                if lvl11 not in message.author.roles:
                                    await message.author.add_roles(lvl11)  
                                    print(f"Gave {message.author} {lvl11}")
                                    await message.author.remove_roles(lvl10)
                                    print(f"Removed {lvl10} from {message.author}")
                                
                            if current_level == 12:
                                if lvl12 not in message.author.roles:
                                    await message.author.add_roles(lvl12)
                                    print(f"Gave {message.author} {lvl12}")
                                    await message.author.remove_roles(lvl11)
                                    print(f"Removed {lvl11} from {message.author}")
                                    
                            if current_level == 13:
                                if lvl13 not in message.author.roles:
                                    await message.author.add_roles(lvl13)
                                    print(f"Gave {message.author} {lvl13}")
                                    await message.author.remove_roles(lvl12)
                                    print(f"Removed {lvl12} from {message.author}")
                                    
                            if current_level == 14:
                                if lvl14 not in message.author.roles:
                                    await message.author.add_roles(lvl14)
                                    print(f"Gave {message.author} {lvl14}")
                                    await message.author.remove_roles(lvl13)
                                    print(f"Removed {lvl13} from {message.author}")
                                    
                            if current_level == 15:
                                if lvl15 not in message.author.roles:
                                    await message.author.add_roles(lvl15) 
                                    print(f"Gave {message.author} {lvl15}")
                                    await message.author.remove_roles(lvl14)
                                    print(f"Removed {lvl14} from {message.author}")                    

        
                            """
                            value = cell.value
                            row_number = cell.row
                            column_number = cell.col                
                            """                    
            except Exception as e:
                print(f"Error: {e}")

            await bot.process_commands(message)
            
    except Exception as e:
        print(f"Error: {e}")
            

@bot.command()
async def level(ctx):
    if ctx.author.id == 811235357663297546:

        try:
            user_data = []

            for author_id_str, xp_value in xp_data.items():
                try:
                    current_level = calculate_level(xp_value)
                    author_id = int(author_id_str)
                    user = bot.get_user(author_id)
                    user_data.append((user, xp_value, current_level))
                
                except Exception as e:
                    print(f"Error for user {author_id}: {e}")

            sorted_user_data = sorted(user_data, key=lambda x: x[1], reverse=True)

            for user, xp, level in sorted_user_data:
                print(f"user: {user} | xp: {xp} | level: {level}")

        except Exception as e:
            print(f"Error: {e}")
        """
        if author_id in xp_data:
            xp = xp_data[author_id]
            level = calculate_level(xp)
            await ctx.send(f'You are at level {level} with {xp} XP.')
        else:
            await ctx.send('You have not earned any XP yet.')
            # show top users by level / exp        
        """



@bot.command()
async def count(ctx):
    """Count total messages per user in all channels."""
    if ctx.author.id == 811235357663297546:    
        message_counts = {}
    
        for channel in ctx.guild.text_channels:
            try:
                async for message in channel.history(limit=None):
                    message_counts[message.author] = message_counts.get(message.author, 0) + 1
            except discord.Forbidden:
                # Handle the Forbidden error
                #await ctx.send(f"Missing access to read messages in {channel.name}")
                print(f"Missing access to read messages in {channel.name}")
    
        sorted_users = sorted(message_counts.items(), key=lambda x: x[1], reverse=True)
        top_list = "\n".join([f"{member.name}: {count}" for member, count in sorted_users[:50]])
        #await ctx.send(f"Message count per user in all text channels:\n{top_list}")
        print(f"Message count per user in all text channels:\n{top_list}")


@bot.command()
async def count_messages1(ctx):
    if ctx.author.id == 811235357663297546: 
        end_date = datetime.datetime.utcnow()  # Current date and time
        start_date = end_date - datetime.timedelta(days=1)
        message_count = 0
        for channel in ctx.guild.text_channels:
            try:
                async for message in channel.history(limit=None, after=start_date, before=end_date):
                    message_count += 1
            except discord.Forbidden:
                print(f"Missing access to read messages in {channel.name}")
        print(f'Total messages between {start_date} and {end_date}: {message_count}')


@bot.command()
async def count_messages7(ctx):
    if ctx.author.id == 811235357663297546: 
        end_date = datetime.datetime.utcnow()  # Current date and time
        start_date = end_date - datetime.timedelta(days=7)
        message_count = 0
        for channel in ctx.guild.text_channels:
            try:
                async for message in channel.history(limit=None, after=start_date, before=end_date):
                    message_count += 1
            except discord.Forbidden:
                print(f"Missing access to read messages in {channel.name}")
        print(f'Total messages between {start_date} and {end_date}: {message_count}')


@bot.command()
async def count_messages14(ctx):
    if ctx.author.id == 811235357663297546: 
        end_date = datetime.datetime.utcnow()  # Current date and time
        start_date = end_date - datetime.timedelta(days=14)
        message_count = 0
        for channel in ctx.guild.text_channels:
            try:
                async for message in channel.history(limit=None, after=start_date, before=end_date):
                    message_count += 1
            except discord.Forbidden:
                print(f"Missing access to read messages in {channel.name}")
        print(f'Total messages between {start_date} and {end_date}: {message_count}')  


@bot.command()
async def count_messages30(ctx):
    if ctx.author.id == 811235357663297546: 
        end_date = datetime.datetime.utcnow()  # Current date and time
        start_date = end_date - datetime.timedelta(days=30)
        message_count = 0
        for channel in ctx.guild.text_channels:
            try:
                async for message in channel.history(limit=None, after=start_date, before=end_date):
                    message_count += 1
            except discord.Forbidden:
                print(f"Missing access to read messages in {channel.name}")
        print(f'Total messages between {start_date} and {end_date}: {message_count}')


@bot.command()
async def count_messages60(ctx):
    if ctx.author.id == 811235357663297546: 
        end_date = datetime.datetime.utcnow()  # Current date and time
        start_date = end_date - datetime.timedelta(days=60)
        message_count = 0
        for channel in ctx.guild.text_channels:
            try:
                async for message in channel.history(limit=None, after=start_date, before=end_date):
                    message_count += 1
            except discord.Forbidden:
                print(f"Missing access to read messages in {channel.name}")
        print(f'Total messages between {start_date} and {end_date}: {message_count}')


@bot.command()
async def count_messages(ctx, time: int):
    if ctx.author.id == 811235357663297546: 
        end_date = datetime.datetime.utcnow()  # Current date and time
        start_date = end_date - datetime.timedelta(days=time)
        message_count = 0
        for channel in ctx.guild.text_channels:
            print(channel.name)
            try:
                async for message in channel.history(limit=None, after=start_date, before=end_date):
                    message_count += 1
            except discord.Forbidden:
                print(f"Missing access to read messages in {channel.name}")
        print(f'Total messages between {start_date} and {end_date}: {message_count}')


@bot.command()
async def count_threads(ctx, time: int):
    if ctx.author.id == 811235357663297546:

        end_date = datetime.datetime.utcnow()  # Current date and time
        start_date = end_date - datetime.timedelta(days=time)
        thread_message_count = 0
        
        for channel in ctx.guild.text_channels:
            print(f"CHANNELNAME:                      {channel.name}")
            for thread in channel.threads:
                print(channel.name)
                print(thread.name)
                async for message in thread.history(limit=None, after=start_date, before=end_date):
                    thread_message_count += 1
        print(f'Total thread_messages between {start_date} and {end_date}: {thread_message_count}')


@bot.command()
async def count_forum_threads(ctx, time: int):
    if ctx.author.id == 811235357663297546:
        end_date = datetime.datetime.utcnow()  # Current date and time
        start_date = end_date - datetime.timedelta(days=time)
        forum_thread_message_count = 0
        
        for forum in ctx.guild.forums:
            print(f"FORUMNAME:                      {forum.name}")
            for thread in forum.threads:
                print(forum.name)
                print(thread.name)
                async for message in thread.history(limit=None, after=start_date, before=end_date):
                    print(f"THREAD NAME: {thread.name}")
                    forum_thread_message_count += 1
            """
            async for thread in forum.archived_threads(limit=None, before=end_date):
                print(f"ARCHIVED THREAD NAME: {thread.name}")
                forum_thread_message_count += 1            
            """

        print(f'Total forum_thread_messages between {start_date} and {end_date}: {forum_thread_message_count}')


@bot.command()
async def top_gradio(ctx, channel_id):
    if ctx.author.id == 811235357663297546:    
        message_counts = {}
        try:
            channel = await bot.fetch_channel(channel_id)
            
            async for message in channel.history(limit=None):
                message_counts[message.author] = message_counts.get(message.author, 0) + 1
        except discord.Forbidden:
            print(f"Missing access to read messages in {channel.name}")
    
        sorted_users = sorted(message_counts.items(), key=lambda x: x[1], reverse=True)
        top_list = "\n".join([f"{member.name}: {count}" for member, count in sorted_users[:50]])
        print(f"Message count per user in the channel:\n{top_list}")


@bot.command()
async def top_gradio_threads(ctx, channel_id):
    if ctx.author.id == 811235357663297546:    
        message_counts = {}
        
        channel = await bot.fetch_channel(channel_id)
        print(channel)
        threads = channel.threads
        print(threads)
        for thread in threads:
            print(f"Thread Name: {thread.name}, Thread ID: {thread.id}, Parent ID: {thread.parent_id}")        
            async for message in thread.history(limit=None):
                message_counts[message.author] = message_counts.get(message.author, 0) + 1
        
        async for thread in channel.archived_threads(limit=None):
            print(f"ARCHIVED Thread Name: {thread.name}, ARCHIVED Thread ID: {thread.id}, Parent ID: {thread.parent_id}") 
            async for message in thread.history(limit=None):
                message_counts[message.author] = message_counts.get(message.author, 0) + 1            



        sorted_users = sorted(message_counts.items(), key=lambda x: x[1], reverse=True)
        top_list = "\n".join([f"[{member.id}]{member.name}: {count}" for member, count in sorted_users[:50]])
        print(f"Message count per user in the channel:\n{top_list}")

        
""""""
DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
def run_bot():
    bot.run(DISCORD_TOKEN)
threading.Thread(target=run_bot).start()
def greet(name):
    return "Hello " + name + "!"
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
demo.launch()