Example #1
0
 def load_guild(self, guild):
     guild_row = Guild.get_or_create(serverid=guild.id)[0]
     for row in guild_row.admin_roles:
         role = guild.get_role(row.roleid)
         if role:
             self.admin_roles[guild.id].add(role.id)
         else:
             row.delete_instance()
     for row in guild_row.mod_roles:
         role = guild.get_role(row.roleid)
         if role:
             self.mod_roles[guild.id].add(role.id)
         else:
             row.delete_instance()
     for row in guild_row.trusted_roles:
         role = guild.get_role(row.roleid)
         if role:
             self.trusted_roles[guild.id].add(role.id)
         else:
             row.delete_instance()
     for row in guild_row.command_permissions:
         member = guild.get_member(row.userid)
         if member:
             self.command_permissions[guild.id][member.id] = row
         else:
             row.delete_instance()
Example #2
0
 async def channels(self, ctx):
     # TODO: allow guild admins to use this, restricted to single guild
     embed = Embed(
         timestamp=ctx.message.created_at,
         color=0x50f3d7,
         title='Bug Reporting Channels')
     guild_row = Guild.get_or_none(serverid=ctx.guild.id)
     guild_channels = []
     non_guild_channels = dict()
     for row in BugReportingPlatform.select():
         for channel_row in row.bug_channels:
             channel = self.bot.get_channel(channel_row.channelid)
             if not channel:
                 channel_row.delete_instance()
                 continue
             description = f"{row.platform}/{row.branch}: {channel.mention}"
             if channel_row.guild == guild_row:
                 guild_channels.append(description)
             else:
                 # TODO: get guild names and add to description
                 if channel_row.guild.serverid not in non_guild_channels:
                     non_guild_channels[channel_row.guild.serverid] = []
                 non_guild_channels[channel_row.guild.serverid].append(description)
     if guild_channels:
         embed.add_field(name=f'`{ctx.guild.name}` server', value="\n".join(guild_channels))
     for guild_id, channel_list in non_guild_channels.items():
         guild = self.bot.get_guild(guild_id)
         server_name = self.bot.get_guild(guild_id).name or f"[{guild_id}][MISSING GUILD]"
         embed.add_field(name=f'`{server_name}` server', value="\n".join(channel_list))
     if not guild_channels and not non_guild_channels:
         await ctx.send("There are no configured bug reporting channels")
     else:
         await ctx.send(embed=embed)
Example #3
0
 def get_guild_db_config(self, guild_id):
     try:
         if guild_id in Utils.GUILD_CONFIGS:
             return Utils.GUILD_CONFIGS[guild_id]
         row = Guild.get_or_create(serverid=guild_id)[0]
         Utils.GUILD_CONFIGS[guild_id] = row
         return row
     except Exception as e:
         Utils.get_embed_and_log_exception("--------Failed to get config--------", self, e)
         return None
Example #4
0
 async def on_guild_remove(self, guild):
     del Utils.GUILD_CONFIGS[guild.id]
     # keep guild record and clear channel configs and default lang
     guild_row = Guild.get(serverid=guild.id)
     guild_row.memberrole = 0
     guild_row.nonmemberrole = 0
     guild_row.mutedrole = 0
     guild_row.welcomechannelid = 0
     guild_row.ruleschannelid = 0
     guild_row.logchannelid = 0
     guild_row.entrychannelid = 0
     guild_row.rulesreactmessageid = 0
     guild_row.defaultlocale = ''
     guild_row.save()
Example #5
0
    async def set_channel_locale(self, ctx, locale: str, channel_id: int = 0):
        """
        Set Locale for a specific channel

        locale: Locale string, or one of ["*", "x", "none", "unset", "off"] to unset
        channel_id: ID for channel to set, or do not provide ID and invocation channel will be used.
        """
        # TODO: add ALL_LOCALES as channel option
        if locale not in Lang.locales and locale not in self.unset_str:
            await ctx.send(Lang.get_locale_string('lang/unknown_locale', ctx, locale=locale, locale_lsit=Lang.locales))
            return

        # use input channel, or if input is 0, use channel from command context
        channel_id = ctx.channel.id if channel_id == 0 else channel_id

        guild_row = Guild.get_or_create(serverid=ctx.guild.id)[0]
        old_value = None

        localization_row = Localization.select().join(Guild).where(
            (Guild.serverid == ctx.guild.id) &
            (Localization.channelid == channel_id))

        if len(localization_row) == 1:
            localization_row = localization_row[0]
            old_value = localization_row.locale
        else:
            localization_row = None

        if locale in self.unset_str:
            if not localization_row:
                await ctx.send(Lang.get_locale_string('lang/channel_not_unset', ctx, channelid=channel_id))
            else:
                localization_row.delete_instance()
                await ctx.send(Lang.get_locale_string('lang/channel_unset', ctx, old_value=old_value))
            return

        if not localization_row:
            localization_row = Localization.create(guild=guild_row, channelid=channel_id)

        if localization_row.locale == locale:
            await ctx.send(Lang.get_locale_string('lang/channel_already_set', ctx, channelid=channel_id, locale=locale))
            return

        localization_row.locale = locale
        localization_row.save()
        await ctx.send(Lang.get_locale_string('lang/channel_set', ctx, channelid=channel_id, locale=locale))
Example #6
0
def get_defaulted_locale(ctx):
    locale = 'en_US'
    if isinstance(ctx, Context):
        # TODO: move guild/channel checks to LangConfig, store in dict, update there on guild events and config changes
        cid = ctx.channel.id

        if ctx.guild is None:
            # DM - default the language
            locale = Configuration.get_var('broadcast_locale', 'en_US')
            if locale == ALL_LOCALES:
                return locales
            return [locale]

        # TODO: create lookup table so we don't hit database every time
        #  github issue #91
        gid = ctx.guild.id
        guild_row = Guild.get_or_none(serverid=gid)
        chan_locale = Localization.get_or_none(channelid=cid)

        # Bot default is English
        if guild_row is not None and guild_row.defaultlocale in locales:
            # server locale overrides bot default
            locale = guild_row.defaultlocale
        if chan_locale is not None and chan_locale.locale in locales:
            # channel locale overrides server
            locale = chan_locale.locale
    elif isinstance(ctx, str):
        # String assumes caller knows better and is overriding all else
        if ctx == ALL_LOCALES:
            return locales
        if ctx not in locales:
            if ctx != '':
                Logging.info(
                    f"Locale string override '{ctx}' not found. Defaulting.")
        else:
            locale = ctx
    else:
        Logging.info(f"Cannot derive locale from context: {ctx}")
        locale = False

    if locale not in locales:
        Logging.info(f"Missing locale {locale} - defaulting to English")
        locale = 'en_US'
    return [locale]
Example #7
0
    async def set_server_locale(self, ctx, locale: str):
        """Set default locale for this server"""
        if locale not in Lang.locales and locale not in self.unset_str:
            await ctx.send(Lang.get_locale_string('lang/unknown_locale', ctx, locale=locale, locale_lsit=Lang.locales))
            return

        if locale in self.unset_str:
            locale = ""
        guild_row = Guild.get_or_create(serverid=ctx.guild.id)[0]

        # Don't set/save if input arg is already default
        if locale == guild_row.defaultlocale:
            await ctx.send(
                Lang.get_locale_string('lang/default_not_changed', ctx, locale=locale, server_name=ctx.guild.name))
            return

        guild_row.defaultlocale = locale
        guild_row.save()
        await ctx.send(Lang.get_locale_string('lang/default_set', ctx, locale=locale, server_name=ctx.guild.name))
Example #8
0
    async def lang(self, ctx):
        """Show language settings for this server"""
        channels = []
        embed = discord.Embed(
            timestamp=ctx.message.created_at,
            color=0x663399,
            title=Lang.get_locale_string('lang/lang_settings_title', ctx, server_name=ctx.guild.name))

        guild_row = Guild.get_or_none(serverid=ctx.guild.id)
        if guild_row:
            embed.add_field(name="Server default", value=guild_row.defaultlocale or "none")

        localization_rows = Localization.select().join(Guild).where(Guild.serverid == ctx.guild.id)
        for row in localization_rows:
            channels.append(self.bot.get_channel(row.channelid).mention)
            embed.add_field(name=f"#{self.bot.get_channel(row.channelid).name}",
                            value=row.locale,
                            inline=True)
        await ctx.send(embed=embed)
Example #9
0
    def init_guild(self, guild_id):
        watch = ReactWatch.get_or_create(serverid=guild_id)[0]
        self.mutes[guild_id] = Configuration.get_persistent_var(f"react_mutes_{guild_id}", dict())
        self.min_react_lifespan[guild_id] = Configuration.get_persistent_var(f"min_react_lifespan_{guild_id}", 0.5)
        self.mute_duration[guild_id] = watch.muteduration

        # track react add/remove per guild
        self.recent_reactions[guild_id] = dict()
        self.react_removers[guild_id] = dict()
        self.react_adds[guild_id] = dict()

        # list of emoji to watch
        self.emoji[guild_id] = dict()
        for e in watch.emoji:
            self.emoji[guild_id][e.emoji] = e

        # enable listening if set in db
        if watch.watchremoves:
            self.activate_react_watch(guild_id)

        self.guilds[guild_id] = Guild.get_or_create(serverid=guild_id)[0]
Example #10
0
    async def add_channel(self, ctx, channel: TextChannel, platform, branch):
        try:
            guild_row = Guild.get(serverid=ctx.guild.id)
        except DoesNotExist:
            await ctx.send(f"I couldn't find a record for guild id {ctx.guild.id}... call a plumber!")
            return

        try:
            platform_row = BugReportingPlatform.get(platform=platform, branch=branch)
        except DoesNotExist:
            await ctx.send(f"I couldn't find a record for platform/branch `{platform}`/`{branch}`")
            return

        try:
            record, created = BugReportingChannel.get_or_create(guild=guild_row, platform=platform_row, channelid=channel.id)
        except IntegrityError:
            await ctx.send(f"channel{channel.mention} is already in used for bug reporting")
            return

        if created:
            await ctx.send(f"{channel.mention} will now be used to record `{platform}/{branch}` bug reports")
        else:
            await ctx.send(f"{channel.mention} was already configured for `{platform}/{branch}` bug reports")
Example #11
0
 def init_guild(self, guild):
     row = Guild.get_or_create(serverid=guild.id)[0]
     Utils.GUILD_CONFIGS[guild.id] = row
     return row
Example #12
0
    async def permission_config(self, ctx):
        is_bot_admin = await self.bot.permission_manage_bot(ctx)

        embed = discord.Embed(timestamp=ctx.message.created_at,
                              color=0x663399,
                              title=Lang.get_locale_string(
                                  "permission_config/info",
                                  ctx,
                                  server_name=ctx.guild.name))
        embed.add_field(
            name='Configurable Permissions',
            value=
            f"Trusted Roles, Mod Roles, Admin Roles{', Bot Admins' if is_bot_admin else ''}",
            inline=False)

        admin_roles = set()
        mod_roles = set()
        trusted_roles = set()
        user_permissions = set()

        guild_row = Guild.get(serverid=ctx.guild.id)

        for row in guild_row.admin_roles:
            role = ctx.guild.get_role(row.roleid)
            if role:
                admin_roles.add(role.mention)
        for row in guild_row.mod_roles:
            role = ctx.guild.get_role(row.roleid)
            if role:
                mod_roles.add(role.mention)
        for row in guild_row.trusted_roles:
            role = ctx.guild.get_role(row.roleid)
            if role:
                trusted_roles.add(role.mention)
        for row in guild_row.command_permissions:
            member = ctx.guild.get_member(row.userid)
            if member:
                member_desc = Utils.get_member_log_name(member)
                desc = f"{member_desc} - `{'ALLOW' if row.allow else 'DENY'}`: `{row.command}`"
                user_permissions.add(desc)

        embed.add_field(name="Admin Roles",
                        value='\n'.join(admin_roles),
                        inline=False)
        embed.add_field(name="Mod Roles",
                        value='\n'.join(mod_roles),
                        inline=False)
        embed.add_field(name="Trusted Roles",
                        value='\n'.join(trusted_roles),
                        inline=False)
        embed.add_field(name="User Permissions",
                        value='\n'.join(user_permissions),
                        inline=False)

        if is_bot_admin:
            bot_admins = set()
            for row in BotAdmin.select():
                user = self.bot.get_user(row.userid)
                bot_admins.add(f"{user.mention} {str(user)} ({user.id})")
            embed.add_field(name="Bot Admins",
                            value='\n'.join(bot_admins),
                            inline=False)

        await ctx.send(embed=embed)