From 38169a82df89a12d9d7594a3333d6af864661978 Mon Sep 17 00:00:00 2001 From: Draper <27962761+Drapersniper@users.noreply.github.com> Date: Sun, 25 Oct 2020 21:46:59 +0000 Subject: [PATCH] small PR adding `[p]llset info` and `[p]audioset logs` (#4527) * small PR adding `[p]llset info` and `[p]audioset logs` * fixed + improvements * Zip file properly --- redbot/cogs/audio/core/commands/audioset.py | 45 ++++++++++++++++++++- redbot/cogs/audio/core/commands/llset.py | 19 +++++++++ redbot/cogs/audio/utils.py | 8 ++++ 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/redbot/cogs/audio/core/commands/audioset.py b/redbot/cogs/audio/core/commands/audioset.py index a9bc4a49a..5990c63d6 100644 --- a/redbot/cogs/audio/core/commands/audioset.py +++ b/redbot/cogs/audio/core/commands/audioset.py @@ -1,6 +1,8 @@ import asyncio import contextlib import logging +import os +import tarfile from pathlib import Path from typing import Union @@ -18,7 +20,7 @@ from redbot.core.utils.predicates import MessagePredicate, ReactionPredicate from ...audio_dataclasses import LocalPath from ...converters import ScopeParser from ...errors import MissingGuild, TooManyMatches -from ...utils import CacheLevel, PlaylistScope +from ...utils import CacheLevel, PlaylistScope, has_internal_server from ..abc import MixinMeta from ..cog_utils import CompositeMetaClass, PlaylistConverter, __version__ @@ -1129,6 +1131,47 @@ class AudioSetCommands(MixinMeta, metaclass=CompositeMetaClass): await self.send_embed_msg(ctx, description=box(msg, lang="ini")) + @command_audioset.command(name="logs") + @commands.is_owner() + @has_internal_server() + @commands.guild_only() + async def command_audioset_logs(self, ctx: commands.Context): + """Sends the Lavalink server logs to your DMs.""" + datapath = cog_data_path(raw_name="Audio") + logs = datapath / "logs" / "spring.log" + zip_name = None + try: + try: + if not (logs.exists() and logs.is_file()): + return await ctx.send(_("No logs found in your data folder.")) + except OSError: + return await ctx.send(_("No logs found in your data folder.")) + + def check(path): + return os.path.getsize(str(path)) > (8388608 - 1000) + + if check(logs): + zip_name = logs.with_suffix(".tar.gz") + zip_name.unlink(missing_ok=True) + with tarfile.open(zip_name, "w:gz") as tar: + tar.add(str(logs), arcname="spring.log", recursive=False) + if check(zip_name): + await ctx.send( + _("Logs are too large, you can find them in {path}").format( + path=zip_name.absolute() + ) + ) + zip_name = None + else: + await ctx.author.send(file=discord.File(str(zip_name))) + else: + await ctx.author.send(file=discord.File(str(logs))) + except discord.HTTPException: + await ctx.send(_("I need to be able to DM you to send you the logs.")) + finally: + if zip_name is not None: + zip_name.unlink(missing_ok=True) + @command_audioset.command(name="status") @commands.is_owner() @commands.guild_only() diff --git a/redbot/cogs/audio/core/commands/llset.py b/redbot/cogs/audio/core/commands/llset.py index 2c7ba484d..19357031d 100644 --- a/redbot/cogs/audio/core/commands/llset.py +++ b/redbot/cogs/audio/core/commands/llset.py @@ -5,6 +5,7 @@ import discord from redbot.core import commands from redbot.core.i18n import Translator +from redbot.core.utils.chat_formatting import box from ..abc import MixinMeta from ..cog_utils import CompositeMetaClass @@ -236,3 +237,21 @@ class LavalinkSetupCommands(MixinMeta, metaclass=CompositeMetaClass): prefix=ctx.prefix ), ) + + @command_llsetup.command(name="info", aliases=["settings"]) + async def command_llsetup_info(self, ctx: commands.Context): + """Display Lavalink connection settings.""" + configs = await self.config.all() + host = configs["host"] + password = configs["password"] + rest_port = configs["rest_port"] + ws_port = configs["ws_port"] + msg = "----" + _("Connection Settings") + "---- \n" + msg += _("Host: [{host}]\n").format(host=host) + msg += _("Rest Port: [{port}]\n").format(port=rest_port) + msg += _("WS Port: [{port}]\n").format(port=ws_port) + msg += _("Password: [{password}]\n").format(password=password) + try: + await self.send_embed_msg(ctx.author, description=box(msg, lang="ini")) + except discord.HTTPException: + await ctx.send(_("I need to be able to DM you to send you this info.")) diff --git a/redbot/cogs/audio/utils.py b/redbot/cogs/audio/utils.py index 16833d006..5f8198c05 100644 --- a/redbot/cogs/audio/utils.py +++ b/redbot/cogs/audio/utils.py @@ -220,3 +220,11 @@ def task_callback(task: asyncio.Task) -> None: with contextlib.suppress(asyncio.CancelledError, asyncio.InvalidStateError): if exc := task.exception(): log.exception(f"{task.get_name()} raised an Exception", exc_info=exc) + + +def has_internal_server(): + async def pred(ctx: commands.Context): + external = await ctx.cog.config.use_external_lavalink() + return not external + + return commands.check(pred)