From 2c2d191a082437f396d2c3318d13f67783af7fbe Mon Sep 17 00:00:00 2001 From: codey Date: Fri, 14 Feb 2025 07:10:09 -0500 Subject: [PATCH] misc/add compliment bridge command --- cogs/karma.py | 2 +- cogs/misc.py | 34 ++++++++++++++++++++++++++++++++++ cogs/radio.py | 13 ++++++++++--- disc_havoc.py | 1 - util/radio_util.py | 21 +++++++++++++++++++++ 5 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 util/radio_util.py diff --git a/cogs/karma.py b/cogs/karma.py index 8069cb3..13f0894 100644 --- a/cogs/karma.py +++ b/cogs/karma.py @@ -148,7 +148,7 @@ class Karma(commands.Cog): """Karma Cog for Havoc""" def __init__(self, bot): importlib.reload(constants) - self.bot = bot + self.bot: discord.Bot = bot self.util = Util(self.bot) # self.karma_regex = regex.compile(r'(\w+)(\+\+|\-\-)') self.karma_regex: Pattern = regex.compile(r'(\b\w+(?:\s+\w+)*)(\+\+($|\s)|\-\-($|\s))') diff --git a/cogs/misc.py b/cogs/misc.py index ed3e08a..28fcaa9 100644 --- a/cogs/misc.py +++ b/cogs/misc.py @@ -255,6 +255,40 @@ class Misc(commands.Cog): except Exception as e: traceback.print_exc() return await ctx.respond(f"Insult failed :(\nError: {str(e)}") + + @bridge.bridge_command() + async def compliment(self, ctx, *, + recipient: Optional[str] = None) -> None: + """ + Compliment someone (or yourself) + Args: + ctx (Any) + recipient (Optional[str]) + Returns: + None + """ + try: + authorDisplay: str = ctx.author.display_name if not(ctx.author.display_name is None)\ + else ctx.message.author.display_name + + if recipient is None: + recipient: str = authorDisplay.strip() + else: + if discord.utils.raw_mentions(recipient): + # There are mentions + recipient_id: int = discord.utils.raw_mentions(recipient)[0] # First mention + recipient: str = self.bot.get_guild(ctx.guild.id)\ + .get_member(recipient_id).display_name + else: + recipient: str = discord.utils.escape_mentions(recipient.strip()) + with ctx.channel.typing(): + compliment: str = await self.util.get_compliment(recipient) + if compliment: + return await ctx.respond(compliment) + return await ctx.respond("Compliment failed :(") + except Exception as e: + traceback.print_exc() + return await ctx.respond(f"Compliment failed :(\nError: {str(e)}") @bridge.bridge_command(aliases=['whiskey']) async def whisky(self, ctx, *, diff --git a/cogs/radio.py b/cogs/radio.py index 8852c15..b13aeb4 100644 --- a/cogs/radio.py +++ b/cogs/radio.py @@ -3,7 +3,9 @@ import logging import traceback +from typing import Optional from discord.ext import bridge, commands, tasks +from util.radio_util import get_now_playing import discord class Radio(commands.Cog): @@ -14,7 +16,7 @@ class Radio(commands.Cog): 'sfm': (1145182936002482196, 1221615558492029050), # Tuple: Guild Id, Chan Id } self.STREAM_URL: str = "https://relay.sfm.codey.lol/aces.ogg" - + self.LAST_NP_TRACK: Optional[str] = None try: self.radio_state_loop.cancel() except Exception as e: @@ -75,7 +77,7 @@ class Radio(commands.Cog): traceback.print_exc() return - @tasks.loop(seconds=2.0) + @tasks.loop(seconds=5.0) async def radio_state_loop(self) -> None: """Radio State Loop""" try: @@ -93,7 +95,12 @@ class Radio(commands.Cog): source = discord.FFmpegOpusAudio(self.STREAM_URL, before_options="-timeout 3000000") vc.play(source, after=lambda e: logging.info("Error: %s", e)\ - if e else None) + if e else None) + # Get Now Playing + np_track = await get_now_playing() + if np_track and not self.LAST_NP_TRACK == np_track: + self.LAST_NP_TRACK: str = np_track + await vc.channel.set_status(f"Now playing: {np_track}") except: traceback.print_exc() diff --git a/disc_havoc.py b/disc_havoc.py index 8a35d2e..5d4e644 100644 --- a/disc_havoc.py +++ b/disc_havoc.py @@ -26,7 +26,6 @@ cogs_list: list[str] = [ 'owner', 'sing', 'meme', - 'ai', 'karma', 'lovehate', 'quote', diff --git a/util/radio_util.py b/util/radio_util.py new file mode 100644 index 0000000..5e108ff --- /dev/null +++ b/util/radio_util.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3.12 + +import logging +from aiohttp import ClientSession, ClientTimeout + +"""Radio Utils""" + +async def get_now_playing() -> str: + np_url: str = "https://api.codey.lol/radio/np" + try: + async with ClientSession() as session: + async with await session.post(np_url, headers={ + 'content-type': 'application/json; charset=utf-8', + }, timeout=ClientTimeout(connect=1.5, sock_read=1.5)) as request: + request.raise_for_status() + response_json = await request.json() + artistsong = response_json.get('artistsong') + return artistsong + except Exception as e: + logging.critical("Now playing retrieval failed: %s", + str(e)) \ No newline at end of file