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." )
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)
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")
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
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()
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)
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)
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
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