such cleanup

This commit is contained in:
2025-02-15 08:36:45 -05:00
parent d60828df07
commit 9d23438b13
12 changed files with 357 additions and 209 deletions

View File

@ -10,20 +10,22 @@ import discord
import regex
from util.sing_util import Utility
from discord.ext import bridge, commands
from disc_havoc import Havoc
BOT_CHANIDS = []
class Sing(commands.Cog):
"""Sing Cog for Havoc"""
def __init__(self, bot):
self.bot: discord.Bot = bot
def __init__(self, bot: Havoc) -> None:
self.bot: Havoc = bot
self.utility = Utility()
global BOT_CHANIDS
BOT_CHANIDS = self.bot.BOT_CHANIDS # Inherit
self.control_strip_regex: Pattern = regex.compile(r"\x0f|\x1f|\035|\002|\u2064|\x02|(\x03([0-9]{1,2}))|(\x03|\003)(?:\d{1,2}(?:,\d{1,2})?)?",
regex.UNICODE)
def is_spamchan(): # pylint: disable=no-method-argument
def is_spamchan(): # type: ignore
"""Check if channel is spam chan"""
def predicate(ctx):
try:
@ -57,7 +59,7 @@ class Sing(commands.Cog):
for _activity in ctx.author.activities:
if _activity.type == discord.ActivityType.listening:
activity: discord.Activity = _activity
activity = _activity
if not activity:
return await ctx.respond("**Error**: No song specified, no activity found to read.")
@ -65,7 +67,9 @@ class Sing(commands.Cog):
if interaction:
await ctx.respond("*Searching...*", ephemeral=True) # Must respond to interactions within 3 seconds, per Discord
(search_artist, search_song, search_subsearch) = self.utility.parse_song_input(song, activity)
parsed = self.utility.parse_song_input(song, activity)
if isinstance(parsed, tuple):
(search_artist, search_song, search_subsearch) = parsed
# await ctx.respond(f"So, {search_song} by {search_artist}? Subsearch: {search_subsearch} I will try...") # Commented, useful for debugging
search_result: list[str] = await self.utility.lyric_search(search_artist, search_song,
@ -73,13 +77,16 @@ class Sing(commands.Cog):
if len(search_result) == 1:
return await ctx.respond(search_result[0].strip())
(search_result_artist, search_result_song, search_result_src,
search_result_confidence, search_result_time_taken) = search_result[0] # First index is a tuple
if not isinstance(search_result[0], tuple):
return # Invalid data type
(
search_result_artist, search_result_song, search_result_src,
search_result_confidence, search_result_time_taken
) = search_result[0] # First index is a tuple
search_result_wrapped: list[str] = search_result[1] # Second index is the wrapped lyrics
search_result_wrapped_short: list[str] = search_result[2] # Third is short wrapped lyrics
if not ctx.channel.id in BOT_CHANIDS:
search_result_wrapped: list[str] = search_result_wrapped_short # Replace with shortened lyrics for non spamchans
search_result_wrapped = search_result_wrapped_short # Replace with shortened lyrics for non spamchans
embeds: list[Optional[discord.Embed]] = []
embed_url: str = f"[on codey.lol](https://codey.lol/#{urllib.parse.quote(search_artist)}/{urllib.parse.quote(search_song)})"
c: int = 0
@ -87,8 +94,8 @@ class Sing(commands.Cog):
for section in search_result_wrapped:
c+=1
if c == len(search_result_wrapped):
footer: str = f"Found on: {search_result_src}"
section: str = self.control_strip_regex.sub('', section)
footer = f"Found on: {search_result_src}"
section = self.control_strip_regex.sub('', section)
# if ctx.guild.id == 1145182936002482196:
# section = section.upper()
embed: discord.Embed = discord.Embed(
@ -131,7 +138,7 @@ class Sing(commands.Cog):
activity: Optional[discord.Activity] = None
for _activity in ctx.interaction.guild.get_member(member_id).activities:
if _activity.type == discord.ActivityType.listening:
activity: discord.Activity = _activity
activity = _activity
parsed: tuple|bool = self.utility.parse_song_input(song=None,
activity=activity)
if not parsed:
@ -139,43 +146,44 @@ class Sing(commands.Cog):
if IS_SPAMCHAN:
await ctx.respond(f"***Reading activity of {member_display}...***")
(search_artist, search_song, search_subsearch) = parsed
await ctx.respond("*Searching...*", ephemeral=True) # Must respond to interactions within 3 seconds, per Discord
search_result: list = await self.utility.lyric_search(search_artist, search_song,
if isinstance(parsed, tuple):
(search_artist, search_song, search_subsearch) = parsed
await ctx.respond("*Searching...*", ephemeral=True) # Must respond to interactions within 3 seconds, per Discord
search_result: list = await self.utility.lyric_search(search_artist, search_song,
search_subsearch)
if len(search_result) == 1:
return await ctx.send(search_result[0].strip())
(search_result_artist, search_result_song, search_result_src,
search_result_confidence, search_result_time_taken) = search_result[0] # First index is a tuple
search_result_wrapped: list[str] = search_result[1] # Second index is the wrapped lyrics
search_result_wrapped_short: list[str] = search_result[2] # Third index is shortened lyrics
if not IS_SPAMCHAN:
search_result_wrapped: list[str] = search_result_wrapped_short # Swap for shortened lyrics if not spam chan
embeds: list[Optional[discord.Embed]] = []
c: int = 0
footer: str = "To be continued..." #Placeholder
for section in search_result_wrapped:
c+=1
if c == len(search_result_wrapped):
footer: str = f"Found on: {search_result_src}"
# if ctx.guild.id == 1145182936002482196:
# section = section.upper()
embed: discord.Embed = discord.Embed(
title=f"{search_result_song} by {search_result_artist}",
description=discord.utils.escape_markdown(section.replace("\n", "\n\n"))
)
embed.add_field(name="Confidence", value=search_result_confidence, inline=True)
embed.add_field(name="Time Taken", value=search_result_time_taken, inline=True)
embed.add_field(name="Link", value=f"[on codey.lol](https://codey.lol/#{urllib.parse.quote(search_result_artist)}/{urllib.parse.quote(search_result_song)})")
embed.set_footer(text=footer)
embeds.append(embed)
if len(search_result) == 1:
return await ctx.send(search_result[0].strip())
for embed in embeds:
await ctx.send(embed=embed)
(search_result_artist, search_result_song, search_result_src,
search_result_confidence, search_result_time_taken) = search_result[0] # First index is a tuple
search_result_wrapped: list[str] = search_result[1] # Second index is the wrapped lyrics
search_result_wrapped_short: list[str] = search_result[2] # Third index is shortened lyrics
if not IS_SPAMCHAN:
search_result_wrapped = search_result_wrapped_short # Swap for shortened lyrics if not spam chan
embeds: list[Optional[discord.Embed]] = []
c: int = 0
footer: str = "To be continued..." #Placeholder
for section in search_result_wrapped:
c+=1
if c == len(search_result_wrapped):
footer = f"Found on: {search_result_src}"
# if ctx.guild.id == 1145182936002482196:
# section = section.upper()
embed: discord.Embed = discord.Embed(
title=f"{search_result_song} by {search_result_artist}",
description=discord.utils.escape_markdown(section.replace("\n", "\n\n"))
)
embed.add_field(name="Confidence", value=search_result_confidence, inline=True)
embed.add_field(name="Time Taken", value=search_result_time_taken, inline=True)
embed.add_field(name="Link", value=f"[on codey.lol](https://codey.lol/#{urllib.parse.quote(search_result_artist)}/{urllib.parse.quote(search_result_song)})")
embed.set_footer(text=footer)
embeds.append(embed)
for _embed in embeds:
await ctx.send(embed=_embed)
except Exception as e:
traceback.print_exc()
return await ctx.respond(f"ERR: {str(e)}")