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}') | |
try: | |
with open('xp_data.json', 'r') as f: | |
xp_data = json.load(f) | |
except FileNotFoundError: | |
xp_data = {} | |
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: | |
"""AWAIT LEVEL ALGORITM OR SOMETHING (MULTIPLE FILES?)""" | |
author_id = str(message.author.id) # dictionary pairs (ID -> TOTAL XP) | |
xp_data.setdefault(author_id, 0) # default if it doesn't already exist | |
xp_data[author_id] += XP_PER_MESSAGE | |
save_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 ** 0.5) | |
async def level(ctx): | |
author_id = str(ctx.author.id) | |
if author_id in xp_data: | |
xp = xp_data[author_id] | |
level = calculate_level(xp) | |
await ctx.send(f'You are at level {level} with {xp} XP.') | |
else: | |
await ctx.send('You have not earned any XP yet.') | |
"""""" | |
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() |