Ejemplo n.º 1
0
def try_regex(bot: Bot, pattern: str,
              message: discord.Message) -> Optional[bool]:
    string = message.system_content
    jump = message.jump_url
    try:
        if utils.safe_regex(string, pattern):
            return True
    except TimeoutError:
        bot.dispatch(
            "guild_log",
            (f"I tried to match `{pattern}` to "
             f"[a message]({jump}), but it took too long. "
             "Try improving the efficiency of your regex. If "
             "you need help, feel free to join the support server."),
            "error",
            message.guild,
        )
        return None
    return False
Ejemplo n.º 2
0
def setup(bot: Bot) -> None:
    bot.add_cog(Fun(bot))
Ejemplo n.º 3
0
def setup(bot: Bot) -> None:
    bot.add_cog(QAEvents(bot))
Ejemplo n.º 4
0
def setup(bot: Bot) -> None:
    bot.add_cog(StatsEvents(bot))
Ejemplo n.º 5
0
def setup(bot: Bot) -> None:
    bot.add_cog(CacheEvents(bot))
Ejemplo n.º 6
0
def setup(bot: Bot) -> None:
    bot.add_cog(Utility(bot))
Ejemplo n.º 7
0
def setup(bot: Bot) -> None:
    bot.add_cog(Settings(bot))
Ejemplo n.º 8
0
async def handle_starboard(bot: Bot, sql_starboard: dict, sql_message: dict,
                           sql_author: dict) -> None:
    guild = bot.get_guild(sql_starboard["guild_id"])

    sql_starboard_message = await bot.db.fetchrow(
        """SELECT * FROM starboard_messages
        WHERE orig_id=$1 AND starboard_id=$2""",
        sql_message["id"],
        sql_starboard["id"],
    )
    if not sql_message["frozen"] or sql_starboard_message is None:
        points = await calculate_points(bot, sql_message, sql_starboard)
    else:
        points = sql_starboard_message["points"]
    if sql_starboard_message is not None:
        await set_points(bot, points, sql_starboard_message["id"])
    message = await bot.cache.fetch_message(
        bot,
        int(sql_message["guild_id"]),
        int(sql_message["channel_id"]),
        int(sql_message["id"]),
    )

    add = False
    edit = sql_starboard["link_edits"]
    delete = False

    forced = False
    frozen = False

    if points >= sql_starboard["required"]:
        add = True
    elif points <= sql_starboard["required_remove"]:
        delete = True

    if (not sql_starboard["allow_bots"]) and sql_author["is_bot"]:
        delete = True
        add = False

    if sql_starboard["link_deletes"] and (message is None):
        delete = True
        add = False

    if sql_message["is_nsfw"] and not sql_starboard["allow_nsfw"]:
        add = False
        delete = True

    if message is not None:
        if sql_starboard["regex"] != "":
            if try_regex(bot, sql_starboard["regex"], message) is False:
                add = False
                delete = True
        if sql_starboard["exclude_regex"] != "":
            if try_regex(bot, sql_starboard["exclude_regex"], message) is True:
                add = False
                delete = True

    if sql_message["frozen"]:
        add = False
        delete = False
        frozen = True

    if sql_starboard["id"] in sql_message["forced"]:
        add = True
        delete = False
        forced = True

    if sql_starboard_message is not None:
        starboard_message = await bot.cache.fetch_message(
            bot,
            int(sql_message["guild_id"]),
            int(sql_starboard_message["starboard_id"]),
            int(sql_starboard_message["id"]),
        )
        if starboard_message is None:
            await bot.db.execute(
                """DELETE FROM starboard_messages
                WHERE id=$1""",
                sql_starboard_message["id"],
            )
        sql_starboard_message = None
    else:
        starboard_message = None

    if delete and starboard_message is not None:
        await bot.db.execute(
            """DELETE FROM starboard_messages
            WHERE id=$1""",
            starboard_message.id,
        )
        try:
            await starboard_message.delete()
        except discord.errors.NotFound:
            pass
    elif not delete:
        embed = None
        attachments = []
        if message is not None:
            embed, attachments = await embed_message(
                bot, message, color=sql_starboard["color"])

        guild = bot.get_guild(int(sql_message["guild_id"]))
        display_emoji = utils.pretty_emoji_string(
            sql_starboard["display_emoji"], guild)

        plain_text = (
            f"**{display_emoji} {points} | <#{sql_message['channel_id']}>**"
            f"{' 🔒' if forced else ''}{' ❄️' if frozen else ''}")

        if starboard_message is None and add and message:
            starboard = guild.get_channel(int(sql_starboard["id"]))
            try:
                m = await starboard.send(plain_text,
                                         embed=embed,
                                         files=attachments)
            except discord.Forbidden:
                bot.dispatch(
                    "guild_log",
                    ("I tried to send a starboard message to "
                     f"{starboard.mention}, but I'm missing the "
                     "proper permissions. Please make sure I have "
                     "the `Send Messages` permission."),
                    "error",
                    guild,
                )
                return
            await bot.db.create_starboard_message(m.id, message.id,
                                                  sql_starboard["id"])
            await set_points(bot, points, m.id)
            if sql_starboard["autoreact"] is True:
                for emoji in sql_starboard["star_emojis"]:
                    try:
                        emoji_id = int(emoji)
                    except ValueError:
                        emoji_id = None
                    if emoji_id:
                        emoji = discord.utils.get(guild.emojis, id=emoji_id)
                    try:
                        await m.add_reaction(emoji)
                    except discord.Forbidden:
                        bot.dispatch(
                            "guild_log",
                            ("I tried to autoreact to a message on the "
                             "starboard, but I'm missing the proper "
                             "permissions. If you don't want me to "
                             "autoreact to messages, set the AutoReact "
                             "setting to False with `sb!s cs "
                             f"{starboard.mention} --autoReact False`"),
                            "error",
                            guild,
                        )
        elif starboard_message is not None and message:
            try:
                if edit:
                    await starboard_message.edit(content=plain_text,
                                                 embed=embed)
                else:
                    await starboard_message.edit(content=plain_text)
            except discord.errors.NotFound:
                pass
        elif starboard_message is not None:
            try:
                await starboard_message.edit(content=plain_text)
            except discord.errors.NotFound:
                pass
Ejemplo n.º 9
0
def setup(bot: Bot) -> None:
    bot.add_cog(StarboardEvents(bot))