Spaces:
Running on CPU Upgrade

File size: 2,227 Bytes
a5603d3
 
0aa83ad
ee7844e
 
 
a5603d3
ee7844e
 
a5603d3
 
 
0aa83ad
 
369bea2
 
 
 
 
 
ee7844e
 
0aa83ad
 
 
ee7844e
 
 
0aa83ad
a5603d3
 
ee7844e
 
 
 
 
 
 
 
 
 
 
 
b7da67d
0aa83ad
b7da67d
0aa83ad
 
 
 
 
369bea2
 
 
0aa83ad
 
74bc7fc
b7da67d
ee7844e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d250f7
c67eb35
ee7844e
 
 
 
 
 
 
74bc7fc
ee7844e
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
import discord
from discord.ext import commands
import json
import time
import matplotlib.pyplot as plt
from io import BytesIO

intents = discord.Intents.default()
intents.message_content = True

bot = commands.Bot(command_prefix='!', intents=intents)

XP_PER_MESSAGE = 10

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):
    if message.author.bot:
        return
    
    author_id = str(message.author.id)
    
    xp_data.setdefault(author_id, [])
    timestamp = int(time.time())
    xp_data[author_id].append((timestamp, XP_PER_MESSAGE))
    
    save_xp_data(xp_data)
    
    await bot.process_commands(message)

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

@bot.command()
async def level(ctx):
    author_id = str(ctx.author.id)
    if author_id in xp_data:
        total_xp = sum(xp for _, xp in xp_data[author_id])
        level = calculate_level(total_xp)
        await ctx.send(f'You are at level {level} with {total_xp} total XP.')
    else:
        await ctx.send('You have not earned any XP yet.')

@bot.command()
async def plot_xp(ctx):
    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):
    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.run('YOUR_BOT_TOKEN')