lunarflu HF staff commited on
Commit
6c5f294
1 Parent(s): c073764

revert to levelbot

Browse files
Files changed (1) hide show
  1. app.py +79 -41
app.py CHANGED
@@ -15,6 +15,20 @@ DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
15
  intents = discord.Intents.all()
16
  bot = commands.Bot(command_prefix='!', intents=intents)
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
 
20
  @bot.event
@@ -22,62 +36,86 @@ async def on_ready():
22
  print(f'Logged in as {bot.user.name}')
23
 
24
 
25
- @bot.command()
26
- async def save_messages(ctx, channel_id: int):
27
- if ctx.author.id == 811235357663297546:
28
- channel = bot.get_channel(channel_id)
29
- if not channel:
30
- await ctx.send("Channel not found.")
31
- return
32
 
33
- messages = []
34
- async for message in channel.history(limit=None):
35
- messages.append(message)
36
 
37
- with open("gradio-questions.json", 'w', encoding='utf-8') as file:
38
- for message in messages:
39
- file.write(f"{message.content}\n")
40
 
41
- await ctx.send(f"Messages from {channel.name} saved to gradio-questions.json")
42
-
43
- with open("gradio-questions.json", 'rb') as file:
44
- discord_file = discord.File(file)
45
- await ctx.send(file=discord_file)
46
 
47
 
48
  @bot.command()
49
- async def save_forum(ctx, channel_id: int):
50
- if ctx.author.id == 811235357663297546:
51
- channel = bot.get_channel(channel_id)
52
- threads = channel.threads
53
- messages = []
54
- for thread in threads:
55
- #messages.append(thread.name)
56
- async for message in thread.history(limit=None):
57
- messages.append(message)
58
-
59
- messages = messages.reverse()
60
- print(messages) # debug
61
-
62
- with open("gradio-questions.json", 'w', encoding='utf-8') as file:
63
- for message in messages:
64
- file.write(f"{message.content}\n")
65
-
66
- await ctx.send(f"Messages from {channel.name} saved to gradio-questions.json")
67
-
68
- with open("gradio-questions.json", 'rb') as file:
69
- discord_file = discord.File(file)
70
- await ctx.send(file=discord_file)
71
 
72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
 
 
 
 
 
 
 
 
 
 
75
 
76
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
 
 
79
 
80
-
81
 
82
 
83
  """"""
 
15
  intents = discord.Intents.all()
16
  bot = commands.Bot(command_prefix='!', intents=intents)
17
 
18
+ XP_PER_MESSAGE = 10
19
+
20
+
21
+ def load_xp_data():
22
+ try:
23
+ with open('xp_data.json', 'r') as f:
24
+ return json.load(f)
25
+ except FileNotFoundError:
26
+ return {}
27
+
28
+
29
+ def save_xp_data(xp_data):
30
+ with open('xp_data.json', 'w') as f:
31
+ json.dump(xp_data, f)
32
 
33
 
34
  @bot.event
 
36
  print(f'Logged in as {bot.user.name}')
37
 
38
 
39
+ @bot.event
40
+ async def on_message(message):
41
+ if message.author.bot:
42
+ return
43
+
44
+ author_id = str(message.author.id)
 
45
 
46
+ xp_data.setdefault(author_id, [])
47
+ timestamp = int(time.time())
48
+ xp_data[author_id].append((timestamp, XP_PER_MESSAGE))
49
 
50
+ save_xp_data(xp_data)
 
 
51
 
52
+ await bot.process_commands(message)
53
+
54
+
55
+ def calculate_level(xp):
56
+ return int(xp ** (1.0 / 3.0)) # 100k messages = lvl 100, good for super long term plan
57
 
58
 
59
  @bot.command()
60
+ async def level(ctx):
61
+ if ctx.author.id == 811235357663297546: # lunarflu
62
+ author_id = str(ctx.author.id)
63
+ if author_id in xp_data:
64
+ total_xp = sum(xp for _, xp in xp_data[author_id])
65
+ level = calculate_level(total_xp)
66
+ await ctx.send(f'You are at level {level} with {total_xp} total XP.')
67
+ # progress bar with ascii? could be nice, easy
68
+ else:
69
+ await ctx.send('You have not earned any XP yet.')
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
 
72
+ @bot.command()
73
+ async def plot_xp(ctx):
74
+ if ctx.author.id == 811235357663297546:
75
+ author_id = str(ctx.author.id)
76
+ if author_id in xp_data:
77
+ timestamps, xp_values = zip(*xp_data[author_id])
78
+ plt.plot(timestamps, xp_values)
79
+ plt.xlabel('Timestamp')
80
+ plt.ylabel('XP')
81
+ plt.title('XP Over Time')
82
+
83
+ image_stream = BytesIO()
84
+ plt.savefig(image_stream, format='png')
85
+ plt.close()
86
+
87
+ image_stream.seek(0)
88
+ await ctx.send(file=discord.File(fp=image_stream, filename='xp_graph.png'))
89
+ else:
90
+ await ctx.send('You have not earned any XP yet.')
91
 
92
 
93
+ @bot.command()
94
+ async def show_xp_data(ctx):
95
+ if ctx.author.id == 811235357663297546:
96
+ author_id = str(ctx.author.id)
97
+ if author_id in xp_data:
98
+ xp = xp_data[author_id]
99
+ await ctx.send(f'Your XP data: {xp}')
100
+ else:
101
+ await ctx.send('You have not earned any XP yet.')
102
 
103
 
104
+ @bot.command()
105
+ async def load_xp(ctx):
106
+ if ctx.author.id == 811235357663297546:
107
+ try:
108
+ xp_data.clear() # Clear current XP data
109
+ with open('xp_data.json', 'r') as f:
110
+ loaded_data = json.load(f)
111
+ xp_data.update(loaded_data) # Update with loaded data
112
+ await ctx.send('XP data has been loaded from the file.')
113
+ except FileNotFoundError:
114
+ await ctx.send('No XP data file found.')
115
 
116
 
117
+
118
 
 
119
 
120
 
121
  """"""