示例#1
0
    async def _delete_data(self, ctx: Context, allowed: bool, list_type: ValidFilterListType, content: str) -> None:
        """Remove an item from a filterlist."""
        allow_type = "whitelist" if allowed else "blacklist"

        # If this is a server invite, we need to convert it.
        if list_type == "GUILD_INVITE" and not IDConverter()._get_id_match(content):
            guild_data = await self._validate_guild_invite(ctx, content)
            content = guild_data.get("id")

        # If it's a file format, let's make sure it has a leading dot.
        elif list_type == "FILE_FORMAT" and not content.startswith("."):
            content = f".{content}"

        # Find the content and delete it.
        log.trace(f"Trying to delete the {content} item from the {list_type} {allow_type}")
        item = self.bot.filter_list_cache[f"{list_type}.{allowed}"].get(content)

        if item is not None:
            try:
                await self.bot.api_client.delete(
                    f"bot/filter-lists/{item['id']}"
                )
                del self.bot.filter_list_cache[f"{list_type}.{allowed}"][content]
                await ctx.message.add_reaction("✅")
            except ResponseCodeError as e:
                log.debug(
                    f"{ctx.author} tried to delete an item with the id {item['id']}, but "
                    f"the API raised an unexpected error: {e}"
                )
                await ctx.message.add_reaction("❌")
        else:
            await ctx.message.add_reaction("❌")
示例#2
0
文件: converters.py 项目: muunie/bot
    async def convert(self, ctx: Context, server_invite: str) -> dict:
        """Check whether the string is a valid Discord server invite."""
        invite_code = INVITE_RE.search(server_invite)
        if invite_code:
            response = await ctx.bot.http_session.get(
                f"{URLs.discord_invite_api}/{invite_code[1]}"
            )
            if response.status != 404:
                invite_data = await response.json()
                return invite_data.get("guild")

        id_converter = IDConverter()
        if id_converter._get_id_match(server_invite):
            raise BadArgument("Guild IDs are not supported, only invites.")

        raise BadArgument("This does not appear to be a valid Discord server invite.")
示例#3
0
def _is_an_unambiguous_user_argument(argument: str) -> bool:
    """Check if the provided argument is a user mention, user id, or username (name#discrim)."""
    has_id_or_mention = bool(IDConverter()._get_id_match(argument) or RE_USER_MENTION.match(argument))

    # Check to see if the author passed a username (a discriminator exists)
    argument = argument.removeprefix('@')
    has_username = len(argument) > 5 and argument[-5] == '#'

    return has_id_or_mention or has_username