Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
| import discord | |
| import os | |
| import threading | |
| from discord.ext import commands | |
| import json | |
| import gradio_client | |
| import gradio as gr | |
| from gradio_client import Client | |
| DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None) | |
| intents = discord.Intents.all() | |
| bot = commands.Bot(command_prefix='!', intents=intents) | |
| """""" | |
| XP_PER_MESSAGE = 10 | |
| """""" | |
| async def on_ready(): | |
| print(f'Logged in as {bot.user.name}') | |
| def load_xp_data(): | |
| try: | |
| with open('xp_data.json', 'r') as f: | |
| return json.load(f) | |
| except FileNotFoundError: | |
| return {} | |
| def save_xp_data(): | |
| with open('xp_data.json', 'w') as f: | |
| json.dump(xp_data, f) | |
| async def on_message(message): | |
| try: | |
| #if message.author != bot.user: # disabling this means we can track bot usage over time | |
| """AWAIT LEVEL ALGORITM OR SOMETHING (MULTIPLE FILES?)""" | |
| author_id = str(message.author.id) # dictionary pairs (ID -> TOTAL XP) | |
| 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) | |
| except Exception as e: | |
| print(f"Error: {e}") | |
| # Calculate the level based on XP | |
| def calculate_level(xp): | |
| return int(xp ** (1.0 / 3.0)) | |
| 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.') | |
| async def backup_xp(ctx): | |
| #save_xp_data() | |
| with open('xp_data.json', 'rb') as f: | |
| await ctx.send(file=discord.File(f, 'xp_data.json')) | |
| async def count_messages(ctx, channel_name: str): | |
| """Count messages per user in a specific channel.""" | |
| channel = discord.utils.get(ctx.guild.text_channels, name=channel_name) | |
| if not channel: | |
| await ctx.send("Channel not found.") | |
| return | |
| message_counts = {} | |
| async for message in channel.history(limit=None): | |
| #if not message.author.bot: | |
| message_counts[message.author] = message_counts.get(message.author, 0) + 1 | |
| sorted_users = sorted(message_counts.items(), key=lambda x: x[1], reverse=True) | |
| top_list = "\n".join([f"{member.name}: {count}" for member, count in sorted_users]) | |
| await ctx.send(f"Message count per user in #{channel_name}:\n{top_list}") | |
| async def count(ctx): | |
| """Count messages per user in a specific channel.""" | |
| message_counts = {} | |
| for channel in ctx.guild.text_channels: | |
| try: | |
| async for message in channel.history(limit=None): | |
| message_counts[message.author] = message_counts.get(message.author, 0) + 1 | |
| except discord.Forbidden: | |
| # Handle the Forbidden error | |
| await ctx.send(f"Missing access to read messages in {channel.name}") | |
| sorted_users = sorted(message_counts.items(), key=lambda x: x[1], reverse=True) | |
| top_list = "\n".join([f"{member.name}: {count}" for member, count in sorted_users]) | |
| await ctx.send(f"Message count per user in all text channels:\n{top_list}") | |
| """ | |
| channel = ctx.channel | |
| if not channel: | |
| await ctx.send("Channel not found.") | |
| return | |
| message_counts = {} | |
| async for message in channel.history(limit=None): | |
| #if not message.author.bot: | |
| message_counts[message.author] = message_counts.get(message.author, 0) + 1 | |
| sorted_users = sorted(message_counts.items(), key=lambda x: x[1], reverse=True) | |
| top_list = "\n".join([f"{member.name}: {count}" for member, count in sorted_users]) | |
| await ctx.send(f"Message count per user:\n{top_list}") | |
| """ | |
| """""" | |
| DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None) | |
| def run_bot(): | |
| bot.run(DISCORD_TOKEN) | |
| threading.Thread(target=run_bot).start() | |
| def greet(name): | |
| return "Hello " + name + "!" | |
| demo = gr.Interface(fn=greet, inputs="text", outputs="text") | |
| demo.launch() |