File size: 3,197 Bytes
cf1e692
7187a61
 
cf1e692
 
6d949e0
7187a61
 
 
a5603d3
7187a61
cf1e692
 
a5603d3
6c5f294
 
cf1e692
80a1e02
 
 
cf1e692
6c5f294
0aa83ad
cf1e692
 
 
80a1e02
 
 
 
6d949e0
cf1e692
 
 
 
 
6d949e0
cf1e692
 
 
 
0aa83ad
 
cf1e692
6c5f294
6d949e0
cf1e692
 
 
 
6d949e0
cf1e692
80a1e02
 
cf1e692
 
 
714224b
6d949e0
 
6c5f294
cf1e692
80a1e02
7187a61
79a1edd
cf1e692
6c5f294
cf1e692
 
 
 
 
 
 
c7ed7f7
 
cf1e692
 
 
 
 
 
 
 
 
 
 
 
 
c7ed7f7
 
 
cf1e692
 
 
 
 
 
 
 
 
c7ed7f7
 
 
cf1e692
c7ed7f7
 
cf1e692
 
 
6d949e0
cf1e692
7187a61
cf1e692
 
 
 
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
import discord
import os
import threading
from discord.ext import commands
import json

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
def calculate_level(xp):
    return int(xp ** (1.0 / 3.0))
""""""


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

try:
    with open('xp_data.json', 'r') as f:
        xp_data = json.load(f)
except FileNotFoundError:
    xp_data = {}

    
def save_xp_data():
    with open('xp_data.json', 'w') as f:
        json.dump(xp_data, f)


@bot.event
async def on_message(message):
    try:
        if message.author != bot.user:
            """AWAIT LEVEL ALGORITM OR SOMETHING (MULTIPLE FILES?)"""
            author_id = str(message.author.id) # dictionary pairs (ID -> TOTAL XP)
            xp_data.setdefault(author_id, 0) # default if it doesn't already exist
            
            xp_data[author_id] += XP_PER_MESSAGE
            print(f"xp_data: {xp_data}")
            
            save_xp_data()
            
            await bot.process_commands(message)

    except Exception as e:
        print(f"Error: {e}")

        



@bot.command()
async def level(ctx):
    author_id = str(ctx.author.id)
    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.')


@bot.command()
async def top_users(ctx, limit: int = 10):
    """Get the top users with the highest message counts."""
    message_counts = {}
    channel = discord.utils.get(ctx.guild.text_channels, name="general")  # Replace with your channel name
    
    async for message in channel.history(limit=None):
        if not message.author.bot:
            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.name}: {count}" for member, count in sorted_users[:limit]])
    await ctx.send(f"Top {limit} users by message count:\n{top_list}")



    """
    for member in ctx.guild.members:
        #if not member.bot:
        message_counts[member] = sum(1 for _ in await ctx.history(user=member).flatten())
    
    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[:limit]])
    await ctx.send(f"Top {limit} users by message count:\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()