From e8225d42f032d96b8d1dd4e1660fbd2f153fec2d Mon Sep 17 00:00:00 2001 From: Ev-1 <33662061+Ev-1@users.noreply.github.com> Date: Sun, 3 Feb 2019 14:06:40 +0100 Subject: [PATCH] More guild settings. --- bot.py | 5 +- cogs/botsettings.py | 151 +++++++++++++++++++++++++++++++++++++---- cogs/cogs.py | 6 +- cogs/utils/settings.py | 2 +- 4 files changed, 146 insertions(+), 18 deletions(-) diff --git a/bot.py b/bot.py index cb68ff2..dcea363 100644 --- a/bot.py +++ b/bot.py @@ -36,6 +36,7 @@ class Bot(commands.Bot): description=config["description"]) self.settings = Settings(**config) + # Todo, maybe fix. Ability to set default bot lang in settings self.localizer = Localizer(self.settings.default_lang_path, "en_en") self.debug = debug @@ -63,7 +64,9 @@ class Bot(commands.Bot): pass elif isinstance(err, commands.NoPrivateMessage): - await ctx.send(self.localizer.format_string("{commands.not_available_dm}", setting.get_locale(ctx.guild.id))) + # Todo, fix, use default bot lang. + await ctx.send('That command is not available in DMs') + #await ctx.send(self.localizer.format_str("{commands.not_available_dm}", self.settings.get(ctx.guild.id, 'locale', 'default_locale'))) elif isinstance(err, commands.CheckFailure): pass diff --git a/cogs/botsettings.py b/cogs/botsettings.py index 6cd3cae..1b6df8f 100644 --- a/cogs/botsettings.py +++ b/cogs/botsettings.py @@ -2,6 +2,7 @@ import discord import os import json import codecs + from discord.ext import commands from cogs.utils import checks @@ -10,12 +11,19 @@ class BotSettings: self.bot = bot self.settings = self.bot.settings + def format_prefixes(self, prefixes): + if prefixes is None: + prefixes = [self.bot.settings.default_prefix] + formatted = '' + for prefix in prefixes: + formatted += f'`{prefix}`, ' + return formatted[:-2] + @checks.is_admin() @commands.group(name='set', hidden=True) async def _set(self, ctx): if ctx.invoked_subcommand is None: - await ctx.invoke(self.bot.get_command('help'), - ctx.command.qualified_name) + await self.current_settings.invoke(ctx) @commands.guild_only() @_set.command(name='serverlocale') @@ -40,19 +48,106 @@ class BotSettings: prefixes = self.settings.get(ctx.guild.id, 'prefixes', 'default_prefix') await ctx.send(self.format_prefixes(prefixes)) - def format_prefixes(self, prefixes): - if prefixes is None: - prefixes = [self.bot.settings.default_prefix] - formatted = '' - for prefix in prefixes: - formatted += f'`{prefix}`, ' - return formatted[:-2] - @commands.guild_only() - @_set.command(name='setmodrole') + @_set.command(name='modrole') async def set_mod_role(self, ctx, modrole: discord.Role): await ctx.send(f'{modrole.name} {modrole.id}') + @commands.guild_only() + @_set.command(name='threshold') + async def set_vote_threshold(self, ctx, threshold: int=50): + if not 0 <= threshold <= 100: + return await ctx.send('Must be between 0 and 100') + + self.bot.settings.set(ctx.guild.id, 'vote_threshold', threshold) + threshold = self.bot.settings.get(ctx.guild.id, 'vote_threshold', 50) + await ctx.send(f'Vote threshold set to {threshold}%') + + @commands.guild_only() + @_set.command(name='textchannels') + async def set_music_text(self, ctx, *channels: discord.TextChannel): + if channels: + channel_ids = [channel.id for channel in channels] + self.bot.settings.set(ctx.guild.id, 'channels.text', channel_ids) + else: + self.bot.settings.set(ctx.guild.id, 'channels.text', None) + + textchannels = self.bot.settings.get(ctx.guild.id, 'channels.text') + + if textchannels: + embed = discord.Embed(title='Music command channels set', color=ctx.me.color) + channels = [ctx.guild.get_channel(channel) for channel in textchannels] + mentioned = [channel.mention for channel in channels if channel is not None] + embed.description = ', '.join(mentioned) + await ctx.send(embed=embed) + else: + embed = discord.Embed(color=ctx.me.color, description='Music command channels cleared') + await ctx.send(embed=embed) + + @commands.guild_only() + @_set.command(name='voicechannels') + 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.id, 'channels.voice', channel_ids) + else: + self.bot.settings.set(ctx.guild.id, 'channels.voice', None) + + voicechannels = self.bot.settings.get(ctx.guild.id, 'channels.voice') + + if voicechannels: + embed = discord.Embed(title='Music channels set', color=ctx.me.color) + channels = [ctx.guild.get_channel(channel) for channel in voicechannels] + mentioned = [channel.name for channel in channels if channel is not None] + embed.description = ', '.join(mentioned) + await ctx.send(embed=embed) + else: + embed = discord.Embed(color=ctx.me.color, description='Music channels cleared') + await ctx.send(embed=embed) + + @commands.guild_only() + @_set.command(name='listenchannels') + async def set_listen_voice(self, ctx, *channels: discord.VoiceChannel): + if channels: + channel_ids = [channel.id for channel in channels] + self.bot.settings.set(ctx.guild.id, 'channels.listen_only', channel_ids) + else: + self.bot.settings.set(ctx.guild.id, 'channels.listen_only', None) + + listenchannels = self.bot.settings.get(ctx.guild.id, 'channels.listen_only') + + if listenchannels: + embed = discord.Embed(title='Listen only channels set', color=ctx.me.color) + channels = [ctx.guild.get_channel(channel) for channel in listenchannels] + mentioned = [channel.name for channel in channels if channel is not None] + embed.description = ', '.join(mentioned) + await ctx.send(embed=embed) + else: + embed = discord.Embed(color=ctx.me.color, description='Listen only channels cleared') + await ctx.send(embed=embed) + + @commands.guild_only() + @_set.command(name='djroles') + async def set_dj_roles(self, ctx, *roles: discord.Role): + print(roles) + role_ids = [role.id for role in roles] + if roles: + self.bot.settings.set(ctx.guild.id, 'roles.dj', role_ids) + else: + self.bot.settings.set(ctx.guild.id, 'roles.dj', None) + + djroles = self.bot.settings.get(ctx.guild.id, 'roles.dj') + + if djroles: + embed = discord.Embed(title='DJ roles set to', color=ctx.me.color) + roles = [ctx.guild.get_role(role) for role in djroles] + mentioned = [role.name for role in roles if role is not None] + embed.description = ', '.join(mentioned) + await ctx.send(embed=embed) + else: + embed = discord.Embed(color=ctx.me.color, description='DJ roles cleared') + await ctx.send(embed=embed) + @commands.guild_only() @_set.command(name='current') async def current_settings(self, ctx): @@ -61,10 +156,40 @@ class BotSettings: embed.set_thumbnail(url=ctx.guild.icon_url) prefixes = self.bot.settings.get(ctx.guild.id, 'prefixes', 'default_prefix') + embed.add_field(name='Prefix', value=self.format_prefixes(prefixes)) + locale = f"{self.bot.settings.get(ctx.guild.id, 'locale', 'default_locale')}" embed.add_field(name='Locale', value=locale) - embed.add_field(name='Prefix', value=self.format_prefixes(prefixes)) - await ctx.send(embed=embed) + + threshold = self.bot.settings.get(ctx.guild.id, 'vote_threshold', 50) + embed.add_field(name='Vote threshold', value=f'{threshold}%') + + textchannels = self.bot.settings.get(ctx.guild.id, 'channels.text') + if textchannels: + channels = [ctx.guild.get_channel(channel) for channel in textchannels] + 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.id, 'channels.voice') + 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)) + + listenchannels = self.bot.settings.get(ctx.guild.id, 'channels.listen_only') + if listenchannels: + channels = [ctx.guild.get_channel(channel) for channel in listenchannels] + mentioned = [channel.name for channel in channels if channel is not None] + embed.add_field(name='Listen only channels', value='\n'.join(mentioned)) + + djroles = self.bot.settings.get(ctx.guild.id, 'roles.dj') + if djroles: + roles = [ctx.guild.get_role(role) for role in djroles] + mentioned = [role.name for role in roles if role is not None] + embed.add_field(name='Dj roles', value=', '.join(mentioned)) + + msg = await ctx.send(embed=embed) + return msg def setup(bot): diff --git a/cogs/cogs.py b/cogs/cogs.py index 2ad85ce..13b70cf 100644 --- a/cogs/cogs.py +++ b/cogs/cogs.py @@ -55,10 +55,10 @@ class Cogs: """Reloads all extensions""" try: for extension in self.bot.extensions: - if extension == 'cogs': + if extension == 'cogs.cogs': continue - self.bot.unload_extension(f'cogs.{extension}') - self.bot.load_extension(f'cogs.{extension}') + self.bot.unload_extension(f'{extension}') + self.bot.load_extension(f'{extension}') await ctx.send('Extensions reloaded') except Exception as e: await ctx.send(f'```py\n{traceback.format_exc()}\n```') diff --git a/cogs/utils/settings.py b/cogs/utils/settings.py index 97b9406..473a617 100644 --- a/cogs/utils/settings.py +++ b/cogs/utils/settings.py @@ -78,7 +78,7 @@ class Settings: class attribute the value of the attribute will get returned.""" guild_id = str(guild_id) - if default and hasattr(self, default): + if default and isinstance(default, str) and hasattr(self, default): default = getattr(self, default) elif not default: default = None