lunarflu HF staff commited on
Commit
2aa3643
1 Parent(s): b17c838

serial experiments cache

Browse files
Files changed (1) hide show
  1. app.py +24 -23
app.py CHANGED
@@ -30,6 +30,7 @@ logger = logging.getLogger(__name__)
30
  logging.basicConfig(level=logging.DEBUG)
31
 
32
  #rate_limiter = RateLimiter(max_calls=10, period=60) # needs testing
 
33
 
34
  # stats stuff ---------------------------------------------------------------------------------------------------------------------------------------------------------
35
  number_of_messages = 0
@@ -196,25 +197,27 @@ async def on_message_edit(before, after):
196
  @bot.event
197
  async def on_message_delete(message):
198
  try:
199
- if message.author == bot.user:
200
- return
201
-
202
- embed = Embed(color=Color.red())
203
- embed.set_author(name=f"{message.author} ID: {message.author.id}", icon_url=message.author.avatar.url if message.author.avatar else bot.user.avatar.url)
204
- embed.title = "Message Deleted"
205
- embed.description = message.content or "*(empty message)*"
206
- embed.add_field(name="Author Username", value=message.author.name, inline=True)
207
- embed.add_field(name="Channel", value=message.channel.mention, inline=True)
208
- #embed.add_field(name="Message Created On", value=message.created_at.strftime("%Y-%m-%d %H:%M:%S UTC"), inline=True)
209
- embed.add_field(name="Message Created On", value=convert_to_timezone(message.created_at, zurich_tz), inline=True)
210
- embed.add_field(name="Message ID", value=message.id, inline=True)
211
- embed.add_field(name="Message Jump URL", value=f"[Jump to message!](https://discord.com/channels/{message.guild.id}/{message.channel.id}/{message.id})", inline=True)
212
- if message.attachments:
213
- attachment_urls = "\n".join([attachment.url for attachment in message.attachments])
214
- embed.add_field(name="Attachments", value=attachment_urls, inline=False)
215
- #embed.set_footer(text=f"{datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S UTC')}")
216
- embed.set_footer(text=f"{convert_to_timezone(datetime.utcnow(), zurich_tz)}")
217
- await bot.log_channel.send(embed=embed)
 
 
218
 
219
  except Exception as e:
220
  print(f"on_message_delete Error: {e}")
@@ -445,13 +448,11 @@ async def on_ready():
445
  guild = bot.get_guild(879548962464493619)
446
  for channel in guild.text_channels: # helps with more accurate logging across restarts
447
  try:
448
- # Fetch and cache the most recent 100 messages from the channel
449
- async for message in channel.history(limit=100):
450
- pass
451
  print(f"Finished caching messages for channel: {channel.name}")
452
  except Exception as e:
453
  print(f"An error occurred while fetching messages from {channel.name}: {e}")
454
- await asyncio.sleep(1)
455
 
456
 
457
  def run_bot():
 
30
  logging.basicConfig(level=logging.DEBUG)
31
 
32
  #rate_limiter = RateLimiter(max_calls=10, period=60) # needs testing
33
+ message_cache = {}
34
 
35
  # stats stuff ---------------------------------------------------------------------------------------------------------------------------------------------------------
36
  number_of_messages = 0
 
197
  @bot.event
198
  async def on_message_delete(message):
199
  try:
200
+ cached_message = message_cache.pop(message.id, None)
201
+ if cached_message:
202
+ if message.author == bot.user:
203
+ return
204
+
205
+ embed = Embed(color=Color.red())
206
+ embed.set_author(name=f"{message.author} ID: {message.author.id}", icon_url=message.author.avatar.url if message.author.avatar else bot.user.avatar.url)
207
+ embed.title = "Message Deleted"
208
+ embed.description = message.content or "*(empty message)*"
209
+ embed.add_field(name="Author Username", value=message.author.name, inline=True)
210
+ embed.add_field(name="Channel", value=message.channel.mention, inline=True)
211
+ #embed.add_field(name="Message Created On", value=message.created_at.strftime("%Y-%m-%d %H:%M:%S UTC"), inline=True)
212
+ embed.add_field(name="Message Created On", value=convert_to_timezone(message.created_at, zurich_tz), inline=True)
213
+ embed.add_field(name="Message ID", value=message.id, inline=True)
214
+ embed.add_field(name="Message Jump URL", value=f"[Jump to message!](https://discord.com/channels/{message.guild.id}/{message.channel.id}/{message.id})", inline=True)
215
+ if message.attachments:
216
+ attachment_urls = "\n".join([attachment.url for attachment in message.attachments])
217
+ embed.add_field(name="Attachments", value=attachment_urls, inline=False)
218
+ #embed.set_footer(text=f"{datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S UTC')}")
219
+ embed.set_footer(text=f"{convert_to_timezone(datetime.utcnow(), zurich_tz)}")
220
+ await bot.log_channel.send(embed=embed)
221
 
222
  except Exception as e:
223
  print(f"on_message_delete Error: {e}")
 
448
  guild = bot.get_guild(879548962464493619)
449
  for channel in guild.text_channels: # helps with more accurate logging across restarts
450
  try:
451
+ message_cache.update({m.id: m async for m in channel.history(limit=100)})
 
 
452
  print(f"Finished caching messages for channel: {channel.name}")
453
  except Exception as e:
454
  print(f"An error occurred while fetching messages from {channel.name}: {e}")
455
+ await asyncio.sleep(0.1)
456
 
457
 
458
  def run_bot():