lunarflu HF staff commited on
Commit
ee7844e
1 Parent(s): 369bea2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -127
app.py CHANGED
@@ -1,28 +1,16 @@
1
  import discord
2
- import os
3
- import threading
4
  from discord.ext import commands
5
  import json
 
 
 
6
 
7
- import gradio_client
8
- import gradio as gr
9
- from gradio_client import Client
10
 
11
- DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
12
- intents = discord.Intents.all()
13
  bot = commands.Bot(command_prefix='!', intents=intents)
14
 
15
-
16
-
17
- """"""
18
  XP_PER_MESSAGE = 10
19
- """"""
20
-
21
-
22
- @bot.event
23
- async def on_ready():
24
- print(f'Logged in as {bot.user.name}')
25
-
26
 
27
  def load_xp_data():
28
  try:
@@ -30,34 +18,33 @@ def load_xp_data():
30
  return json.load(f)
31
  except FileNotFoundError:
32
  return {}
33
-
34
-
35
- def save_xp_data():
36
  with open('xp_data.json', 'w') as f:
37
  json.dump(xp_data, f)
38
 
 
 
 
39
 
40
  @bot.event
41
  async def on_message(message):
42
- try:
43
- #if message.author != bot.user: # disabling this means we can track bot usage over time
44
- """AWAIT LEVEL ALGORITM OR SOMETHING (MULTIPLE FILES?)"""
45
- author_id = str(message.author.id) # dictionary pairs (ID -> TOTAL XP)
46
- xp_data.setdefault(author_id, [])
47
- timestamp = int(time.time())
48
- xp_data[author_id].append((timestamp, XP_PER_MESSAGE))
49
- save_xp_data(xp_data)
50
- await bot.process_commands(message)
51
-
52
- except Exception as e:
53
- print(f"Error: {e}")
54
-
55
 
56
- # Calculate the level based on XP
57
  def calculate_level(xp):
58
  return int(xp ** (1.0 / 3.0))
59
 
60
-
61
  @bot.command()
62
  async def level(ctx):
63
  author_id = str(ctx.author.id)
@@ -68,99 +55,32 @@ async def level(ctx):
68
  else:
69
  await ctx.send('You have not earned any XP yet.')
70
 
71
-
72
  @bot.command()
73
- async def backup_xp(ctx):
74
- #save_xp_data()
75
- with open('xp_data.json', 'rb') as f:
76
- await ctx.send(file=discord.File(f, 'xp_data.json'))
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
- @bot.command()
98
- async def count_messages(ctx, channel_name: str):
99
- """Count messages per user in a specific channel."""
100
- channel = discord.utils.get(ctx.guild.text_channels, name=channel_name)
101
-
102
- if not channel:
103
- await ctx.send("Channel not found.")
104
- return
105
-
106
- message_counts = {}
107
- async for message in channel.history(limit=None):
108
- #if not message.author.bot:
109
- message_counts[message.author] = message_counts.get(message.author, 0) + 1
110
-
111
- sorted_users = sorted(message_counts.items(), key=lambda x: x[1], reverse=True)
112
- top_list = "\n".join([f"{member.name}: {count}" for member, count in sorted_users])
113
- await ctx.send(f"Message count per user in #{channel_name}:\n{top_list}")
114
-
115
 
116
  @bot.command()
117
- async def count(ctx):
118
- """Count messages per user in a specific channel."""
119
- message_counts = {}
120
-
121
- for channel in ctx.guild.text_channels:
122
- try:
123
- async for message in channel.history(limit=None):
124
- message_counts[message.author] = message_counts.get(message.author, 0) + 1
125
- except discord.Forbidden:
126
- # Handle the Forbidden error
127
- await ctx.send(f"Missing access to read messages in {channel.name}")
128
-
129
- sorted_users = sorted(message_counts.items(), key=lambda x: x[1], reverse=True)
130
- top_list = "\n".join([f"{member.name}: {count}" for member, count in sorted_users])
131
- await ctx.send(f"Message count per user in all text channels:\n{top_list}")
132
-
133
-
134
- """
135
- channel = ctx.channel
136
-
137
- if not channel:
138
- await ctx.send("Channel not found.")
139
- return
140
-
141
- message_counts = {}
142
- async for message in channel.history(limit=None):
143
- #if not message.author.bot:
144
- message_counts[message.author] = message_counts.get(message.author, 0) + 1
145
-
146
- sorted_users = sorted(message_counts.items(), key=lambda x: x[1], reverse=True)
147
- top_list = "\n".join([f"{member.name}: {count}" for member, count in sorted_users])
148
- await ctx.send(f"Message count per user:\n{top_list}")
149
-
150
-
151
- """
152
-
153
-
154
-
155
-
156
 
157
-
158
- """"""
159
- DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
160
- def run_bot():
161
- bot.run(DISCORD_TOKEN)
162
- threading.Thread(target=run_bot).start()
163
- def greet(name):
164
- return "Hello " + name + "!"
165
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
166
- demo.launch()
 
1
  import discord
 
 
2
  from discord.ext import commands
3
  import json
4
+ import time
5
+ import matplotlib.pyplot as plt
6
+ from io import BytesIO
7
 
8
+ intents = discord.Intents.default()
9
+ intents.message_content = True
 
10
 
 
 
11
  bot = commands.Bot(command_prefix='!', intents=intents)
12
 
 
 
 
13
  XP_PER_MESSAGE = 10
 
 
 
 
 
 
 
14
 
15
  def load_xp_data():
16
  try:
 
18
  return json.load(f)
19
  except FileNotFoundError:
20
  return {}
21
+
22
+ def save_xp_data(xp_data):
 
23
  with open('xp_data.json', 'w') as f:
24
  json.dump(xp_data, f)
25
 
26
+ @bot.event
27
+ async def on_ready():
28
+ print(f'Logged in as {bot.user.name}')
29
 
30
  @bot.event
31
  async def on_message(message):
32
+ if message.author.bot:
33
+ return
34
+
35
+ author_id = str(message.author.id)
36
+
37
+ xp_data.setdefault(author_id, [])
38
+ timestamp = int(time.time())
39
+ xp_data[author_id].append((timestamp, XP_PER_MESSAGE))
40
+
41
+ save_xp_data(xp_data)
42
+
43
+ await bot.process_commands(message)
 
44
 
 
45
  def calculate_level(xp):
46
  return int(xp ** (1.0 / 3.0))
47
 
 
48
  @bot.command()
49
  async def level(ctx):
50
  author_id = str(ctx.author.id)
 
55
  else:
56
  await ctx.send('You have not earned any XP yet.')
57
 
 
58
  @bot.command()
59
+ async def plot_xp(ctx):
60
+ author_id = str(ctx.author.id)
61
+ if author_id in xp_data:
62
+ timestamps, xp_values = zip(*xp_data[author_id])
63
+ plt.plot(timestamps, xp_values)
64
+ plt.xlabel('Timestamp')
65
+ plt.ylabel('XP')
66
+ plt.title('XP Over Time')
67
+
68
+ image_stream = BytesIO()
69
+ plt.savefig(image_stream, format='png')
70
+ plt.close()
71
+
72
+ image_stream.seek(0)
73
+ await ctx.send(file=discord.File(fp=image_stream, filename='xp_graph.png'))
74
+ else:
75
+ await ctx.send('You have not earned any XP yet.')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
77
  @bot.command()
78
+ async def show_xp_data(ctx):
79
+ author_id = str(ctx.author.id)
80
+ if author_id in xp_data:
81
+ xp = xp_data[author_id]
82
+ await ctx.send(f'Your XP data: {xp}')
83
+ else:
84
+ await ctx.send('You have not earned any XP yet.')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
+ bot.run('YOUR_BOT_TOKEN')