lunarflu HF staff commited on
Commit
cf1e692
1 Parent(s): c7ed7f7
Files changed (1) hide show
  1. app.py +71 -89
app.py CHANGED
@@ -1,130 +1,112 @@
 
1
  import os
2
  import threading
 
 
3
 
4
- import discord
5
- import matplotlib.pyplot as plt
6
- from io import BytesIO
7
  import gradio_client
8
  import gradio as gr
9
  from gradio_client import Client
10
- import json
11
- import time
12
- from discord import app_commands
13
- from discord.ext import commands
14
-
15
-
16
 
17
- # HF GUILD SETTINGS
18
- MY_GUILD_ID = 879548962464493619
19
- MY_GUILD = discord.Object(id=MY_GUILD_ID)
20
  DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
 
 
21
 
22
 
23
- class Bot(commands.Bot):
24
- """This structure allows slash commands to work instantly."""
25
-
26
- def __init__(self):
27
- super().__init__(command_prefix="/", intents=discord.Intents.all())
28
-
29
- async def setup_hook(self):
30
- await self.tree.sync(guild=discord.Object(MY_GUILD_ID))
31
- print(f"Synced slash commands for {self.user}.")
32
 
 
 
 
33
 
34
- client = Bot()
35
 
 
 
 
36
 
37
 
38
- XP_PER_MESSAGE = 10
39
- xp_data = {}
 
 
 
40
 
41
-
42
- @client.event
43
- async def on_ready():
44
- print(f"Logged in as {client.user} (ID: {client.user.id})")
45
- print("------")
46
 
47
 
48
- @client.event
49
  async def on_message(message):
50
  try:
51
- global xp_data
52
-
53
- if message.author.bot:
54
- return
55
-
56
- if message.author.id not in xp_data:
57
- xp_data[message.author.id] = 0
58
 
59
- old = xp_data[message.author.id]
60
- new = old + XP_PER_MESSAGE
61
- xp_data[message.author.id] = new
62
- level = calculate_level(new)
63
-
64
- print(f"{message.author} xp: {xp_data[message.author.id]}")
65
- print(f"{message.author} level: {level}")
66
- print(f"xp_data: {xp_data}")
67
-
68
 
69
  except Exception as e:
70
  print(f"Error: {e}")
71
 
72
-
 
73
  def calculate_level(xp):
74
- return int(xp ** (1.0 / 3.0)) # 100k messages = lvl 100, good for super long term plan
75
 
76
 
77
- @client.command()
78
  async def level(ctx):
79
- global xp_data
80
- print(ctx.author.id)
81
- print(ctx.author.mention)
82
- if ctx.author.id == 811235357663297546:
83
- if ctx.author.id in xp_data:
84
- print(f"{ctx.author} is in xp_data")
85
- xp = xp_data[ctx.author.id]
86
- level = calculate_level(xp)
87
-
88
- await ctx.send(f'You are at level {level} with {xp} total XP.')
89
- # progress bar with ascii? could be nice, easy
90
- else:
91
- await ctx.send('You have not earned any XP yet.')
92
-
93
-
94
-
95
-
96
-
97
 
98
 
99
- @client.hybrid_command(name="levelbot", with_app_command=True, description="check level")
100
- @app_commands.guilds(MY_GUILD)
101
- async def levelbot(ctx):
102
- try:
103
- await ctx.send(f'test')
104
- except Exception as e:
105
- print(f"Error: {e}")
 
 
 
 
 
 
106
 
107
 
108
 
 
 
 
 
 
 
 
 
 
109
 
110
 
111
 
112
-
113
-
114
 
115
 
 
 
 
116
  def run_bot():
117
- client.run(DISCORD_TOKEN)
118
-
119
-
120
  threading.Thread(target=run_bot).start()
121
- """This allows us to run the Discord bot in a Python thread"""
122
- with gr.Blocks() as demo:
123
- gr.Markdown("""
124
- # Huggingbots Server
125
- This space hosts the huggingbots discord bot.
126
- Currently supported models are Falcon and DeepfloydIF
127
- """)
128
- demo.queue(concurrency_count=100)
129
- demo.queue(max_size=100)
130
- demo.launch()
 
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
+ try:
28
+ with open('xp_data.json', 'r') as f:
29
+ xp_data = json.load(f)
30
+ except FileNotFoundError:
31
+ xp_data = {}
32
 
33
+
34
+ def save_xp_data():
35
+ with open('xp_data.json', 'w') as f:
36
+ json.dump(xp_data, f)
 
37
 
38
 
39
+ @bot.event
40
  async def on_message(message):
41
  try:
42
+ if message.author != bot.user:
43
+ """AWAIT LEVEL ALGORITM OR SOMETHING (MULTIPLE FILES?)"""
44
+ author_id = str(message.author.id) # dictionary pairs (ID -> TOTAL XP)
45
+ xp_data.setdefault(author_id, 0) # default if it doesn't already exist
 
 
 
46
 
47
+ xp_data[author_id] += XP_PER_MESSAGE
48
+ save_xp_data()
49
+
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)
64
+ if author_id in xp_data:
65
+ xp = xp_data[author_id]
66
+ level = calculate_level(xp)
67
+ await ctx.send(f'You are at level {level} with {xp} XP.')
68
+ else:
69
+ await ctx.send('You have not earned any XP yet.')
 
 
 
 
 
 
 
 
 
 
 
70
 
71
 
72
+ @bot.command()
73
+ async def top_users(ctx, limit: int = 10):
74
+ """Get the top users with the highest message counts."""
75
+ message_counts = {}
76
+ channel = discord.utils.get(ctx.guild.text_channels, name="general") # Replace with your channel name
77
+
78
+ async for message in channel.history(limit=None):
79
+ if not message.author.bot:
80
+ message_counts[message.author] = message_counts.get(message.author, 0) + 1
81
+
82
+ sorted_users = sorted(message_counts.items(), key=lambda x: x[1], reverse=True)
83
+ top_list = "\n".join([f"{member.name}: {count}" for member, count in sorted_users[:limit]])
84
+ await ctx.send(f"Top {limit} users by message count:\n{top_list}")
85
 
86
 
87
 
88
+ """
89
+ for member in ctx.guild.members:
90
+ #if not member.bot:
91
+ message_counts[member] = sum(1 for _ in await ctx.history(user=member).flatten())
92
+
93
+ sorted_users = sorted(message_counts.items(), key=lambda x: x[1], reverse=True)
94
+ top_list = "\n".join([f"{member.name}: {count}" for member, count in sorted_users[:limit]])
95
+ await ctx.send(f"Top {limit} users by message count:\n{top_list}")
96
+ """
97
 
98
 
99
 
100
+
 
101
 
102
 
103
+
104
+ """"""
105
+ DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
106
  def run_bot():
107
+ bot.run(DISCORD_TOKEN)
 
 
108
  threading.Thread(target=run_bot).start()
109
+ def greet(name):
110
+ return "Hello " + name + "!"
111
+ demo = gr.Interface(fn=greet, inputs="text", outputs="text")
112
+ demo.launch()