Beispiel #1
0
 async def on_message(self, message):
     member = self.bot.guild.get_member(message.author.id)
     if isinstance(message.author, discord.Member) and (
         message.guild != self.bot.guild
     ):
         return
     with DBManager.create_session_scope() as db_session:
         user = User._create_or_get_by_discord_id(
             db_session,
             message.author.id,
             user_name=str(member) if member else str(message.author),
         )
         Message._create(
             db_session,
             message.id,
             message.author.id,
             message.channel.id
             if isinstance(message.author, discord.Member)
             else None,
             message.content,
         )
         db_session.commit()
         HandlerManager.trigger(
             "discord_message",
             message_raw=message,
             message=message.content,
             author=message.author,
             user_level=user.level if user else 50,
             channel=message.channel
             if isinstance(message.author, discord.Member)
             else None,
             whisper=not isinstance(message.author, discord.Member),
         )
Beispiel #2
0
 def process_messages(self):
     with DBManager.create_session_scope() as db_session:
         regular_role = self.bot.get_role(self.settings["regular_role_id"])
         sub_role = self.bot.get_role(self.settings["sub_role_id"])
         for member in regular_role.members:
             count = Message._get_week_count_user(db_session,
                                                  str(member.id))
             if (count < self.settings["min_msgs_per_week"]
                     or sub_role not in member.roles):
                 self.bot.remove_role(member, regular_role)
         db_session.commit()
         messages = Message._get_last_hour(db_session)
         channels_to_listen_in = self.settings[
             "channels_to_listen_in"].split(" ") if len(
                 self.settings["channels_to_listen_in"]) != 0 else []
         for message in messages:
             if message.channel_id not in channels_to_listen_in and len(
                     channels_to_listen_in) != 0:
                 continue
             count = Message._get_day_count_user(db_session,
                                                 message.user_id)
             if message.user_id != str(self.bot.bot_id):
                 if count < self.settings["daily_max_msgs"] - 1:
                     message.user.points += self.settings["hourly_credit"]
                 elif count == self.settings["daily_max_msgs"] - 1:
                     message.user.points += self.settings["daily_limit"]
             message.credited = True
             db_session.commit()
         for user in User._get_users_with_points(
                 db_session, self.settings["min_regular_points"]):
             member = self.bot.get_member(user.discord_id)
             if (not member or sub_role not in member.roles
                     or regular_role in member.roles):
                 continue
             self.bot.add_role(member, regular_role)
Beispiel #3
0
    async def process_messages(self):
        with DBManager.create_session_scope() as db_session:
            regular_role = self.bot.filters.get_role(
                [self.settings["regular_role_id"]], None, {})[0]
            sub_role = self.bot.filters.get_role(
                [self.settings["sub_role_id"]], None, {})[0]
            counts_by_week = Message._get_week_count_by_user(db_session)
            for member in regular_role.members:
                count = counts_by_week.get(str(member.id), 0)
                if (count < self.settings["min_msgs_per_week"]
                        or sub_role not in member.roles):
                    await self.bot.remove_role(
                        member, regular_role,
                        "They failed to meet the requirements to keep the role"
                    )
            channels_to_listen_in = (
                self.settings["channels_to_listen_in"].split(" ")
                if len(self.settings["channels_to_listen_in"]) != 0 else None)
            messages = Message._get_last_hour(db_session,
                                              channels_to_listen_in)
            counts_by_day = Message._get_day_count_by_user(db_session)
            for message in messages:
                count = counts_by_day.get(message.user_id, 0)
                if message.user_id != str(self.bot.bot_id):
                    if count < self.settings["daily_max_msgs"] - 1:
                        message.user.points += self.settings["hourly_credit"]
                    elif count == self.settings["daily_max_msgs"] - 1:
                        message.user.points += self.settings["daily_limit"]
                message.credited = True
                counts_by_day[message.user_id] = count + 1

            for user in User._get_users_with_points(
                    db_session, self.settings["min_regular_points"]):
                member = self.bot.filters.get_member([user.discord_id], None,
                                                     {})[0]
                if not member:
                    continue

                count = counts_by_week.get(str(member.id), 0)
                if (sub_role not in member.roles
                        or (regular_role in member.roles
                            or count < self.settings["min_msgs_per_week"])):
                    continue
                await self.bot.add_role(
                    member, regular_role,
                    "They met the requirements to get the role")
Beispiel #4
0
 def new_message(self, db_session, message):
     return Message._create(
         db_session,
         message.id,
         message.author.id,
         message.channel.id
         if isinstance(message.author, discord.Member) else None,
         [message.content],
     )
Beispiel #5
0
    async def message_edit(self, payload):
        if not self.settings["log_edit_message"]:
            return
        out_channel = self.bot.filters.get_channel(
                [int(self.settings["output_channel"])], None, {}
            )[0]
        sent_in_channel = self.bot.filters.get_channel([int(payload.data["channel_id"])], None, {})[0]
        channels = (
            self.settings["ingore_channels"].split(" ")
            if self.settings["ingore_channels"] != ""
            else []
        )
        if not sent_in_channel or (len(channels) > 0 and str(sent_in_channel.id) in channels):
            return
        message_id = payload.message_id
        guild_id = payload.data.get("guild_id", None)

        message = await sent_in_channel.fetch_message(int(message_id))
        if not guild_id or self.bot.discord_bot.guild.id != int(guild_id):
            return

        with DBManager.create_session_scope() as db_session:
            db_message = Message._get(db_session, str(message_id))
            if not db_message:
                return
            content = json.loads(db_message.content)
            author_id = db_message.user_id
        author = self.bot.discord_bot.get_member(int(author_id))
        if int(author_id) == self.bot.discord_bot.client.user.id:
            return
        embed = discord.Embed(
            description=f"{author} updated their message in {sent_in_channel}",
            colour=await self.get_event_colour(author.guild, "message_edit"),
            timestamp=utils.now(),
        )

        embed.add_field(name="Now:", value=f"{content[-1]}" if content[-1] else "None", inline=False)
        embed.add_field(name="Previous:", value=f"{content[-2]}" if content[-2] else "None", inline=False)
        embed.add_field(
            name="Channel:",
            value=f"{sent_in_channel.mention} ({sent_in_channel})\n[Jump to message]({message.jump_url})",
            inline=False,
        )
        embed.add_field(
            name="ID",
            value=f"```User ID = {author.id}\nMessage ID = {message.id}\nChannel ID = {sent_in_channel.id}```",
            inline=False,
        )
        embed.set_author(
            name=f"{author} ({author.id})", icon_url=str(author.avatar_url),
        )
        await self.bot.say(out_channel, embed=embed)
Beispiel #6
0
 async def message_delete(self, payload):
     if not self.settings["log_delete_message"]:
         return
     out_channel = self.bot.filters.get_channel(
             [int(self.settings["output_channel"])], None, {}
         )[0]
     message_id = payload.message_id
     with DBManager.create_session_scope() as db_session:
         db_message = Message._get(db_session, message_id)
         if not db_message:
             return
         content = json.loads(db_message.content)
         author_id = db_message.user_id
     sent_in_channel = self.bot.filters.get_channel([int(payload.channel_id)], None, {})[0]
     channels = (
         self.settings["ingore_channels"].split(" ")
         if self.settings["ingore_channels"] != ""
         else []
     )
     if not sent_in_channel or (len(channels) > 0 and str(sent_in_channel.id) in channels):
         return
     author = self.bot.discord_bot.get_member(int(author_id))
     if author == self.bot.discord_bot.client.user:
         return
     embed = discord.Embed(
         colour=await self.get_event_colour(author.guild, "message_delete"),
         timestamp=utils.now(),
     )
     embed.add_field(name="Message", value=content[-1] if content[-1] else "None", inline=False)
     embed.add_field(name="Channel", value=sent_in_channel, inline=False)
     embed.add_field(
         name="ID",
         value=f"```Message ID: {message_id}\nUser ID: {author_id}\nChannel ID: {sent_in_channel.id}```",
         inline=False,
     )
     action = discord.AuditLogAction.message_delete
     perp = None
     async for _log in self.bot.discord_bot.guild.audit_logs(limit=2, action=action):
         same_chan = _log.extra.channel.id == sent_in_channel.id
         if _log.target.id == int(author_id) and same_chan:
             perp = f"{_log.user}({_log.user.id})"
             break
     if perp:
         embed.add_field(name="Deleted by", value=perp, inline=False)
     embed.set_footer(text="User ID: " + str(author_id))
     embed.set_author(
         name=f"{author} ({author.id})- Deleted Message",
         icon_url=str(author.avatar_url),
     )
     await self.bot.say(out_channel, embed=embed)
Beispiel #7
0
    async def querymessage(self, bot, author, channel, message, args):
        embed = discord.Embed(
            colour=await self.get_event_colour(author.guild, "message_edit"),
            timestamp=utils.now(),
        )
        embed.set_author(name=f"Message Query Result",)
        _args = message.split(" ") if message != "" else []
        if not _args:
            embed.description = f"Invalid Message ID"
            await self.bot.say(channel=channel, embed=embed)
            return
        message_id = _args[0]
        with DBManager.create_session_scope() as db_session:
            db_message = Message._get(db_session, str(message_id))
            if db_message:
                content = json.loads(db_message.content)
                author_id = db_message.user_id
                channel_id = db_message.channel_id

        if not db_message:
            embed.description = f"Message not found with message id {message_id}"
            await self.bot.say(channel=channel, embed=embed)
            return
        sent_in_channel = self.bot.filters.get_channel([int(channel_id)], None, {})[0]

        try:
            message = await sent_in_channel.fetch_message(int(message_id))
        except (discord.NotFound, discord.Forbidden, discord.HTTPException):
            message = None

        embed.add_field(name="Message History:", value="\n".join(content), inline=False)
        jump_url = (
            f"[Jump to message]({message.jump_url})"
            if message
            else "Message was deleted!"
        )
        embed.add_field(
            name="Channel:",
            value=f"{sent_in_channel.mention} ({sent_in_channel})\n{jump_url}",
            inline=False,
        )
        embed.add_field(
            name="ID",
            value=f"```User ID = {author_id}\nMessage ID = {message_id}\nChannel ID = {channel_id}```",
            inline=False,
        )
        await self.bot.say(channel=channel, embed=embed)
Beispiel #8
0
    async def edit_message(self, payload):
        with DBManager.create_session_scope() as db_session:
            message = Message._get(db_session, payload.message_id)
            if not message:
                return

            new_content = payload.data.get("content", "")
            message.edit_message(db_session, new_content)
            member = self.bot.discord_bot.get_member(message.user_id)
            user_level = self.bot.psudo_level_member(db_session,
                                                     member) if member else 0
            if user_level < 500:
                matched_phrase = self.bot.banphrase_manager.check_message(
                    new_content)
                if matched_phrase:
                    await self.bot.banphrase_manager.punish(
                        member, matched_phrase)
                    channel = await self.bot.discord_bot.get_channel(
                        message.channel_id)
                    message = await channel.fetch_message(
                        int(message.message_id))
                    await message.delete()