66import random
77import re
88from typing import TYPE_CHECKING , Optional
9-
9+ from discord import app_commands
1010import button_paginator as pg
1111import contextlib
1212import discord
1313from discord .ext import commands
14- from ext .helpers import Spotify , grouper , ordinal_suffix_of , gemini_split_string
14+ from ext .helpers import Spotify , grouper , ordinal_suffix_of , gemini_split_string , get_lyrics , find_surrounding_lyrics , filter_banned_words
1515from ext .http import Http
1616from ext .ui .view import Piston
17-
17+ import time
1818import google .generativeai as genai
1919import button_paginator as pg
2020from googletrans import Translator
2323 from ext .models import CodingBot
2424
2525
26+
27+ # I don't like the way its implemented but can't find a better way to do it so if someone can make this command better, that'll be great!
28+
29+ @app_commands .context_menu (name = "translate" )
30+ async def translate (interaction : discord .Interaction , message : discord .Message ):
31+ await interaction .response .defer (ephemeral = True )
32+ trans = message .content
33+ try :
34+ translated = Translator (service_urls = [
35+ 'translate.google.com' ,
36+ 'translate.google.co.kr'
37+ ]).translate (trans )
38+ except Exception as e :
39+ raise e
40+
41+ embed = discord .Embed ()
42+
43+ _from = translated .src .upper ()
44+ _to = translated .dest .upper ()
45+
46+ embed .add_field (
47+ name = f"Original ({ _from } )" ,
48+ value = trans ,
49+ inline = False
50+ )
51+ embed .add_field (
52+ name = f"Translated ({ _to } )" ,
53+ value = translated .text ,
54+ inline = False
55+ )
56+
57+ await interaction .followup .send (
58+ embed = embed ,
59+ ephemeral = True
60+ )
61+
62+
63+
2664class Miscellaneous (commands .Cog , command_attrs = dict (hidden = False )):
2765 hidden = False
2866
@@ -40,6 +78,9 @@ async def cog_check(self, ctx: commands.Context[CodingBot]) -> bool:
4078 return True
4179 await ctx .send ("Please use commands in the server instead of dms" )
4280 return False
81+
82+ async def cog_load (self ):
83+ self .bot .tree .add_command (translate )
4384
4485 @commands .hybrid_command (
4586 name = "retry" ,
@@ -66,6 +107,7 @@ async def retry(self, ctx: commands.Context[CodingBot]):
66107 name = "afk" , aliases = ["afk-set" , "set-afk" ], help = "Sets your afk"
67108 )
68109 @commands .cooldown (1 , 10 , commands .BucketType .member )
110+ @commands .has_role (734283436637814844 ) # lvl 30+
69111 async def afk (
70112 self , ctx : commands .Context [CodingBot ], * , reason : Optional [str ] = None
71113 ):
@@ -496,46 +538,42 @@ async def on_timeout():
496538
497539 await paginator .start ()
498540
499- @commands .command (name = "translate" )
500- async def translate (self , ctx : commands .Context , * , text : str = None ):
501- if not ctx .message .reference and not text :
502- return await ctx .reply ("Please reply a message or provide a text to translate!" )
503-
504- if text :
505- trans = text
506- message = ctx .message
507- else :
508- message = await ctx .channel .fetch_message (ctx .message .reference .message_id )
509- trans = message .content
510541
511- try :
512- translated = Translator (service_urls = [
513- 'translate.google.com' ,
514- 'translate.google.co.kr'
515- ]).translate (trans )
516- except Exception as e :
517- raise e
542+ @commands .hybrid_command (name = "lyric" , aliases = ["lyrics" ])
543+ async def lyric (self , ctx : commands .Context , member : discord .Member = None ):
544+ member = member or ctx .author
518545
519- embed = discord .Embed ()
546+ spotify_activity = None
547+ for activity in member .activities :
548+ if isinstance (activity , discord .Spotify ):
549+ spotify_activity = activity
550+ break
551+
552+ if not spotify_activity :
553+ await ctx .send (f"{ member .display_name } is not listening to Spotify." )
554+ return
555+
556+ else :
557+ duration = time .time ()- spotify_activity .start .timestamp ()
558+ song_title = spotify_activity .title
559+ song_artist = spotify_activity .artist
560+ lyr = get_lyrics (song_title , song_artist )
561+ if not lyr :
562+ return await ctx .send (f"Lyrics not found for song - { song_title } - { song_artist } " )
563+ if lyr [1 ] == 1 :
564+ lyr = "\n " .join (find_surrounding_lyrics (lyr [0 ], int (duration )))
565+ else :
566+ lyr = "\n " .join (lyr .splitlines ()[0 :5 ])
567+
568+ lyr = filter_banned_words (lyr )
569+ embed = discord .Embed (description = lyr , title = f"{ song_title } - { song_artist } " , color = spotify_activity .color )
570+ embed .set_author (name = ctx .author , icon_url = ctx .author .avatar .url )
571+ embed .set_thumbnail (url = spotify_activity .album_cover_url )
572+
573+ await ctx .send (embed = embed )
574+
520575
521- _from = translated .src .upper ()
522- _to = translated .dest .upper ()
523-
524- embed .add_field (
525- name = f"Original ({ _from } )" ,
526- value = trans ,
527- inline = False
528- )
529- embed .add_field (
530- name = f"Translated ({ _to } )" ,
531- value = translated .text ,
532- inline = False
533- )
534576
535- await message .reply (
536- embed = embed ,
537- allowed_mentions = discord .AllowedMentions .none ()
538- )
539577
540578
541579async def setup (bot : CodingBot ):
0 commit comments