async def add_lvl4(self, ctx, command:str, person:discord.Member): command_object = self.bot.get_command(command) if command_object is not None: cog_name = command_object.cog_name overrides = Configuration.get_var(ctx.guild.id, "PERM_OVERRIDES") if cog_name not in overrides: overrides[cog_name] = { "required": -1, "commands": {}, "people": [] } override = overrides[cog_name] parts = command.split(" ") while len(parts) > 0: part = parts.pop(0) if not part in override["commands"]: override["commands"][part] = override = { "required": -1, "commands": {}, "people": [] } else: override = override["commands"][part] if person.id not in override["people"]: override["people"].append(person.id) await ctx.send(f"{Emoji.get_chat_emoji('YES')} {Translator.translate('lvl4_added', ctx, member=person, command=command)}") else: await ctx.send( f"{Emoji.get_chat_emoji('NO')} {Translator.translate('already_had_lvl4', ctx, member=person, command=command)}") Configuration.save(ctx.guild.id) else: await ctx.send(f"{Emoji.get_chat_emoji('NO')} {Translator.translate('command_not_found', ctx)}")
async def add_cog_override(self, ctx, cog:str, perm_lvl:int): cog = cog if cog in ctx.bot.cogs.keys(): cogo = ctx.bot.cogs[cog] if not hasattr(cogo, "permissions"): await ctx.send(f"{Emoji.get_chat_emoji('NO')} {Translator.translate('core_cog_no_override', ctx, cog=cog)}") elif perm_lvl in range(7): min_lvl = cogo.permissions["min"] max_lvl = cogo.permissions["max"] if perm_lvl < min_lvl: await ctx.send(f"{Emoji.get_chat_emoji('NO')} {Translator.translate('cog_min_perm_violation', ctx, cog=cog, min_lvl=min_lvl, min_lvl_name=Translator.translate(f'perm_lvl_{min_lvl}', ctx))}") elif perm_lvl > max_lvl: await ctx.send( f"{Emoji.get_chat_emoji('NO')} {Translator.translate('cog_max_perm_violation', ctx, cog=cog, max_lvl=max_lvl, max_lvl_name=Translator.translate(f'perm_lvl_{max_lvl}', ctx))}") else: overrides = Configuration.get_var(ctx.guild.id, "PERM_OVERRIDES") if cog not in overrides: overrides[cog] = { "required": perm_lvl, "commands": {}, "people": [] } else: overrides[cog]["required"] = perm_lvl Configuration.save(ctx.guild.id) await ctx.send(f"{Emoji.get_chat_emoji('YES')} {Translator.translate('cog_override_applied', ctx, cog=cog, perm_lvl=perm_lvl, perm_lvl_name=Translator.translate(f'perm_lvl_{perm_lvl}', ctx))}") else: await ctx.send(f"{Emoji.get_chat_emoji('NO')} {Translator.translate('invalid_override_lvl', ctx)}") else: await ctx.send(f"{Emoji.get_chat_emoji('NO')} {Translator.translate('cog_not_found', ctx)}")
async def remove_cog_override(self, ctx, cog: str): overrides = Configuration.get_var(ctx.guild.id, "PERM_OVERRIDES") if cog in overrides: overrides[cog]["required"] = -1 Configuration.save(ctx.guild.id) await ctx.send(f"{Emoji.get_chat_emoji('YES')} {Translator.translate('cog_override_removed', ctx, cog=cog)}") else: await ctx.send(f"{Emoji.get_chat_emoji('NO')} {Translator.translate('cog_override_not_found', ctx, cog=cog)}")
async def blacklist_remove(self, ctx, *, word: str): blacklist = Configuration.get_var(ctx.guild.id, "WORD_BLACKLIST") if word not in blacklist: await MessageUtils.send_to(ctx, "NO", "not_blacklisted", word=word) else: blacklist.remove(word) await MessageUtils.send_to(ctx, "YES", "entry_removed", entry=word) Configuration.save(ctx.guild.id)
async def blacklist_add(self, ctx, *, word: str): blacklist = Configuration.get_var(ctx.guild.id, "CENSORING", "TOKEN_BLACKLIST") if word.lower() in blacklist: await MessageUtils.send_to(ctx, "NO", "already_blacklisted", word=word) else: blacklist.append(word.lower()) await MessageUtils.send_to(ctx, "YES", "entry_added", entry=word) Configuration.save(ctx.guild.id)
async def on_guild_channel_delete(self, channel): changed = False for name in ["IGNORED_CHANNELS_CHANGES", "IGNORED_CHANNELS_OTHER"]: channels = Configuration.get_var(channel.guild.id, name) if channel.id in channels: channels.remove(channel.id) changed = True if changed: Configuration.save(channel.guild.id)
async def ignored_channels_changes_remove(self, ctx, channel: TextChannel): """ignored_channels_remove_help""" channels = Configuration.get_var(ctx.guild.id, "MESSAGE_LOGS", 'IGNORED_CHANNELS_CHANGES') if not channel.id in channels: await MessageUtils.send_to(ctx, 'NO', 'ignored_channels_not_on_list', channel=channel.mention) else: channels.remove(channel.id) await MessageUtils.send_to(ctx, 'YES', 'ignored_channels_changes_removed', channel=channel.mention) Configuration.save(ctx.guild.id)
async def ignored_channels_edits_remove(self, ctx, channel: TextChannel): """ignored_channels_remove_help""" channels = Configuration.get_var(ctx.guild.id, 'IGNORED_CHANNELS_OTHER') if channel.id not in channels: await MessageUtils.send_to(ctx, 'NO', 'ignored_channels_not_on_list') else: channels.remove(channel.id) await MessageUtils.send_to(ctx, 'YES', 'ignored_channels_edits_removed', channel=channel.mention) Configuration.save(ctx.guild.id)
async def ignored_channels_changes_add(self, ctx, channel:TextChannel): """ignored_channels_add_help""" channels = Configuration.get_var(ctx.guild.id, 'IGNORED_CHANNELS_CHANGES') if channel.id in channels: await MessageUtils.send_to(ctx, 'NO', 'ignored_channels_already_on_list') else: channels.append(channel.id) await MessageUtils.send_to(ctx, 'YES', 'ignored_channels_changes_added', channel=channel.mention) Configuration.save(ctx.guild.id)
async def on_guild_role_delete(self, role): lists = ["ROLE_LIST", "TRUSTED_ROLES", "MOD_ROLES", "ADMIN_ROLES"] changed = False for l in lists: roles = Configuration.get_var(role.guild.id, l) if role.id in roles: roles.remove(role.id) changed = True if changed: Configuration.save(role.guild.id)
async def word_blacklist_remove(self, ctx, *, word: str): blacklist = Configuration.get_var(ctx.guild.id, "CENSORING", "WORD_BLACKLIST") if word not in blacklist: await MessageUtils.send_to(ctx, "NO", "not_blacklisted", word=word) else: blacklist.remove(word) await MessageUtils.send_to(ctx, "YES", "entry_removed", entry=word) Configuration.save(ctx.guild.id) if ctx.guild.id in self.bot.get_cog("Censor").regexes: del self.bot.get_cog("Censor").regexes[ctx.guild.id]
async def role_list_remove(self, ctx, *, role: discord.Role): """configure_role_list_remove""" roles = Configuration.get_var(ctx.guild.id, "ROLE_LIST") mode = "whitelist" if Configuration.get_var(ctx.guild.id, "ROLE_WHITELIST") else "blacklist" if role.id not in roles: await MessageUtils.send_to(ctx, "NO", f"role_list_rmv_fail_{mode}", role=Utils.escape_markdown(role.name)) else: roles.remove(role.id) Configuration.save(ctx.guild.id) await MessageUtils.send_to(ctx, "YES", f"role_list_rmv_confirmation_{mode}", role=Utils.escape_markdown(role.name))
async def blacklist_add(self, ctx, *, word: str): blacklist = Configuration.get_var(ctx.guild.id, "WORD_BLACKLIST") if word in blacklist: await MessageUtils.send_to(ctx, "NO", "already_blacklisted", word=word) elif len(word) < 3: await MessageUtils.send_to(ctx, "NO", "entry_too_short") else: blacklist.append(word) await MessageUtils.send_to(ctx, "YES", "entry_added", entry=word) Configuration.save(ctx.guild.id)
async def word_blacklist_add(self, ctx, *, word: str): blacklist = Configuration.get_var(ctx.guild.id, "CENSORING", "WORD_BLACKLIST") if word.lower() in blacklist: await MessageUtils.send_to(ctx, "NO", "already_blacklisted", word=word) else: blacklist.append(word.lower()) await MessageUtils.send_to(ctx, "YES", "entry_added", entry=word) Configuration.save(ctx.guild.id) if ctx.guild.id in self.bot.get_cog("Censor").regexes: del self.bot.get_cog("Censor").regexes[ctx.guild.id]
async def remove_item(ctx, item, item_type, list_name="roles"): roles = Configuration.get_var(ctx.guild.id, f"{item_type}_{list_name}".upper()) sname = list_name[:-1] if list_name[-1:] == "s" else list_name if item.id not in roles: await ctx.send( f"{Emoji.get_chat_emoji('NO')} {Translator.translate(f'was_no_{item_type}_{sname}', ctx, item=item.name)}") else: roles.remove(item.id) Configuration.save(ctx.guild.id) await ctx.send( f"{Emoji.get_chat_emoji('YES')} {Translator.translate(f'{item_type}_{sname}_removed', ctx, item=item.name)}")
def validate_configs(self): for guild in self.bot.guilds: for type in ("TRUSTED", "MOD", "ADMIN"): to_remove = [] roles = Configuration.get_var(guild.id, type + "_ROLES") for role in roles: if guild.get_role(role) is None: to_remove.append(role) for role in to_remove: roles.remove(role) Configuration.save(guild.id)
async def role_list_add(self, ctx, *, role:discord.Role): """configure_role_list_add""" roles = Configuration.get_var(ctx.guild.id, "ROLE_LIST") mode = "whitelist" if Configuration.get_var(ctx.guild.id, "ROLE_WHITELIST") else "blacklist" if role == ctx.guild.default_role: await MessageUtils.send_to(ctx, "NO", "default_role_forbidden") elif role.id in roles: await MessageUtils.send_to(ctx, "NO", f"role_list_add_fail_{mode}", role=Utils.escape_markdown(role.name)) else: roles.append(role.id) Configuration.save(ctx.guild.id) await MessageUtils.send_to(ctx, "YES", f"role_list_add_confirmation_{mode}", role=Utils.escape_markdown(role.name))
async def blacklist_add(self, ctx, *, entry: str): """Add a new entry to the list""" guild_id = ctx.guild.id existing_matches = self.get_matches_pretty(guild_id, entry) if len(existing_matches) > 0: out = '\n'.join(existing_matches) await ctx.send( f"This name is already covered with existing entries: \n{out}") else: blacklist = Configuration.get_var(guild_id, "BAD_NAMES") blacklist.append(entry) Configuration.save(guild_id) self.assemble_detector(ctx.guild) await ctx.send(f"``{entry}`` has been added to the blacklist")
async def add_item(ctx, item, item_type, list_name="roles"): roles = Configuration.get_var(ctx.guild.id, f"{item_type}_{list_name}".upper()) sname = list_name[:-1] if list_name[-1:] == "s" else list_name if item == ctx.guild.default_role: return await ctx.send( f"{Emoji.get_chat_emoji('NO')} {Translator.translate(f'default_role_forbidden', ctx)}") if item.id in roles: await ctx.send( f"{Emoji.get_chat_emoji('NO')} {Translator.translate(f'already_{item_type}_{sname}', ctx, item=item.name)}") else: roles.append(item.id) Configuration.save(ctx.guild.id) await ctx.send( f"{Emoji.get_chat_emoji('YES')} {Translator.translate(f'{item_type}_{sname}_added', ctx, item=item.name)}")
async def remove_command_override(self, ctx, command:str): command = command.lower() command_object = self.bot.get_command(command) if command_object is not None: cog_name = command_object.cog_name overrides = Configuration.get_var(ctx.guild.id, "PERM_OVERRIDES") found = False if cog_name in overrides: override = Permissioncheckers.get_perm_dict(command_object.qualified_name.split(" "), overrides[cog_name], True) if override is not None: found = True override["required"] = -1 Configuration.save(ctx.guild.id) await ctx.send(f"{Emoji.get_chat_emoji('YES')} {Translator.translate('command_override_removed', ctx, command=command)}") if not found: await ctx.send(f"{Emoji.get_chat_emoji('NO')} {Translator.translate('command_override_not_found', ctx, command=command)}")
async def remove_logging(self, ctx, cid: LoggingChannel, *, types): channel = self.bot.get_channel(int(cid)) channels = Configuration.get_var(ctx.guild.id, "LOG_CHANNELS") if cid not in channels: await ctx.send(f"{Emoji.get_chat_emoji('NO')} {Translator.translate('no_log_channel', ctx, channel=f'<{cid}>')}") else: info = channels[cid] removed = [] ignored = [] unable = [] known, unknown = self.extract_types(types) message = "" for t in known: if t in info: if self.can_remove(ctx.guild.id, t): removed.append(t) info.remove(t) else: unable.append(t) else: ignored.append(t) if len(removed) > 0: message += f"{Emoji.get_chat_emoji('YES')} {Translator.translate('logs_disabled_channel', ctx, channel=channel.mention if channel is not None else cid)}{', '.join(removed)}" if len(ignored) > 0: message += f"\n{Emoji.get_chat_emoji('WARNING')}{Translator.translate('logs_already_disabled_channel', ctx, channel=channel.mention if channel is not None else cid)}{', '.join(ignored)}" if len(unable) > 0: message += f"\n {Emoji.get_chat_emoji('NO')}{Translator.translate('logs_unable', ctx)} {', '.join(unable)}" if len(unknown) > 0: message += f"\n {Emoji.get_chat_emoji('NO')}{Translator.translate('logs_unknown', ctx)}{', '.join(unknown)}" if len(info) > 0: embed = discord.Embed(color=6008770) embed.add_field(name=cid, value=self.get_channel_properties(ctx, cid, channels[cid])) else: embed=None await ctx.send(message, embed=embed) empty = [] for cid, info in channels.items(): if len(info) is 0: empty.append(cid) for e in empty: del channels[e] Configuration.save(ctx.guild.id)
async def remove_lvl4(self, ctx, command: str, person: discord.Member): command_object = self.bot.get_command(command) if command_object is not None: cog_name = command_object.cog_name overrides = Configuration.get_var(ctx.guild.id, "PERM_OVERRIDES") found = False if cog_name in overrides: lvl4_list = Permissioncheckers.get_perm_dict(command.split(" "), overrides[cog_name], strict=True) if lvl4_list is not None and person.id in lvl4_list["people"]: found = True if found: lvl4_list["people"].remove(person.id) await ctx.send(f"{Emoji.get_chat_emoji('YES')} {Translator.translate('lvl4_removed', ctx, member=person, command=command)}") Configuration.save(ctx.guild.id) else: await ctx.send( f"{Emoji.get_chat_emoji('NO')} {Translator.translate('did_not_have_lvl4', ctx, member=person, command=command)}")
async def add_command_override(self, ctx, command:str, perm_lvl:int): command = command.lower() command_object = self.bot.get_command(command) if command_object is not None: cog = command_object.instance cog_name = command_object.cog_name if not hasattr(cog, "permissions"): await ctx.send(f"{Emoji.get_chat_emoji('NO')} {Translator.translate('command_core_cog_no_override', ctx, command=command, cog_name=cog_name)}") elif perm_lvl in range(7): perm_dict = Permissioncheckers.get_perm_dict(command_object.qualified_name.split(" "), cog.permissions) if perm_lvl < perm_dict["min"]: lvl = cog.permissions['min'] await ctx.send(f"{Emoji.get_chat_emoji('NO')} {Translator.translate('command_min_perm_violation', ctx, command=command, min_lvl=lvl, min_lvl_name=Translator.translate(f'perm_lvl_{lvl}', ctx))}") elif perm_lvl > perm_dict["max"]: lvl = cog.permissions['max'] await ctx.send(f"{Emoji.get_chat_emoji('NO')} {Translator.translate('command_max_perm_violation', ctx, command=command, max_lvl=lvl, max_lvl_name=Translator.translate(f'perm_lvl_{lvl}', ctx))}") else: overrides = Configuration.get_var(ctx.guild.id, "PERM_OVERRIDES") if cog_name not in overrides: overrides[cog_name] = { "required": -1, "commands": {}, "people": [] } override = overrides[cog_name] parts = command_object.qualified_name.split(" ") while len(parts) > 0: part = parts.pop(0) if not part in override["commands"]: override["commands"][part] = override = { "required": -1, "commands": {}, "people": [] } else: override = override["commands"][part] override["required"] = perm_lvl Configuration.save(ctx.guild.id) await ctx.send(f"{Emoji.get_chat_emoji('YES')} {Translator.translate('command_override_confirmation', ctx, command=command, perm_lvl=perm_lvl, perm_lvl_name=Translator.translate(f'perm_lvl_{perm_lvl}', ctx))}") else: await ctx.send(f"{Emoji.get_chat_emoji('NO')} {Translator.translate('invalid_override_lvl', ctx)}") else: await ctx.send(f"{Emoji.get_chat_emoji('NO')} {Translator.translate('command_not_found', ctx)}")
async def add_logging(self, ctx, channel:discord.TextChannel, *, types): cid = str(channel.id) channels = Configuration.get_var(ctx.guild.id, "LOG_CHANNELS") if cid not in channels: channels[cid] = { "CATEGORIES": [], "DISABLED_KEYS": [] } info = channels[cid]["CATEGORIES"] added = [] ignored = [] message = "" known, unknown = self.extract_types(types) for t in known: if t in info: ignored.append(t) else: info.append(t) added.append(t) if len(added) > 0: message += f"{Emoji.get_chat_emoji('YES')} {Translator.translate('logs_added', ctx)}{', '.join(added)}" if len(ignored) > 0: message += f"\n{Emoji.get_chat_emoji('WARNING')}{Translator.translate('logs_ignored', ctx)}{', '.join(ignored)}" if len(unknown) > 0: message += f"\n {Emoji.get_chat_emoji('NO')}{Translator.translate('logs_unknown', ctx)}{', '.join(unknown)}" embed = discord.Embed(color=6008770) embed.add_field(name=channel.id, value=self.get_channel_properties(ctx, channel.id, channels[cid]["CATEGORIES"])) await ctx.send(message, embed=embed) Configuration.save(ctx.guild.id) features = [] for a in added: feature = Utils.find_key(Features.requires_logging, a) if feature is not None and not Configuration.get_var(ctx.guild.id, feature): features.append(feature) if len(features) > 0: async def yes(): await ctx.invoke(self.enable_feature, ", ".join(features)) await Confirmation.confirm(ctx, MessageUtils.assemble(ctx.guild.id, 'WHAT', 'confirmation_enable_features', count=len(features)) + ', '.join(features), on_yes=yes)
async def blacklist_remove(self, ctx, *, entry: str): """Removes an entry from the list""" guild_id = ctx.guild.id blacklist = Configuration.get_var(guild_id, "BAD_NAMES") if entry in blacklist: blacklist.remove(entry) Configuration.save(guild_id) self.assemble_detector(ctx.guild) await ctx.send(f"``{entry}`` has been removed from the blacklist") else: # check if it's matched under something else matches = self.get_matches_pretty(guild_id, entry) if len(matches) > 0: out = '\n'.join(matches) await ctx.send( f"``{entry}`` is not on the blacklist by itself, but parts of it are:\n{out}" ) else: await ctx.send( f"``{entry}`` is not on the blacklist, nor does it contain anything that is on the list" )
async def on_guild_role_delete(self, role: discord.Role): roles = Configuration.get_var(role.guild.id, "SELF_ROLES") if role.id in roles: roles.remove(role.id) Configuration.save(role.guild.id)
def update_config_section(guild, section, new_values, user, replace=False): fields = VALIDATORS[section] errors = dict() guild_config = Configuration.get_var(guild.id, section) new_values = convert_back(new_values) modified_values = new_values.copy() preview = guild_config.copy() preview.update(**new_values) for k, v in new_values.items(): if not replace and k in guild_config and guild_config[k] == v: modified_values.pop(k) elif isinstance(fields, dict): if k not in fields: errors[k] = "Unknown key" else: validated = fields[k](guild, v, preview, user, new_values) if validated is not True: errors[k] = validated else: validated = fields(guild, k, v, preview, user, new_values) if validated is not True: errors[k] = validated if replace: for k in Configuration.TEMPLATE[section].keys(): if k not in new_values: errors[k] = "Missing field" if len(modified_values) == 0: errors[section] = "Nothing to save!" if len(errors) > 0: raise ValidationException(errors) user_parts = {"user": Utils.clean_user(user), "user_id": user.id} old = dict(**guild_config) if replace: Configuration.set_cat(guild.id, section, modified_values) else: guild_config.update(**modified_values) Configuration.save(guild.id) for k, v in modified_values.items(): o = old[k] if k in old else None new = modified_values[k] if section in SPECIAL_HANDLERS: s = SPECIAL_HANDLERS[section] if isinstance(s, dict) and k in s: s[k](guild, o, new, user_parts) else: s(k, guild, o, new, user_parts) else: GearbotLogging.log_key(guild.id, "config_change", option_name=Translator.translate( f"config_{section}_{k}".lower(), guild), old=o, new=new, **user_parts) if replace: for k in (set(old.keys()) - set(modified_values.keys())): o = old[k] if section in SPECIAL_HANDLERS: s = SPECIAL_HANDLERS[section] if isinstance(s, dict) and k in s: s[k](guild, o, None, user_parts) else: s(k, guild, o, None, user_parts) else: GearbotLogging.log_key(guild.id, "config_change", option_name=Translator.translate( f"config_{section}_{k}".lower(), guild), old=old, new=None, **user_parts) to_return = { k: [str(rid) if isinstance(rid, int) else rid for rid in v] if isinstance(v, list) else str(v) if isinstance(v, int) else v for k, v in Configuration.get_var(guild.id, section).items() } return dict(status="Updated", modified_values=to_return)