File size: 3,590 Bytes
a5603d3
7187a61
 
a5603d3
0aa83ad
ee7844e
 
 
7187a61
 
 
a5603d3
 
7187a61
d2beb75
 
a5603d3
6c5f294
 
714224b
 
6c5f294
 
 
 
 
 
 
 
 
 
 
 
0aa83ad
 
ee7844e
 
 
0aa83ad
 
6c5f294
 
714224b
 
6c5f294
 
 
714224b
 
 
 
 
 
 
8010cdb
714224b
 
6c5f294
8010cdb
714224b
6c5f294
 
 
 
7187a61
79a1edd
 
6c5f294
d2beb75
 
 
 
 
714224b
d2beb75
 
 
 
 
 
 
79a1edd
 
6c5f294
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79a1edd
 
6c5f294
 
 
 
 
 
 
 
 
3b06990
 
6c5f294
 
 
 
 
 
 
 
 
 
 
3b06990
 
6c5f294
3b06990
79a1edd
7187a61
 
 
d2beb75
7187a61
d2beb75
 
 
 
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
import discord
import os
import threading
from discord.ext import commands
import json
import time
import matplotlib.pyplot as plt
from io import BytesIO
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

xp_data = {}


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


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


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


@bot.event
async def on_message(message):
    global xp_data
    
    if message.author.bot:
        return

    if message.author.id not in xp_data:
        xp_data[message.author.id] = 0
        
    old = xp_data[message.author.id]
    new = old + XP_PER_MESSAGE
    xp_data[message.author.id] = new
    level = calculate_level(new)
    
    print(f"{message.author.mention} xp: {xp_data[message.author.id]}")
    print(f"{message.author.mention} level: {level}")
    save_xp_data(xp_data)
    



def calculate_level(xp):
    return int(xp ** (1.0 / 3.0)) # 100k messages = lvl 100, good for super long term plan


@bot.command()
async def level(ctx):
    global xp_data
    print(ctx.author.id)
    print(ctx.author.mention)
    if ctx.author.id == 811235357663297546: # lunarflu
        if ctx.author.id in xp_data:

            xp = xp_data[ctx.author.id]
            level = calculate_level(xp)

            await ctx.send(f'You are at level {level} with {xp} total XP.')
            # progress bar with ascii? could be nice, easy
        else:
            await ctx.send('You have not earned any XP yet.')


@bot.command()
async def plot_xp(ctx):
    if ctx.author.id == 811235357663297546:
        author_id = str(ctx.author.id)
        if author_id in xp_data:
            timestamps, xp_values = zip(*xp_data[author_id])
            plt.plot(timestamps, xp_values)
            plt.xlabel('Timestamp')
            plt.ylabel('XP')
            plt.title('XP Over Time')
            
            image_stream = BytesIO()
            plt.savefig(image_stream, format='png')
            plt.close()
            
            image_stream.seek(0)
            await ctx.send(file=discord.File(fp=image_stream, filename='xp_graph.png'))
        else:
            await ctx.send('You have not earned any XP yet.')


@bot.command()
async def show_xp_data(ctx):
    if ctx.author.id == 811235357663297546:
        author_id = str(ctx.author.id)
        if author_id in xp_data:
            xp = xp_data[author_id]
            await ctx.send(f'Your XP data: {xp}')
        else:
            await ctx.send('You have not earned any XP yet.')


@bot.command()
async def load_xp(ctx):
    if ctx.author.id == 811235357663297546:
        try:
            xp_data.clear()  # Clear current XP data
            with open('xp_data.json', 'r') as f:
                loaded_data = json.load(f)
                xp_data.update(loaded_data)  # Update with loaded data
            await ctx.send('XP data has been loaded from the file.')
        except FileNotFoundError:
            await ctx.send('No XP data file found.')


    


        
""""""
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()