diff --git a/cogs/botsettings.py b/cogs/botsettings.py index 57b7344..9d8ff21 100644 --- a/cogs/botsettings.py +++ b/cogs/botsettings.py @@ -146,6 +146,35 @@ class BotSettings: embed = discord.Embed(color=ctx.me.color, description='DJ roles cleared') await ctx.send(embed=embed) + @commands.guild_only() + @_set.command(name='maxduration') + async def set_max_track_duration(self, ctx, duration: int=None): + self.bot.settings.set(ctx.guild, 'duration.max', duration) + + duration = self.bot.settings.get(ctx.guild, 'duration.max') + + if duration: + embed = discord.Embed(title='Max track duration set to', color=ctx.me.color) + embed.description = f'{duration} minutes' + await ctx.send(embed=embed) + else: + embed = discord.Embed(color=ctx.me.color, description='Max track length removed') + await ctx.send(embed=embed) + + @commands.guild_only() + @_set.command(name='dynamicmax') + async def set_track_duration_type(self, ctx, dynamic: bool=False): + self.bot.settings.set(ctx.guild, 'duration.is_dynamic', dynamic) + + is_dynamic = self.bot.settings.get(ctx.guild, 'duration.is_dynamic') + + embed = discord.Embed(title='Max duration type set to', color=ctx.me.color) + if is_dynamic: + embed.description = f'Dynamic' + else: + embed.description = f'Static' + await ctx.send(embed=embed) + @commands.guild_only() @_set.command(name='current') async def current_settings(self, ctx): @@ -162,6 +191,13 @@ class BotSettings: threshold = self.bot.settings.get(ctx.guild, 'vote_threshold', 50) embed.add_field(name='Vote threshold', value=f'{threshold}%') + is_dynamic = self.bot.settings.get(ctx.guild, 'duration.is_dynamic', 'default_is_dynamic') + embed.add_field(name='Dynamic max duration', value=is_dynamic) + + duration = self.bot.settings.get(ctx.guild, 'maxduration') + if duration: + embed.add_field(name='Max track duration', value=f'{duration} minutes') + textchannels = self.bot.settings.get(ctx.guild, 'channels.text') if textchannels: channels = [ctx.guild.get_channel(channel) for channel in textchannels] diff --git a/cogs/music.py b/cogs/music.py index 3e8be7e..cc9c01a 100644 --- a/cogs/music.py +++ b/cogs/music.py @@ -92,11 +92,20 @@ class Music: if results['loadType'] == 'PLAYLIST_LOADED': tracks = results['tracks'] - for track in tracks: - player.add(requester=ctx.author.id, track=track) + maxlength = self.max_track_length(ctx.guild, player) + if maxlength: + numtracks = 0 + for track in tracks: + if track['info']['length'] <= maxlength: + player.add(requester=ctx.author.id, track=track) + numtracks += 1 + else: + numtracks = len(tracks) + for track in tracks: + player.add(requester=ctx.author.id, track=track) embed.title = '{playlist_enqued}' - embed.description = f'{results["playlistInfo"]["name"]} - {len(tracks)} {{tracks}}' + embed.description = f'{results["playlistInfo"]["name"]} - {numtracks} {{tracks}}' embed = localizer.format_embed(embed) await ctx.send(embed=embed) else: @@ -173,7 +182,6 @@ class Music: return await ctx.send(localizer.format_str("{skip_to.exceeds_queue}")) await player.skip(pos - 1) msg = localizer.format_str("{skip_to.skipped_to}", _title=player.current.title, _pos=pos) - print(msg, player.current.title, pos) await ctx.send(msg) @commands.command(name='stop', aliases=['stopp']) @@ -519,7 +527,7 @@ class Music: embed = localizer.format_embed(embed, _volume=player.volume) await ctx.send(embed=embed) - @commands.command(name='nomalize', aliases=['normal','nl','normaliser']) + @commands.command(name='normalize', aliases=['normal','nl','normaliser']) @checks.DJ_or(alone=True) async def _normalize(self, ctx): """ Reset the equalizer and """ @@ -704,9 +712,17 @@ class Music: async def enqueue(self, ctx, track, embed): player = self.bot.lavalink.players.get(ctx.guild.id) - track, pos_global, pos_local = player.add(requester=ctx.author.id, track=track) localizer = self.getLocalizer(ctx.guild) + maxlength = self.max_track_length(ctx.guild, player) + if maxlength and track['info']['length'] > maxlength: + embed.description = localizer.format_str("{enqueue.toolong}", + _length=lavalink.utils.format_time(track['info']['length']), + _max=lavalink.utils.format_time(maxlength)) + return + + track, pos_global, pos_local = player.add(requester=ctx.author.id, track=track) + if player.current is not None: queue_duration = 0 for i, track in enumerate(player.queue): @@ -730,14 +746,19 @@ 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): + def max_track_length(self, guild, player): + is_dynamic = self.bot.settings.get(guild, 'duration.is_dynamic', 'default_duration_type') + maxlength = self.bot.settings.get(guild, 'duration.max', None) + if maxlength is None: + return None + if len(player.listeners): # Avoid division by 0. listeners = len(player.listeners) else: listeners = 1 - maxlen = max(60*60/listeners, 60*10) - return maxlen + if maxlength > 10 and is_dynamic: + return max(maxlength*60*1000/listeners, 60*10*1000) + else: + return maxlength*60*1000 async def on_command_error(self, ctx, err): if isinstance(err, commands.CommandInvokeError): diff --git a/cogs/utils/settings.py b/cogs/utils/settings.py index cdf826d..0bf69e1 100644 --- a/cogs/utils/settings.py +++ b/cogs/utils/settings.py @@ -9,9 +9,10 @@ class Settings: self._SETTINGS_PATH = self._DATA_PATH + 'settings.yaml' self.default_prefix = default_settings["prefix"] - self.default_mod = default_settings["moderator_role"] + self.default_mod = default_settings["moderator role"] self.default_locale = default_settings["locale"] self.default_threshold = default_settings["threshold"] + self.default_is_dynamic = default_settings["dynamic max duration"] if not self.default_locale: locale, codepage = locale.getlocale() @@ -85,7 +86,7 @@ class Settings: return default value = self._get(self.settings[guild_id], setting.split('.')) - if value: + if value is not None: return value else: return default diff --git a/data/config.yaml.example b/data/config.yaml.example index f999c41..f314103 100644 --- a/data/config.yaml.example +++ b/data/config.yaml.example @@ -10,9 +10,10 @@ locale_path: ./localization default server settings: prefix: - "!" - moderator_role: Moderator + moderator role: Moderator locale: en_en threshold: 50 + dynamic max duration: Yes lavalink nodes: main: # The preferred lavalink node for your music bot diff --git a/localization/en_en/music.json b/localization/en_en/music.json index 81244de..27b8748 100644 --- a/localization/en_en/music.json +++ b/localization/en_en/music.json @@ -72,7 +72,8 @@ "position": "position", "playing_in": "Playing in", "enqueued": "Song enqueued", - "estimated": "estimated" + "estimated": "estimated", + "toolong": "Track too long. `{_length}>{_max}`" }, "history": { "title": "Track history", diff --git a/localization/nb_no/music.json b/localization/nb_no/music.json index e7e8e30..c0592c9 100644 --- a/localization/nb_no/music.json +++ b/localization/nb_no/music.json @@ -72,7 +72,8 @@ "position": "Køposisjon", "playing_in": "Spiller av om", "enqueued": "Ny sang lagt til i køen", - "estimated": "estimert" + "estimated": "estimert", + "toolong": "Sang for lang. `{_length}>{_max}`" }, "history": { "title": "Avspillingshistorikk", diff --git a/localization/nn_no/music.json b/localization/nn_no/music.json index 3de3484..788c1cf 100644 --- a/localization/nn_no/music.json +++ b/localization/nn_no/music.json @@ -72,7 +72,8 @@ "position": "Køposisjon", "playing_in": "Spelar av om", "enqueued": "Ny song lagt til i køa", - "estimated": "estimert" + "estimated": "estimert", + "toolong": "Song for lang. `{_length}>{_max}`" }, "history": { "title": "Avspelingshistorikk",