Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
save_messages
Browse files
app.py
CHANGED
|
@@ -15,92 +15,27 @@ DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
|
|
| 15 |
intents = discord.Intents.all()
|
| 16 |
bot = commands.Bot(command_prefix='!', intents=intents)
|
| 17 |
|
| 18 |
-
XP_PER_MESSAGE = 10
|
| 19 |
|
| 20 |
-
def load_xp_data():
|
| 21 |
-
try:
|
| 22 |
-
with open('xp_data.json', 'r') as f:
|
| 23 |
-
return json.load(f)
|
| 24 |
-
except FileNotFoundError:
|
| 25 |
-
return {}
|
| 26 |
-
|
| 27 |
-
def save_xp_data(xp_data):
|
| 28 |
-
with open('xp_data.json', 'w') as f:
|
| 29 |
-
json.dump(xp_data, f)
|
| 30 |
|
| 31 |
@bot.event
|
| 32 |
async def on_ready():
|
| 33 |
print(f'Logged in as {bot.user.name}')
|
| 34 |
|
| 35 |
-
@bot.event
|
| 36 |
-
async def on_message(message):
|
| 37 |
-
if message.author.bot:
|
| 38 |
-
return
|
| 39 |
-
|
| 40 |
-
author_id = str(message.author.id)
|
| 41 |
-
|
| 42 |
-
xp_data.setdefault(author_id, [])
|
| 43 |
-
timestamp = int(time.time())
|
| 44 |
-
xp_data[author_id].append((timestamp, XP_PER_MESSAGE))
|
| 45 |
-
|
| 46 |
-
save_xp_data(xp_data)
|
| 47 |
-
|
| 48 |
-
await bot.process_commands(message)
|
| 49 |
-
|
| 50 |
-
def calculate_level(xp):
|
| 51 |
-
return int(xp ** (1.0 / 3.0))
|
| 52 |
|
| 53 |
@bot.command()
|
| 54 |
-
async def
|
| 55 |
-
|
| 56 |
-
if
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
await ctx.send(f'You are at level {level} with {total_xp} total XP.')
|
| 60 |
-
else:
|
| 61 |
-
await ctx.send('You have not earned any XP yet.')
|
| 62 |
-
|
| 63 |
-
@bot.command()
|
| 64 |
-
async def plot_xp(ctx):
|
| 65 |
-
author_id = str(ctx.author.id)
|
| 66 |
-
if author_id in xp_data:
|
| 67 |
-
timestamps, xp_values = zip(*xp_data[author_id])
|
| 68 |
-
plt.plot(timestamps, xp_values)
|
| 69 |
-
plt.xlabel('Timestamp')
|
| 70 |
-
plt.ylabel('XP')
|
| 71 |
-
plt.title('XP Over Time')
|
| 72 |
-
|
| 73 |
-
image_stream = BytesIO()
|
| 74 |
-
plt.savefig(image_stream, format='png')
|
| 75 |
-
plt.close()
|
| 76 |
-
|
| 77 |
-
image_stream.seek(0)
|
| 78 |
-
await ctx.send(file=discord.File(fp=image_stream, filename='xp_graph.png'))
|
| 79 |
-
else:
|
| 80 |
-
await ctx.send('You have not earned any XP yet.')
|
| 81 |
-
|
| 82 |
-
@bot.command()
|
| 83 |
-
async def show_xp_data(ctx):
|
| 84 |
-
author_id = str(ctx.author.id)
|
| 85 |
-
if author_id in xp_data:
|
| 86 |
-
xp = xp_data[author_id]
|
| 87 |
-
await ctx.send(f'Your XP data: {xp}')
|
| 88 |
-
else:
|
| 89 |
-
await ctx.send('You have not earned any XP yet.')
|
| 90 |
-
|
| 91 |
|
| 92 |
-
|
| 93 |
-
async def load_xp(ctx):
|
| 94 |
-
try:
|
| 95 |
-
xp_data.clear() # Clear current XP data
|
| 96 |
-
with open('xp_data.json', 'r') as f:
|
| 97 |
-
loaded_data = json.load(f)
|
| 98 |
-
xp_data.update(loaded_data) # Update with loaded data
|
| 99 |
-
await ctx.send('XP data has been loaded from the file.')
|
| 100 |
-
except FileNotFoundError:
|
| 101 |
-
await ctx.send('No XP data file found.')
|
| 102 |
|
|
|
|
|
|
|
|
|
|
| 103 |
|
|
|
|
| 104 |
|
| 105 |
|
| 106 |
|
|
|
|
| 15 |
intents = discord.Intents.all()
|
| 16 |
bot = commands.Bot(command_prefix='!', intents=intents)
|
| 17 |
|
|
|
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
@bot.event
|
| 21 |
async def on_ready():
|
| 22 |
print(f'Logged in as {bot.user.name}')
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
@bot.command()
|
| 26 |
+
async def save_messages(ctx, channel_id: int):
|
| 27 |
+
channel = bot.get_channel(channel_id)
|
| 28 |
+
if not channel:
|
| 29 |
+
await ctx.send("Channel not found.")
|
| 30 |
+
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
messages = await channel.history(limit=None).flatten()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
with open("gradio-questions.json", 'w', encoding='utf-8') as file:
|
| 35 |
+
for message in messages:
|
| 36 |
+
file.write(f"{message.author.display_name} ({message.author.id}): {message.content}\n")
|
| 37 |
|
| 38 |
+
await ctx.send(f"Messages from {channel.name} saved to {file_name}")
|
| 39 |
|
| 40 |
|
| 41 |
|