diff --git a/cogs/basic.py b/cogs/basic.py index 7a4170a..2805ed4 100644 --- a/cogs/basic.py +++ b/cogs/basic.py @@ -58,7 +58,7 @@ class Basic(commands.Cog): self.description = "This category is available to anyone on this server. Voting is required in certain commands." self.ctx_menu = app_commands.ContextMenu( name="play", - callback=self._play, + callback=self._play ) self.bot.tree.add_command(self.ctx_menu) @@ -111,39 +111,45 @@ class Basic(commands.Cog): await player.do_next() @commands.dynamic_cooldown(cooldown_check, commands.BucketType.guild) - async def _play(self, ctx: commands.Context, message: discord.Message): + async def _play(self, interaction: discord.Interaction, message: discord.Message): query = "" + if message.content: url = re.findall( "http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+", message.content) if url: query = url[0] + elif message.attachments: query = message.attachments[0].url if not query: - return await ctx.send(get_lang(ctx.guild.id, key="noPlaySource"), ephemeral=True) + return await interaction.response.send_message(get_lang(interaction.guild.id, key="noPlaySource"), ephemeral=True) - player: voicelink.Player = ctx.guild.voice_client + player: voicelink.Player = interaction.guild.voice_client if not player: - player = await connect_channel(ctx) + player = await connect_channel(interaction) - if not player.is_user_join(ctx.author): - return await ctx.send(player.get_msg('notInChannel').format(ctx.author.mention, player.channel.mention), ephemeral=True) + if not player.is_user_join(interaction.user): + return await interaction.response.send_message(player.get_msg('notInChannel').format(interaction.user.mention, player.channel.mention), ephemeral=True) - tracks = await player.get_tracks(query, requester=ctx.author) + tracks = await player.get_tracks(query, requester=interaction.user) if not tracks: - return await ctx.send(player.get_msg('noTrackFound')) + return await interaction.response.send_message(player.get_msg('noTrackFound')) try: if isinstance(tracks, voicelink.Playlist): index = await player.add_track(tracks.tracks) - await ctx.send(player.get_msg('playlistLoad').format(tracks.name, index)) + await interaction.response.send_message(player.get_msg('playlistLoad').format(tracks.name, index)) else: position = await player.add_track(tracks[0]) - await ctx.send((f"`{player.get_msg('live')}`" if tracks[0].is_stream else "") + (player.get_msg('trackLoad_pos').format(tracks[0].title, tracks[0].author, tracks[0].formatLength, position) if position >= 1 and player.is_playing else player.get_msg('trackLoad').format(tracks[0].title, tracks[0].author, tracks[0].formatLength))) + await interaction.response.send_message((f"`{player.get_msg('live')}`" if tracks[0].is_stream else "") + (player.get_msg('trackLoad_pos').format(tracks[0].title, tracks[0].author, tracks[0].formatLength, position) if position >= 1 and player.is_playing else player.get_msg('trackLoad').format(tracks[0].title, tracks[0].author, tracks[0].formatLength))) except voicelink.QueueFull as e: - await ctx.send(e) + await interaction.response.send_message(e) + + except Exception as e: + return await interaction.response.send_message(e, ephemeral=True) + finally: if not player.is_playing: await player.do_next() diff --git a/function.py b/function.py index 665085c..6bec5c8 100644 --- a/function.py +++ b/function.py @@ -11,7 +11,7 @@ from datetime import datetime from time import strptime from io import BytesIO from pymongo import MongoClient -from typing import Optional +from typing import Optional, Union from addons import Settings root_dir = os.path.dirname(os.path.abspath(__file__)) @@ -204,20 +204,24 @@ async def similar_track(player) -> bool: return False -async def connect_channel(ctx: commands.Context, channel: discord.VoiceChannel = None): +async def connect_channel(ctx: Union[commands.Context, discord.Interaction], channel: discord.VoiceChannel = None): voicelink = import_module("voicelink"); try: - channel = channel or ctx.author.voice.channel + channel = channel or ctx.author.voice.channel if isinstance(ctx, commands.Context) else ctx.user.voice.channel except: raise voicelink.VoicelinkException(get_lang(ctx.guild.id, 'noChannel')) check = channel.permissions_for(ctx.guild.me) if check.connect == False or check.speak == False: - raise voicelink.VoicelinkException( - get_lang(ctx.guild.id, 'noPermission')) + raise voicelink.VoicelinkException(get_lang(ctx.guild.id, 'noPermission')) + + player: voicelink.Player = await channel.connect(cls=voicelink.Player( + ctx.bot if isinstance(ctx, commands.Context) else ctx.client, channel, ctx + )) + + if player.is_ipc_connected: + await player.send_ws({"op": "createPlayer", "members_id": [member.id for member in channel.members]}) - player: voicelink.Player = await channel.connect(cls=voicelink.Player(ctx.bot, channel, ctx)) - await player.send_ws({"op": "createPlayer", "members_id": [member.id for member in channel.members]}) return player def time(millis:int) -> str: diff --git a/voicelink/player.py b/voicelink/player.py index 1986495..8523f91 100644 --- a/voicelink/player.py +++ b/voicelink/player.py @@ -44,7 +44,8 @@ from discord import ( Member, Embed, ui, - Message + Message, + Interaction ) from discord.ext import commands @@ -76,12 +77,12 @@ class Player(VoiceProtocol): self, client: Optional[Client] = None, channel: Optional[VoiceChannel] = None, - ctx: commands.Context = None, + ctx: Union[commands.Context, Interaction] = None, ): self.client = client self._bot = client self.context = ctx - self.dj: Member = ctx.author + self.dj: Member = ctx.user if isinstance(ctx, Interaction) else ctx.author self.channel: VoiceChannel = channel self._guild = channel.guild if channel else None self._ipc_connection: bool = False