Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Synced repo using 'sync_with_huggingface' Github Action
Browse files
app.py
CHANGED
@@ -27,15 +27,7 @@ with open(file_path, 'w') as json_file:
|
|
27 |
gspread_bot = gspread.service_account(filename='service_account.json')
|
28 |
worksheet = gspread_bot.open("levelbot").sheet1
|
29 |
""""""
|
30 |
-
|
31 |
-
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
32 |
-
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
33 |
-
"""
|
34 |
-
def query(payload):
|
35 |
-
response = requests.post(API_URL, headers=headers, json=payload)
|
36 |
-
return response.json()
|
37 |
-
"""
|
38 |
-
|
39 |
""""""
|
40 |
|
41 |
@bot.event
|
@@ -44,6 +36,10 @@ async def on_ready():
|
|
44 |
print(f"XP_PER_MESSAGE: {XP_PER_MESSAGE}")
|
45 |
|
46 |
|
|
|
|
|
|
|
|
|
47 |
def calculate_level(xp):
|
48 |
return int(xp ** (1.0 / 3.0))
|
49 |
|
@@ -52,336 +48,83 @@ def calculate_xp(level):
|
|
52 |
return (int(level ** 3))
|
53 |
|
54 |
|
55 |
-
|
56 |
-
async def on_message(message):
|
57 |
try:
|
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 |
-
10: lvl10,
|
96 |
-
11: lvl11,
|
97 |
-
12: lvl12,
|
98 |
-
13: lvl13,
|
99 |
-
14: lvl14,
|
100 |
-
15: lvl15,
|
101 |
-
}
|
102 |
-
|
103 |
-
# does a record already exist?
|
104 |
-
cell = worksheet.find(str(message.author.id))
|
105 |
-
length = len(worksheet.col_values(1))
|
106 |
-
if cell is None:
|
107 |
-
print(f"creating new record for {message.author}")
|
108 |
-
# if not, create new record
|
109 |
-
string_member_id = str(message.author.id)
|
110 |
-
xp = 10
|
111 |
-
new_level = calculate_level(xp)
|
112 |
-
message_author_name = message.author.name
|
113 |
-
worksheet.update(values=[[string_member_id, message_author_name, xp, new_level]], range_name=f'A{length+1}:D{length+1}')
|
114 |
-
else:
|
115 |
-
if cell:
|
116 |
-
# if so, update that row...
|
117 |
-
# update exp, can only be in a positive direction
|
118 |
-
# read
|
119 |
-
xp = worksheet.cell(cell.row, cell.col+2).value
|
120 |
-
xp = int(xp) + XP_PER_MESSAGE
|
121 |
-
current_level = calculate_level(xp)
|
122 |
-
print(f"updating record for {message.author}: {xp} xp")
|
123 |
-
# write with added xp
|
124 |
-
worksheet.update(values=[[xp, current_level]], range_name=f'C{cell.row}:D{cell.row}')
|
125 |
-
# initial role assignment
|
126 |
-
if current_level == 1:
|
127 |
-
if lvl1 not in message.author.roles:
|
128 |
-
await message.author.add_roles(lvl1)
|
129 |
-
print(f"Gave {message.author} {lvl1}")
|
130 |
-
# level up
|
131 |
-
elif current_level >= 2 and current_level <=15:
|
132 |
-
current_role = lvls[current_level]
|
133 |
-
if current_role not in message.author.roles:
|
134 |
-
await message.author.add_roles(current_role)
|
135 |
-
print(f"Gave {message.author} {current_role}")
|
136 |
-
await message.author.remove_roles(lvls[current_level -1])
|
137 |
-
print(f"Removed {lvls[current_level -1]} from {message.author}")
|
138 |
-
|
139 |
-
|
140 |
-
"""
|
141 |
-
value = cell.value
|
142 |
-
row_number = cell.row
|
143 |
-
column_number = cell.col
|
144 |
-
"""
|
145 |
-
except Exception as e:
|
146 |
-
print(f"Error: {e}")
|
147 |
-
|
148 |
-
await bot.process_commands(message)
|
149 |
-
|
150 |
-
except Exception as e:
|
151 |
-
print(f"Error: {e}")
|
152 |
-
|
153 |
-
|
154 |
-
@bot.command()
|
155 |
-
async def level(ctx):
|
156 |
-
if ctx.author.id == 811235357663297546:
|
157 |
-
|
158 |
-
try:
|
159 |
-
user_data = []
|
160 |
-
|
161 |
-
for author_id_str, xp_value in xp_data.items():
|
162 |
-
try:
|
163 |
-
current_level = calculate_level(xp_value)
|
164 |
-
author_id = int(author_id_str)
|
165 |
-
user = bot.get_user(author_id)
|
166 |
-
user_data.append((user, xp_value, current_level))
|
167 |
-
|
168 |
-
except Exception as e:
|
169 |
-
print(f"Error for user {author_id}: {e}")
|
170 |
-
|
171 |
-
sorted_user_data = sorted(user_data, key=lambda x: x[1], reverse=True)
|
172 |
-
|
173 |
-
for user, xp, level in sorted_user_data:
|
174 |
-
print(f"user: {user} | xp: {xp} | level: {level}")
|
175 |
-
|
176 |
-
except Exception as e:
|
177 |
-
print(f"Error: {e}")
|
178 |
-
"""
|
179 |
-
if author_id in xp_data:
|
180 |
-
xp = xp_data[author_id]
|
181 |
-
level = calculate_level(xp)
|
182 |
-
await ctx.send(f'You are at level {level} with {xp} XP.')
|
183 |
else:
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
#await ctx.send(f"Missing access to read messages in {channel.name}")
|
203 |
-
print(f"Missing access to read messages in {channel.name}")
|
204 |
-
|
205 |
-
sorted_users = sorted(message_counts.items(), key=lambda x: x[1], reverse=True)
|
206 |
-
top_list = "\n".join([f"{member.name}: {count}" for member, count in sorted_users[:50]])
|
207 |
-
#await ctx.send(f"Message count per user in all text channels:\n{top_list}")
|
208 |
-
print(f"Message count per user in all text channels:\n{top_list}")
|
209 |
-
|
210 |
-
|
211 |
-
@bot.command()
|
212 |
-
async def count_messages1(ctx):
|
213 |
-
if ctx.author.id == 811235357663297546:
|
214 |
-
end_date = datetime.datetime.utcnow() # Current date and time
|
215 |
-
start_date = end_date - datetime.timedelta(days=1)
|
216 |
-
message_count = 0
|
217 |
-
for channel in ctx.guild.text_channels:
|
218 |
-
try:
|
219 |
-
async for message in channel.history(limit=None, after=start_date, before=end_date):
|
220 |
-
message_count += 1
|
221 |
-
except discord.Forbidden:
|
222 |
-
print(f"Missing access to read messages in {channel.name}")
|
223 |
-
print(f'Total messages between {start_date} and {end_date}: {message_count}')
|
224 |
-
|
225 |
-
|
226 |
-
@bot.command()
|
227 |
-
async def count_messages7(ctx):
|
228 |
-
if ctx.author.id == 811235357663297546:
|
229 |
-
end_date = datetime.datetime.utcnow() # Current date and time
|
230 |
-
start_date = end_date - datetime.timedelta(days=7)
|
231 |
-
message_count = 0
|
232 |
-
for channel in ctx.guild.text_channels:
|
233 |
-
try:
|
234 |
-
async for message in channel.history(limit=None, after=start_date, before=end_date):
|
235 |
-
message_count += 1
|
236 |
-
except discord.Forbidden:
|
237 |
-
print(f"Missing access to read messages in {channel.name}")
|
238 |
-
print(f'Total messages between {start_date} and {end_date}: {message_count}')
|
239 |
-
|
240 |
-
|
241 |
-
@bot.command()
|
242 |
-
async def count_messages14(ctx):
|
243 |
-
if ctx.author.id == 811235357663297546:
|
244 |
-
end_date = datetime.datetime.utcnow() # Current date and time
|
245 |
-
start_date = end_date - datetime.timedelta(days=14)
|
246 |
-
message_count = 0
|
247 |
-
for channel in ctx.guild.text_channels:
|
248 |
-
try:
|
249 |
-
async for message in channel.history(limit=None, after=start_date, before=end_date):
|
250 |
-
message_count += 1
|
251 |
-
except discord.Forbidden:
|
252 |
-
print(f"Missing access to read messages in {channel.name}")
|
253 |
-
print(f'Total messages between {start_date} and {end_date}: {message_count}')
|
254 |
-
|
255 |
-
|
256 |
-
@bot.command()
|
257 |
-
async def count_messages30(ctx):
|
258 |
-
if ctx.author.id == 811235357663297546:
|
259 |
-
end_date = datetime.datetime.utcnow() # Current date and time
|
260 |
-
start_date = end_date - datetime.timedelta(days=30)
|
261 |
-
message_count = 0
|
262 |
-
for channel in ctx.guild.text_channels:
|
263 |
-
try:
|
264 |
-
async for message in channel.history(limit=None, after=start_date, before=end_date):
|
265 |
-
message_count += 1
|
266 |
-
except discord.Forbidden:
|
267 |
-
print(f"Missing access to read messages in {channel.name}")
|
268 |
-
print(f'Total messages between {start_date} and {end_date}: {message_count}')
|
269 |
-
|
270 |
-
|
271 |
-
@bot.command()
|
272 |
-
async def count_messages60(ctx):
|
273 |
-
if ctx.author.id == 811235357663297546:
|
274 |
-
end_date = datetime.datetime.utcnow() # Current date and time
|
275 |
-
start_date = end_date - datetime.timedelta(days=60)
|
276 |
-
message_count = 0
|
277 |
-
for channel in ctx.guild.text_channels:
|
278 |
-
try:
|
279 |
-
async for message in channel.history(limit=None, after=start_date, before=end_date):
|
280 |
-
message_count += 1
|
281 |
-
except discord.Forbidden:
|
282 |
-
print(f"Missing access to read messages in {channel.name}")
|
283 |
-
print(f'Total messages between {start_date} and {end_date}: {message_count}')
|
284 |
-
|
285 |
-
|
286 |
-
@bot.command()
|
287 |
-
async def count_messages(ctx, time: int):
|
288 |
-
if ctx.author.id == 811235357663297546:
|
289 |
-
end_date = datetime.datetime.utcnow() # Current date and time
|
290 |
-
start_date = end_date - datetime.timedelta(days=time)
|
291 |
-
message_count = 0
|
292 |
-
for channel in ctx.guild.text_channels:
|
293 |
-
print(channel.name)
|
294 |
-
try:
|
295 |
-
async for message in channel.history(limit=None, after=start_date, before=end_date):
|
296 |
-
message_count += 1
|
297 |
-
except discord.Forbidden:
|
298 |
-
print(f"Missing access to read messages in {channel.name}")
|
299 |
-
print(f'Total messages between {start_date} and {end_date}: {message_count}')
|
300 |
-
|
301 |
-
|
302 |
-
@bot.command()
|
303 |
-
async def count_threads(ctx, time: int):
|
304 |
-
if ctx.author.id == 811235357663297546:
|
305 |
-
|
306 |
-
end_date = datetime.datetime.utcnow() # Current date and time
|
307 |
-
start_date = end_date - datetime.timedelta(days=time)
|
308 |
-
thread_message_count = 0
|
309 |
-
|
310 |
-
for channel in ctx.guild.text_channels:
|
311 |
-
print(f"CHANNELNAME: {channel.name}")
|
312 |
-
for thread in channel.threads:
|
313 |
-
print(channel.name)
|
314 |
-
print(thread.name)
|
315 |
-
async for message in thread.history(limit=None, after=start_date, before=end_date):
|
316 |
-
thread_message_count += 1
|
317 |
-
print(f'Total thread_messages between {start_date} and {end_date}: {thread_message_count}')
|
318 |
-
|
319 |
-
|
320 |
-
@bot.command()
|
321 |
-
async def count_forum_threads(ctx, time: int):
|
322 |
-
if ctx.author.id == 811235357663297546:
|
323 |
-
end_date = datetime.datetime.utcnow() # Current date and time
|
324 |
-
start_date = end_date - datetime.timedelta(days=time)
|
325 |
-
forum_thread_message_count = 0
|
326 |
-
|
327 |
-
for forum in ctx.guild.forums:
|
328 |
-
print(f"FORUMNAME: {forum.name}")
|
329 |
-
for thread in forum.threads:
|
330 |
-
print(forum.name)
|
331 |
-
print(thread.name)
|
332 |
-
async for message in thread.history(limit=None, after=start_date, before=end_date):
|
333 |
-
print(f"THREAD NAME: {thread.name}")
|
334 |
-
forum_thread_message_count += 1
|
335 |
-
"""
|
336 |
-
async for thread in forum.archived_threads(limit=None, before=end_date):
|
337 |
-
print(f"ARCHIVED THREAD NAME: {thread.name}")
|
338 |
-
forum_thread_message_count += 1
|
339 |
-
"""
|
340 |
-
|
341 |
-
print(f'Total forum_thread_messages between {start_date} and {end_date}: {forum_thread_message_count}')
|
342 |
-
|
343 |
|
344 |
-
@bot.command()
|
345 |
-
async def top_gradio(ctx, channel_id):
|
346 |
-
if ctx.author.id == 811235357663297546:
|
347 |
-
message_counts = {}
|
348 |
-
try:
|
349 |
-
channel = await bot.fetch_channel(channel_id)
|
350 |
-
|
351 |
-
async for message in channel.history(limit=None):
|
352 |
-
message_counts[message.author] = message_counts.get(message.author, 0) + 1
|
353 |
-
except discord.Forbidden:
|
354 |
-
print(f"Missing access to read messages in {channel.name}")
|
355 |
-
|
356 |
-
sorted_users = sorted(message_counts.items(), key=lambda x: x[1], reverse=True)
|
357 |
-
top_list = "\n".join([f"{member.name}: {count}" for member, count in sorted_users[:50]])
|
358 |
-
print(f"Message count per user in the channel:\n{top_list}")
|
359 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
360 |
|
361 |
-
@bot.command()
|
362 |
-
async def top_gradio_threads(ctx, channel_id):
|
363 |
-
if ctx.author.id == 811235357663297546:
|
364 |
-
message_counts = {}
|
365 |
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
message_counts[message.author] = message_counts.get(message.author, 0) + 1
|
374 |
-
|
375 |
-
async for thread in channel.archived_threads(limit=None):
|
376 |
-
print(f"ARCHIVED Thread Name: {thread.name}, ARCHIVED Thread ID: {thread.id}, Parent ID: {thread.parent_id}")
|
377 |
-
async for message in thread.history(limit=None):
|
378 |
-
message_counts[message.author] = message_counts.get(message.author, 0) + 1
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
sorted_users = sorted(message_counts.items(), key=lambda x: x[1], reverse=True)
|
383 |
-
top_list = "\n".join([f"[{member.id}]{member.name}: {count}" for member, count in sorted_users[:50]])
|
384 |
-
print(f"Message count per user in the channel:\n{top_list}")
|
385 |
|
386 |
|
387 |
""""""
|
|
|
27 |
gspread_bot = gspread.service_account(filename='service_account.json')
|
28 |
worksheet = gspread_bot.open("levelbot").sheet1
|
29 |
""""""
|
30 |
+
bot_ids = [1136614989411655780, 1166392942387265536, 1158038249835610123, 1130774761031610388, 1155489509518098565, 1155169841276260546, 1152238037355474964, 1154395078735953930]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
""""""
|
32 |
|
33 |
@bot.event
|
|
|
36 |
print(f"XP_PER_MESSAGE: {XP_PER_MESSAGE}")
|
37 |
|
38 |
|
39 |
+
|
40 |
+
|
41 |
+
|
42 |
+
|
43 |
def calculate_level(xp):
|
44 |
return int(xp ** (1.0 / 3.0))
|
45 |
|
|
|
48 |
return (int(level ** 3))
|
49 |
|
50 |
|
51 |
+
async def add_exp(member):
|
|
|
52 |
try:
|
53 |
+
guild = bot.get_guild(879548962464493619)
|
54 |
+
lvl1 = guild.get_role(1171861537699397733)
|
55 |
+
lvl2 = guild.get_role(1171861595115245699)
|
56 |
+
lvl3 = guild.get_role(1171861626715115591)
|
57 |
+
lvl4 = guild.get_role(1171861657975259206)
|
58 |
+
lvl5 = guild.get_role(1171861686580412497)
|
59 |
+
lvl6 = guild.get_role(1171861900301172736)
|
60 |
+
lvl7 = guild.get_role(1171861936258941018)
|
61 |
+
lvl8 = guild.get_role(1171861968597024868)
|
62 |
+
lvl9 = guild.get_role(1171862009982242836)
|
63 |
+
lvl10 = guild.get_role(1164188093713223721)
|
64 |
+
lvl11 = guild.get_role(1171524944354607104)
|
65 |
+
lvl12 = guild.get_role(1171524990257082458)
|
66 |
+
lvl13 = guild.get_role(1171525021928263791)
|
67 |
+
lvl14 = guild.get_role(1171525062201966724)
|
68 |
+
lvl15 = guild.get_role(1171525098465918996)
|
69 |
+
lvls = {
|
70 |
+
1: lvl1, 2: lvl2, 3: lvl3, 4: lvl4, 5: lvl5, 6: lvl6, 7: lvl7, 8: lvl8, 9: lvl9, 10: lvl10,
|
71 |
+
11: lvl11, 12: lvl12, 13: lvl13, 14: lvl14, 15: lvl15,
|
72 |
+
}
|
73 |
+
#if member.id == 811235357663297546:
|
74 |
+
# does a record already exist?
|
75 |
+
cell = worksheet.find(str(member.id))
|
76 |
+
length = len(worksheet.col_values(1))
|
77 |
+
if cell is None:
|
78 |
+
print(f"creating new record for {member}")
|
79 |
+
# if not, create new record
|
80 |
+
string_member_id = str(member.id)
|
81 |
+
xp = 10
|
82 |
+
current_level = calculate_level(xp)
|
83 |
+
member_name = member.name
|
84 |
+
worksheet.update(values=[[string_member_id, member_name, xp, current_level]], range_name=f'A{length+1}:D{length+1}')
|
85 |
+
# initial role assignment
|
86 |
+
if current_level == 1:
|
87 |
+
if lvl1 not in member.roles:
|
88 |
+
await member.add_roles(lvl1)
|
89 |
+
print(f"Gave {member} {lvl1}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
else:
|
91 |
+
if cell:
|
92 |
+
# if so, update that row...
|
93 |
+
xp = worksheet.cell(cell.row, cell.col+2).value
|
94 |
+
xp = int(xp) + XP_PER_MESSAGE
|
95 |
+
current_level = calculate_level(xp)
|
96 |
+
print(f"updating record for {member}: {xp} xp")
|
97 |
+
# write with added xp
|
98 |
+
worksheet.update(values=[[xp, current_level]], range_name=f'C{cell.row}:D{cell.row}')
|
99 |
+
# level up
|
100 |
+
if current_level >= 2 and current_level <=15:
|
101 |
+
current_role = lvls[current_level]
|
102 |
+
if current_role not in member.roles:
|
103 |
+
await member.add_roles(current_role)
|
104 |
+
print(f"Gave {member} {current_role}")
|
105 |
+
await member.remove_roles(lvls[current_level-1])
|
106 |
+
print(f"Removed {lvls[current_level-1]} from {member}")
|
107 |
+
except Exception as e:
|
108 |
+
print(f"add_exp Error: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
|
111 |
+
@bot.event
|
112 |
+
async def on_message(message):
|
113 |
+
try:
|
114 |
+
if message.author.id not in bot_ids:
|
115 |
+
await add_exp(message.author)
|
116 |
+
await bot.process_commands(message)
|
117 |
+
except Exception as e:
|
118 |
+
print(f"on_message Error: {e}")
|
119 |
|
|
|
|
|
|
|
|
|
120 |
|
121 |
+
@bot.event
|
122 |
+
async def on_reaction_add(reaction, user):
|
123 |
+
try:
|
124 |
+
if user.id not in bot_ids:
|
125 |
+
await add_exp(user)
|
126 |
+
except Exception as e:
|
127 |
+
print(f"on_reaction_add Error: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
|
129 |
|
130 |
""""""
|