Merge pull request #68 from NicholeMattera/master

Fixed bug in lists cog.
This commit is contained in:
Ave 2020-05-06 03:43:20 +03:00 committed by GitHub
commit 09d96591ed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,6 +2,7 @@ import config
import discord import discord
import io import io
import urllib.parse import urllib.parse
import os.path
from discord.ext import commands from discord.ext import commands
from discord.ext.commands import Cog from discord.ext.commands import Cog
@ -88,6 +89,40 @@ 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 os.path.splitext(a.filename)[1] in [".png", ".jpg", ".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"])
@ -229,9 +264,7 @@ class Lists(Cog):
( (
a a
for a in message.attachments for a in message.attachments
if a.filename.endswith(".png") if os.path.splitext(a.filename)[1] in [".png", ".jpg", ".jpeg"]
or a.filename.endswith(".jpg")
or a.filename.endswith(".jpeg")
), ),
None, None,
) )
@ -285,14 +318,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 +337,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)