diff --git a/cogs/botsettings.py b/cogs/botsettings.py index da17a65..57b7344 100644 --- a/cogs/botsettings.py +++ b/cogs/botsettings.py @@ -84,19 +84,19 @@ class BotSettings: await ctx.send(embed=embed) @commands.guild_only() - @_set.command(name='voicechannels') + @_set.command(name='musicchannels') async def set_music_voice(self, ctx, *channels: discord.VoiceChannel): if channels: channel_ids = [channel.id for channel in channels] - self.bot.settings.set(ctx.guild, 'channels.voice', channel_ids) + self.bot.settings.set(ctx.guild, 'channels.music', channel_ids) else: - self.bot.settings.set(ctx.guild, 'channels.voice', None) + self.bot.settings.set(ctx.guild, 'channels.music', None) - voicechannels = self.bot.settings.get(ctx.guild, 'channels.voice') + musicchannels = self.bot.settings.get(ctx.guild, 'channels.music') - if voicechannels: + if musicchannels: embed = discord.Embed(title='Music channels set', color=ctx.me.color) - channels = [ctx.guild.get_channel(channel) for channel in voicechannels] + channels = [ctx.guild.get_channel(channel) for channel in musicchannels] mentioned = [channel.name for channel in channels if channel is not None] embed.description = ', '.join(mentioned) await ctx.send(embed=embed) @@ -168,11 +168,11 @@ class BotSettings: mentioned = [channel.mention for channel in channels if channel is not None] embed.add_field(name='Text channels', value='\n'.join(mentioned)) - voicechannels = self.bot.settings.get(ctx.guild, 'channels.voice') + voicechannels = self.bot.settings.get(ctx.guild, 'channels.music') if voicechannels: channels = [ctx.guild.get_channel(channel) for channel in voicechannels] mentioned = [channel.name for channel in channels if channel is not None] - embed.add_field(name='Voice channels', value='\n'.join(mentioned)) + embed.add_field(name='Music channels', value='\n'.join(mentioned)) listenchannels = self.bot.settings.get(ctx.guild, 'channels.listen_only') if listenchannels: diff --git a/cogs/music.py b/cogs/music.py index 91cd70e..28db7ba 100644 --- a/cogs/music.py +++ b/cogs/music.py @@ -39,6 +39,18 @@ class Music: lang = self.bot.settings.get(guild, 'locale', 'default_locale') return LocalizerWrapper(self.bot.localizer, lang, "music.response") + async def __local_check(self, ctx): + """ Ensures the commands are used in the correct channels """ + textchannels = self.bot.settings.get(ctx.guild, 'channels.text', []) + if textchannels: + if ctx.channel.id not in textchannels: + localizer = self.getLocalizer(ctx.guild) + response = localizer.format_str('{settings_check.textchannel}') + for channel_id in textchannels: + response += f'<#{channel_id}>, ' + await ctx.send(response[:-2]) + return False + return True async def __before_invoke(self, ctx): # TODO: rewrite this thing. Probably remove ensure_voice. @@ -134,12 +146,15 @@ class Music: player.add_skipper(ctx.author) total = len(player.listeners) skips = len(player.skip_voters) - if skips >= math.ceil(total/2) or player.current.requester == ctx.author.id: + threshold = self.bot.settings.get(ctx.guild, 'vote_threshold', 'default_threshold') + + if skips/total >= threshold/100 or player.current.requester == ctx.author.id: await player.skip() await ctx.send(localizer.format_str("{skip.skipped}")) else: if skips != 0: - msg = localizer.format_str("{skip.require_vote}", _skips=skips, _total=math.ceil(total/2)) + needed = math.ceil(total*threshold/100) + msg = localizer.format_str("{skip.require_vote}", _skips=skips, _total=needed) await ctx.send(msg) @commands.command(name='skipto', aliases=['forceskip','hopptil','tvinghopp']) @@ -662,11 +677,26 @@ class Music: if not should_connect: raise commands.CommandInvokeError('Not connected.') - permissions = ctx.author.voice.channel.permissions_for(ctx.me) + voice_channel = ctx.author.voice.channel + + permissions = voice_channel.permissions_for(ctx.me) if not permissions.connect or not permissions.speak: # Check user limit too? raise commands.CommandInvokeError('I need the `CONNECT` and `SPEAK` permissions.') + voice_channels = self.bot.settings.get(ctx.guild, 'channels.music', []) + + if voice_channels: + if voice_channel.id not in voice_channels: + localizer = self.getLocalizer(ctx.guild) + response = localizer.format_str('{settings_check.voicechannel}') + for channel_id in voice_channels: + channel = ctx.guild.get_channel(channel_id) + if channel is not None: + response += f'{channel.name}, ' + await ctx.send(response[:-2]) + raise commands.CommandInvokeError('You need to be in the right voice channel') + player.store('channel', ctx.channel.id) await self.connect_to(ctx.guild.id, str(ctx.author.voice.channel.id)) @@ -674,15 +704,6 @@ class Music: if int(player.channel_id) != ctx.author.voice.channel.id: raise commands.CommandInvokeError('You need to be in my voicechannel.') - def max_track_length(self, guild): - player = self.bot.lavalink.players.get(guild.id) - if len(player.listeners): - listeners = len(player.listeners) - else: - listeners = 1 - maxlen = max(60*60/listeners, 60*10) - return maxlen - async def enqueue(self, ctx, track, embed): player = self.bot.lavalink.players.get(ctx.guild.id) @@ -712,6 +733,15 @@ class Music: duration = lavalink.utils.format_time(int(track.duration)) embed.description = f'[{track.title}]({track.uri})\n**{duration}**' + def max_track_length(self, guild): + player = self.bot.lavalink.players.get(guild.id) + if len(player.listeners): + listeners = len(player.listeners) + else: + listeners = 1 + maxlen = max(60*60/listeners, 60*10) + return maxlen + async def on_command_error(self, ctx, err): if isinstance(err, commands.CommandInvokeError): pass diff --git a/localization/en_en/music.json b/localization/en_en/music.json index 374ce59..81244de 100644 --- a/localization/en_en/music.json +++ b/localization/en_en/music.json @@ -80,6 +80,10 @@ "track": "`{_index}.` **[{_title}]({_uri})** _by <@{_id}>_", "previous": "**{_len} Previous songs:**", "empty": "There are no tracks in the history" + }, + "settings_check": { + "textchannel": "You can't use music commands in this channel.\nTry in: ", + "voicechannel": "I can only play music in: " } } } \ No newline at end of file diff --git a/localization/nb_no/music.json b/localization/nb_no/music.json index 9653d68..e7e8e30 100644 --- a/localization/nb_no/music.json +++ b/localization/nb_no/music.json @@ -80,6 +80,10 @@ "track": "`{_index}.` **[{_title}]({_uri})** _av <@{_id}>_", "previous": "**Tidligere sanger ({_len}):**", "empty": "Ingen tidligere spilte sanger" + }, + "settings_check": { + "textchannel": "Du kan ikke bruke musikkommandoer i denne kanalen.\nPrøv i: ", + "voicechannel": "Jeg kan bare spille musikk i: " } } } \ No newline at end of file diff --git a/localization/nn_no/music.json b/localization/nn_no/music.json index 5988f99..3de3484 100644 --- a/localization/nn_no/music.json +++ b/localization/nn_no/music.json @@ -80,6 +80,10 @@ "track": "`{_index}.` **[{_title}]({_uri})** _av <@{_id}>_", "previous": "**Tidlegare songar ({_len}):**", "empty": "Ingen tidlegare spelte songar" + }, + "settings_check": { + "textchannel": "Du kan ikkje bruke musikkommandoar i denne kanalen.\nPrøv i: ", + "voicechannel": "Eg kan berre spele musikk i: " } } } \ No newline at end of file