Exemplo n.º 1
0
def insert_message(message):

    try:
        message_type = message.type

        if message_type == MessageType.default:
            message_type = None
        else:
            message_type = message_type.value
        logged = LoggedMessage.create(messageid=message.id,
                                      content=message.content,
                                      author=message.author.id,
                                      channel=message.channel.id,
                                      server=message.guild.id,
                                      type=message_type,
                                      pinned=message.pinned)
        for a in message.attachments:
            LoggedAttachment.create(id=a.id,
                                    url=a.proxy_url,
                                    isImage=(a.width is not None
                                             or a.width is 0),
                                    messageid=message.id)
    except IntegrityError:
        pass
    return logged
Exemplo n.º 2
0
 async def buildCache(self, guild: discord.Guild, limit=250):
     start = time.perf_counter()
     GearbotLogging.info(
         f"Populating modlog with missed messages during downtime for {guild.name} ({guild.id})."
     )
     newCount = 0
     editCount = 0
     count = 0
     for channel in guild.text_channels:
         if channel.permissions_for(guild.get_member(
                 self.bot.user.id)).read_messages:
             logged_messages = LoggedMessage.select().where(
                 LoggedMessage.channel == channel.id).order_by(
                     LoggedMessage.messageid.desc()).limit(limit * 1.5)
             messages = dict()
             for message in logged_messages:
                 messages[message.messageid] = message
             async for message in channel.history(limit=limit,
                                                  reverse=False):
                 if not self.running:
                     GearbotLogging.info(
                         "Cog unloaded while still building cache, aborting."
                     )
                     return
                 if message.author == self.bot.user:
                     continue
                 if message.id not in messages.keys():
                     try:
                         LoggedMessage.create(
                             messageid=message.id,
                             author=message.author.id,
                             content=message.content,
                             timestamp=message.created_at.timestamp(),
                             channel=channel.id,
                             server=channel.guild.id)
                         for a in message.attachments:
                             LoggedAttachment.create(
                                 id=a.id,
                                 url=a.url,
                                 isImage=(a.width is not None
                                          or a.width is 0),
                                 messageid=message.id)
                         newCount = newCount + 1
                     except IntegrityError:
                         # somehow we didn't fetch enough messages, did someone set off a nuke in the channel?
                         logged = LoggedMessage.get(messageid=message.id)
                         if logged.content != message.content:
                             logged.content = message.content
                             logged.save()
                             editCount = editCount + 1
                 else:
                     logged = messages[message.id]
                     if logged.content != message.content:
                         logged.content = message.content
                         logged.save()
                         editCount = editCount + 1
                     count = count + 1
     GearbotLogging.info(
         f"Discovered {newCount} new messages and {editCount} edited in {guild.name} (checked {count}) in {time.perf_counter() - start }s."
     )
Exemplo n.º 3
0
 async def quote(self, ctx:commands.Context, message_id:int):
     """quote_help"""
     embed = None
     async with ctx.typing():
         message = LoggedMessage.get_or_none(messageid=message_id)
         if message is None:
             for guild in self.bot.guilds:
                 for channel in guild.text_channels:
                     try:
                         dmessage: discord.Message = await channel.get_message(message_id)
                         for a in dmessage.attachments:
                             LoggedAttachment.get_or_create(id=a.id, url=a.url,
                                                            isImage=(a.width is not None or a.width is 0),
                                                            messageid=message.id)
                         message = LoggedMessage.create(messageid=message_id, content=dmessage.content, author=dmessage.author.id, timestamp = dmessage.created_at.timestamp(), channel=channel.id, server=dmessage.guild.id)
                     except Exception as ex:
                         #wrong channel
                         pass
                     if message is not None:
                         break
         if message is not None:
             channel = self.bot.get_channel(message.channel)
             attachment = None
             attachments = LoggedAttachment.select().where(LoggedAttachment.messageid == message_id)
             if len(attachments) == 1:
                 attachment = attachments[0]
             embed = discord.Embed(colour=discord.Color(0xd5fff), timestamp=datetime.utcfromtimestamp(message.timestamp))
             if message.content is None or message.content == "":
                 if attachment is not None:
                     if attachment.isImage:
                         embed.set_image(url=attachment.url)
                     else:
                         embed.add_field(name=Translator.translate("attachement_link", ctx), value=attachment.url)
             else:
                 description = message.content
                 embed = discord.Embed(colour=discord.Color(0xd5fff), description=description, timestamp=datetime.utcfromtimestamp(message.timestamp))
                 embed.add_field(name="​", value=f"https://discordapp.com/channels/{channel.guild.id}/{channel.id}/{message_id}")
                 if attachment is not None:
                     if attachment.isImage:
                         embed.set_image(url=attachment.url)
                     else:
                         embed.add_field(name=Translator.translate("attachement_link", ctx), value=attachment.url)
             try:
                 user = await commands.MemberConverter().convert(ctx, message.author)
             except:
                 user = await ctx.bot.get_user_info(message.author)
             embed.set_author(name=user.name, icon_url=user.avatar_url)
             embed.set_footer(text=Translator.translate("quote_footer", ctx, channel=self.bot.get_channel(message.channel).name, user=Utils.clean(ctx.author.display_name), message_id=message_id))
     if embed is None:
         await ctx.send(Translator.translate("quote_not_found", ctx))
     else:
         if channel.is_nsfw() and not ctx.channel.is_nsfw():
             await ctx.send(f"{Emoji.get_chat_emoji('NO')} {Translator.translate('quote_nsfw_refused', ctx)}")
             return
         await ctx.send(embed=embed)
         if ctx.channel.permissions_for(ctx.me).manage_messages:
             await ctx.message.delete()
Exemplo n.º 4
0
 async def on_message(self, message: discord.Message):
     if not hasattr(message.channel, "guild") or message.channel.guild is None:
         return
     if Configuration.get_var(message.guild.id, "EDIT_LOGS"):
         LoggedMessage.create(messageid=message.id, author=message.author.id, content=message.content,
                              channel=message.channel.id, server=message.guild.id)
         for a in message.attachments:
             LoggedAttachment.create(id=a.id, url=a.url, isImage=(a.width is not None or a.width is 0),
                                     messageid=message.id)
Exemplo n.º 5
0
    async def buildCache(self, guild: discord.Guild, limit=None, startup=False):
        if limit is None:
            limit = 500 if startup else 50
        GearbotLogging.info(f"Populating modlog with missed messages during downtime for {guild.name} ({guild.id}).")
        newCount = 0
        editCount = 0
        count = 0
        no_access = 0
        fetch_times = []
        processing_times = []
        for channel in guild.text_channels:
            permissions = channel.permissions_for(guild.get_member(self.bot.user.id))
            if permissions.read_messages and permissions.read_message_history:

                async for message in channel.history(limit=limit, reverse=False,
                                                     before=self.cache_message if startup else None):
                    processing = time.perf_counter()
                    if not self.running:
                        GearbotLogging.info("Cog unloaded while still building cache, aborting.")
                        return
                    fetch = time.perf_counter()
                    logged = LoggedMessage.get_or_none(messageid=message.id)
                    fetch_times.append(time.perf_counter() - fetch)
                    if logged is None:
                        LoggedMessage.create(messageid=message.id, author=message.author.id,
                                             content=message.content,
                                             channel=channel.id, server=channel.guild.id)
                        for a in message.attachments:
                            LoggedAttachment.create(id=a.id, url=a.url,
                                                    isImage=(a.width is not None or a.width is 0),
                                                    messageid=message.id)
                        newCount = newCount + 1
                    elif message.edited_at is not None:
                        if logged.content != message.content:
                            logged.content = message.content
                            logged.save()
                            editCount = editCount + 1
                    count = count + 1
                    processing_times.append(time.perf_counter() - processing)
                    if count % min(75, int(limit / 2)) is 0:
                        await asyncio.sleep(0)

                await asyncio.sleep(0)
            else:
                no_access += 1
        GearbotLogging.info(
            f"Discovered {newCount} new messages and {editCount} edited in {guild.name} (checked {count})")
        total_fetch_time = sum(fetch_times)
        avg_fetch_time = (total_fetch_time / len(fetch_times)) * 1000
        total_processing = (sum(processing_times)) * 1000
        avg_processing = total_processing / len(processing_times)
        GearbotLogging.info(f"Average fetch time: {avg_fetch_time} (total fetch time: {total_fetch_time})")
        GearbotLogging.info(f"Average processing time: {avg_processing} (total of {total_processing})")
        GearbotLogging.info(f"Was unable to read messages from {no_access} channels")
Exemplo n.º 6
0
def insert_message(message):
    logged = LoggedMessage.create(messageid=message.id,
                                  content=message.content,
                                  author=message.author.id,
                                  channel=message.channel.id,
                                  server=message.guild.id)
    for a in message.attachments:
        LoggedAttachment.get_or_create(id=a.id,
                                       url=a.url,
                                       isImage=(a.width is not None
                                                or a.width is 0),
                                       messageid=message.id)
    return logged
Exemplo n.º 7
0
def insert_message(message):

    try:
        logged = LoggedMessage.create(messageid=message.id, content=message.content,
                                   author=message.author.id,
                                   channel=message.channel.id, server=message.guild.id)
        for a in message.attachments:
            LoggedAttachment.create(id=a.id, url=a.url,
                                       isImage=(a.width is not None or a.width is 0),
                                       messageid=message.id)
    except IntegrityError as ex:
        pass
    return logged
Exemplo n.º 8
0
async def insert_message(message):
    if message.id not in recent_list and message.id not in previous_list:
        message_type = message.type
        if message_type == MessageType.default:
            message_type = None
        else:
            if not isinstance(message_type, int):
                message_type = message_type.value
        m = fakeLoggedMessage(messageid=message.id,
                              content=message.content,
                              author=message.author.id,
                              channel=message.channel.id,
                              server=message.guild.id,
                              type=message_type,
                              pinned=message.pinned,
                              attachments=[
                                  LoggedAttachment(id=a.id,
                                                   name=a.filename,
                                                   isImage=(a.width is not None
                                                            or a.width is 0),
                                                   message_id=message.id)
                                  for a in message.attachments
                              ])
        batch[message.id] = m

        recent_list.add(message.id)
        if len(batch) >= 1000:
            asyncio.create_task(flush(force=True))
    return message
Exemplo n.º 9
0
    async def quote(self, ctx: commands.Context, *, message: Message):
        """quote_help"""
        await ctx.trigger_typing()
        member = message.guild.get_member(ctx.author.id)
        if member is None:
            await GearbotLogging.send_to(ctx, 'NO',
                                         'quote_not_visible_to_user')
        else:
            permissions = message.channel.permissions_for(member)
            if permissions.read_message_history and permissions.read_message_history:
                if message.channel.is_nsfw() and not ctx.channel.is_nsfw():
                    await GearbotLogging.send_to(ctx, 'NO',
                                                 'quote_nsfw_refused')
                else:
                    attachment = None
                    attachments = LoggedAttachment.select().where(
                        LoggedAttachment.messageid == message.id)
                    if len(attachments) == 1:
                        attachment = attachments[0]
                    embed = discord.Embed(colour=discord.Color(0xd5fff),
                                          timestamp=message.created_at)
                    if message.content is None or message.content == "":
                        if attachment is not None:
                            if attachment.isImage:
                                embed.set_image(url=attachment.url)
                            else:
                                embed.add_field(name=Translator.translate(
                                    "attachment_link", ctx),
                                                value=attachment.url)
                    else:
                        description = message.content
                        embed = discord.Embed(colour=discord.Color(0xd5fff),
                                              description=description,
                                              timestamp=message.created_at)
                        embed.add_field(
                            name="​",
                            value=f"[Jump to message]({message.jump_url})")
                        if attachment is not None:
                            if attachment.isImage:
                                embed.set_image(url=attachment.url)
                            else:
                                embed.add_field(name=Translator.translate(
                                    "attachment_link", ctx),
                                                value=attachment.url)
                    user = message.author
                    embed.set_author(name=user.name, icon_url=user.avatar_url)
                    embed.set_footer(text=Translator.translate(
                        "quote_footer",
                        ctx,
                        channel=message.channel.name,
                        user=Utils.clean_user(ctx.author),
                        message_id=message.id))
                    await ctx.send(embed=embed)
                    if ctx.channel.permissions_for(ctx.me).manage_messages:
                        await ctx.message.delete()

            else:
                await GearbotLogging.send_to(ctx, 'NO',
                                             'quote_not_visible_to_user')
Exemplo n.º 10
0
    async def on_raw_message_delete(self, data: RawMessageDeleteEvent):
        if data.message_id in self.bot.data["message_deletes"]:
            self.bot.data["message_deletes"].remove(data.message_id)
            return
        if not Features.is_logged(data.guild_id, "EDIT_LOGS"):
            return
        message = await MessageUtils.get_message_data(self.bot,
                                                      data.message_id)
        if message is not None:
            if message.channel in self.bot.being_cleaned:
                self.bot.being_cleaned[message.channel].add(data.message_id)
                return
            guild = self.bot.get_guild(message.server)
            user: discord.User = await Utils.get_user(message.author)
            hasUser = user is not None
            if not hasUser or user.id in Configuration.get_var(
                    guild.id, "IGNORED_USERS") or user.id == guild.me.id:
                return
            attachments = LoggedAttachment.select().where(
                LoggedAttachment.messageid == data.message_id)
            channel = self.bot.get_channel(message.channel)
            name = Utils.clean_user(user) if hasUser else str(message.author)
            GearbotLogging.log_to(
                guild.id, "EDIT_LOGS",
                f"{Emoji.get_chat_emoji('TRASH')} {Translator.translate('message_removed', guild.id, name=name, user_id=user.id if hasUser else 'WEBHOOK', channel=channel.mention)}"
            )
            if Configuration.get_var(channel.guild.id, "EMBED_EDIT_LOGS"):

                embed = discord.Embed(
                    timestamp=datetime.datetime.utcfromtimestamp(time.time()),
                    description=message.content)
                embed.set_author(
                    name=user.name if hasUser else message.author,
                    icon_url=user.avatar_url if hasUser else EmptyEmbed)

                embed.set_footer(text=f"Sent in #{channel.name}")
                if len(attachments) > 0:
                    embed.add_field(
                        name=Translator.translate('attachment_link', guild),
                        value="\n".join(attachment.url
                                        for attachment in attachments))
                GearbotLogging.log_to(guild.id, "EDIT_LOGS", embed=embed)
            else:
                cleaned_content = await Utils.clean(message.content,
                                                    channel.guild)
                GearbotLogging.log_to(guild.id,
                                      "EDIT_LOGS",
                                      f"**Content:** {cleaned_content}",
                                      can_stamp=False)
                count = 1
                for attachment in attachments:
                    GearbotLogging.log_to(
                        guild.id,
                        "EDIT_LOGS",
                        f"**Attachment{f' {count}' if len(attachments) > 1 else ''}:** <{attachment.url}>",
                        can_stamp=False)
                    count += 1
Exemplo n.º 11
0
 async def on_message(self, message: discord.Message):
     if not hasattr(message.channel,
                    "guild") or message.channel.guild is None:
         return
     if Configuration.getConfigVar(
             message.guild.id,
             "MINOR_LOGS") is 0 or message.author == self.bot.user:
         return
     for a in message.attachments:
         LoggedAttachment.create(id=a.id,
                                 url=a.url,
                                 isImage=(a.width is not None
                                          or a.width is 0),
                                 messageid=message.id)
     LoggedMessage.create(messageid=message.id,
                          author=message.author.id,
                          content=message.content,
                          timestamp=message.created_at.timestamp(),
                          channel=message.channel.id,
                          server=message.guild.id)
Exemplo n.º 12
0
async def insert_message(bot, message):
    if is_cache_enabled(bot):
        pipe = bot.redis_pool.pipeline()
        pipe.hmset_dict(message.id,
                        author=message.author.id,
                        content=message.content,
                        channel=message.channel.id,
                        server=message.guild.id)
        pipe.expire(message.id, 5 * 60 + 2)
        await pipe.execute()
    LoggedMessage.create(messageid=message.id,
                         author=message.author.id,
                         content=message.content,
                         channel=message.channel.id,
                         server=message.guild.id)
    for a in message.attachments:
        LoggedAttachment.create(id=a.id,
                                url=a.url,
                                isImage=(a.width is not None or a.width is 0),
                                messageid=message.id)