Fixed bug in lists cog.

This commit is contained in:
Nichole Mattera 2020-05-05 19:43:23 -04:00
parent ce74bbe138
commit 6bc7a0c4aa

View file

@ -88,6 +88,42 @@ class Lists(Cog):
await message.edit(embed=None) await message.edit(embed=None)
async def cache_message(self, message):
msg = {
"has_attachment": False,
"attachment_filename": "",
"attachment_data": b"",
"content": message.content,
}
if len(message.attachments) != 0:
attachment = next(
(
a
for a in message.attachments
if a.filename.endswith(".png")
or a.filename.endswith(".jpg")
or a.filename.endswith(".jpeg")
),
None,
)
if attachment is not None:
msg["has_attachment"] = True
msg["attachment_filename"] = attachment.filename
msg["attachment_data"] = await attachment.read()
return msg
async def send_cached_message(self, channel, message):
if message["has_attachment"] == True:
file = discord.File(
io.BytesIO(message["attachment_data"]),
filename=message["attachment_filename"],
)
await channel.send(content=message["content"], file=file)
else:
await channel.send(content=message["content"])
# Commands # Commands
@commands.command(aliases=["list"]) @commands.command(aliases=["list"])
@ -285,14 +321,17 @@ class Lists(Cog):
) )
elif self.is_recycle(targeted_reaction): elif self.is_recycle(targeted_reaction):
messages = await channel.history( messages = [await self.cache_message(targeted_message)]
for message in await channel.history(
limit=None, after=targeted_message, oldest_first=True limit=None, after=targeted_message, oldest_first=True
).flatten() ).flatten():
messages.append(await self.cache_message(message))
await channel.purge(limit=len(messages) + 1, bulk=True) await channel.purge(limit=len(messages) + 1, bulk=True)
await channel.send(targeted_message.content)
for message in messages: for message in messages:
await channel.send(message.content) await self.send_cached_message(channel, message)
await log_channel.send( await log_channel.send(
self.create_log_message( self.create_log_message(
@ -301,30 +340,52 @@ class Lists(Cog):
) )
elif self.is_insert_above(targeted_reaction): elif self.is_insert_above(targeted_reaction):
messages = await channel.history( messages = [await self.cache_message(targeted_message)]
for message in await channel.history(
limit=None, after=targeted_message, oldest_first=True limit=None, after=targeted_message, oldest_first=True
).flatten() ).flatten():
messages.append(await self.cache_message(message))
await channel.purge(limit=len(messages) + 1, bulk=True) await channel.purge(limit=len(messages) + 1, bulk=True)
await channel.send(content) if attachment_filename is not None and attachment_data is not None:
await channel.send(targeted_message.content) file = discord.File(
io.BytesIO(attachment_data), filename=attachment_filename
)
await channel.send(content=content, file=file)
else:
await channel.send(content)
for message in messages: for message in messages:
await channel.send(message.content) await self.send_cached_message(channel, message)
await log_channel.send( await log_channel.send(
self.create_log_message("💬", "List item added:", user, channel) self.create_log_message("💬", "List item added:", user, channel)
) )
elif self.is_insert_below(targeted_reaction): elif self.is_insert_below(targeted_reaction):
messages = await channel.history( await targeted_reaction.remove(user)
limit=None, after=targeted_message, oldest_first=True
).flatten() messages = []
await channel.purge(limit=len(messages) + 1, bulk=True)
for message in await channel.history(
limit=None, after=targeted_message, oldest_first=True
).flatten():
messages.append(await self.cache_message(message))
await channel.purge(limit=len(messages), bulk=True)
if attachment_filename is not None and attachment_data is not None:
file = discord.File(
io.BytesIO(attachment_data), filename=attachment_filename
)
await channel.send(content=content, file=file)
else:
await channel.send(content)
await channel.send(targeted_message.content)
await channel.send(content)
for message in messages: for message in messages:
await channel.send(message.content) await self.send_cached_message(channel, message)
await log_channel.send( await log_channel.send(
self.create_log_message("💬", "List item added:", user, channel) self.create_log_message("💬", "List item added:", user, channel)