diff --git a/bot/src/cogs/info.py b/bot/src/cogs/info.py index d9da3c9..238cd55 100644 --- a/bot/src/cogs/info.py +++ b/bot/src/cogs/info.py @@ -260,7 +260,7 @@ class Info(commands.Cog): # IF roles list is longer than 2048, create text file and send it if len(roles) > 2048: - await discord_utils.send_as_txt_file(interaction, roles, f"./assets/temp/{interaction.guild.id}_roles.txt") + await discord_utils.send_as_txt_file(interaction, roles, f"{interaction.guild.id}_roles.txt") else: embed = discord.Embed(color=interaction.guild.me.color, description=roles) embed.set_author(name=f"Roller ({len(interaction.guild.roles)})", icon_url=interaction.guild.icon) @@ -692,7 +692,7 @@ class Info(commands.Cog): # If the list of roles is too long, send it as a file if len(roles) > 2048: await discord_utils.send_as_txt_file( - interaction, roles, f"./assets/temp/{interaction.guild.id}_{interaction.user.id}_roles.txt" + interaction, roles, f"{interaction.guild.id}_{interaction.user.id}_roles.txt" ) else: embed = discord.Embed(color=bruker.color, description=roles) diff --git a/bot/src/cogs/utils/discord_utils.py b/bot/src/cogs/utils/discord_utils.py index 232c110..de292b8 100644 --- a/bot/src/cogs/utils/discord_utils.py +++ b/bot/src/cogs/utils/discord_utils.py @@ -1,4 +1,4 @@ -import os +import io import discord from discord.ext import commands @@ -6,31 +6,20 @@ from discord.ext import commands from .misc_utils import Paginator -async def send_as_txt_file(interaction: discord.Interaction, content: str, file_path: str): +async def send_as_txt_file(interaction: discord.Interaction, content: str, filename: str): """ - Sends a string as a txt file and deletes the file afterwards + Sends a string as a txt file. The file is kept in memory, so nothing is written to disk Parameters ---------- interaction (discord.Interaction): Slash command context object content (str): String that's too long to send - file_path (str): Path to file + filename (str): Name of the file shown in Discord """ - # Create file - with open(file_path, "w", encoding="utf-8") as file: - file.write(content) - - # Send file - txt_file = discord.File(file_path) + txt_file = discord.File(io.BytesIO(content.encode("utf-8")), filename=filename) await interaction.response.send_message(file=txt_file) - # Delete file - try: - os.remove(file_path) - except OSError: - pass - async def get_discord_guild(bot: commands.Bot, id: int) -> discord.Guild | None: """