Skip to content

Commit

Permalink
Fix message cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
Twixes committed Nov 17, 2024
1 parent 9bb2394 commit 5a7fc69
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions plugins/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
# You should have received a copy of the GNU General Public License along with Somsiad.
# If not, see <https://www.gnu.org/licenses/>.

import asyncio
from dataclasses import dataclass
from functools import cached_property
import json
from typing import List, Mapping, Optional, Sequence
import discord
Expand Down Expand Up @@ -131,22 +131,28 @@ def _all_available_commands_as_tools(self) -> Sequence[FunctionDefinition]:
for full_command_name, command in self._all_available_commands.items()
]

def embeds_to_text(self, ctx: commands.Context, embeds: List[discord.Embed]) -> str:
async def embeds_to_text(self, ctx: commands.Context, embeds: List[discord.Embed]) -> str:
parts = []
for embed in embeds:
if embed.title:
parts.append(embed.title)
if embed.description:
parts.append(clean_content_converter.convert(ctx, embed.description))
parts.append(await clean_content_converter.convert(ctx, embed.description))
if embed.fields:
parts.append(
"\n".join(
f"{clean_content_converter.convert(ctx, field.name)}: {clean_content_converter.convert(ctx, field.value)}"
embed_fields_cleaned = await asyncio.gather(
*(
asyncio.gather(
clean_content_converter.convert(ctx, field.name),
clean_content_converter.convert(ctx, field.value),
loop=self.bot.loop,
)
for field in embed.fields
)
),
loop=self.bot.loop,
)
parts.append("\n".join(f"{name}: {value}" for name, value in embed_fields_cleaned))
if embed.footer.text:
parts.append(clean_content_converter.convert(ctx, embed.footer.text))
parts.append(await clean_content_converter.convert(ctx, embed.footer.text))
return "\n".join(parts)

async def message_to_text(self, ctx: commands.Context, message: discord.Message) -> Optional[str]:
Expand All @@ -163,7 +169,7 @@ async def message_to_text(self, ctx: commands.Context, message: discord.Message)
parts[0] = parts[0][len(prefix) :].lstrip()
break
if message.embeds:
parts.append(self.embeds_to_text(ctx, message.embeds))
parts.append(await self.embeds_to_text(ctx, message.embeds))
if message.attachments:
parts.extend(f"Załącznik {i+1}: {attachment.filename}" for i, attachment in enumerate(message.attachments))
return "\n".join(parts)
Expand Down

0 comments on commit 5a7fc69

Please sign in to comment.