import discord import os import threading from discord.ext import commands import json import time import matplotlib.pyplot as plt from io import BytesIO 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 def load_xp_data(): try: with open('xp_data.json', 'r') as f: return json.load(f) except FileNotFoundError: return {} def save_xp_data(xp_data): with open('xp_data.json', 'w') as f: json.dump(xp_data, f) @bot.event async def on_ready(): print(f'Logged in as {bot.user.name}') @bot.event async def on_message(message): if message.author.bot: return author_id = str(message.author.id) 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) def calculate_level(xp): return int(xp ** (1.0 / 3.0)) # 100k messages = lvl 100, good for super long term plan @bot.command() async def level(ctx): if ctx.author.id == 811235357663297546: # lunarflu 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.') # progress bar with ascii? could be nice, easy else: await ctx.send('You have not earned any XP yet.') @bot.command() async def plot_xp(ctx): if ctx.author.id == 811235357663297546: author_id = str(ctx.author.id) if author_id in xp_data: timestamps, xp_values = zip(*xp_data[author_id]) plt.plot(timestamps, xp_values) plt.xlabel('Timestamp') plt.ylabel('XP') plt.title('XP Over Time') image_stream = BytesIO() plt.savefig(image_stream, format='png') plt.close() image_stream.seek(0) await ctx.send(file=discord.File(fp=image_stream, filename='xp_graph.png')) else: await ctx.send('You have not earned any XP yet.') @bot.command() async def show_xp_data(ctx): if ctx.author.id == 811235357663297546: author_id = str(ctx.author.id) if author_id in xp_data: xp = xp_data[author_id] await ctx.send(f'Your XP data: {xp}') else: await ctx.send('You have not earned any XP yet.') @bot.command() async def load_xp(ctx): if ctx.author.id == 811235357663297546: try: xp_data.clear() # Clear current XP data with open('xp_data.json', 'r') as f: loaded_data = json.load(f) xp_data.update(loaded_data) # Update with loaded data await ctx.send('XP data has been loaded from the file.') except FileNotFoundError: await ctx.send('No XP data file found.') """""" 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()