Skip to content

Commit

Permalink
why must we burden ourselves with descriptive commit messages
Browse files Browse the repository at this point in the history
closes #6
  • Loading branch information
techguydave committed Aug 18, 2024
1 parent 465fbe4 commit 05e6c04
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
52 changes: 39 additions & 13 deletions extensions/fix_link.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,46 @@
"""
Fix message links
"""
from typing import Union
from interactions import (
BrandColors,
Button,
ButtonStyle,
check,
ContextMenuContext,
Embed,
Extension,
check,
listen,
Member,
Message,
message_context_menu,
slash_command
slash_command,
User
)

from interactions.api.events import MessageCreate, MessageUpdate
from src import logutil, linkparser

logger = logutil.init_logger(__name__)

class FixLink(Extension):
async def is_a_bot(self, author: Union[Member, User]) -> bool:
return author.bot

async def not_a_bot(ctx: ContextMenuContext) -> bool:
result = not ctx.target.author.bot
return result

close_button = Button(
label="Close",
custom_id="close_msg",
style=ButtonStyle.GREY
)

async def get_new_links(self, ctx:ContextMenuContext):
async def get_new_links(self, embeds: list[Embed]):
url_list = []
message: Message = ctx.target

# Iterate through embeds in the original message and try to fix the links
for embed in message.embeds:
for embed in embeds:
new_url = linkparser.LinkParser(embed.url).fix()
if new_url is not None:
url_list.append(new_url)
Expand All @@ -49,26 +59,39 @@ async def no_link_found(self, ctx:ContextMenuContext):
]
)

async def not_a_bot(ctx: ContextMenuContext):
not_a_bot = not ctx.target.author.bot
return not_a_bot
@listen(MessageCreate)
async def on_message_create(self, event: MessageCreate):
a_bot = await self.is_a_bot(event.message.author)
if (not a_bot):
message: Message = event.message
# If there are embeds in the original message try to fix the links
if len(message.embeds) > 0:
new_links = await self.get_new_links(message.embeds)
new_links_list = "\n".join([i for i in new_links])
logger.debug(new_links_list)

# If any links were fixed, return them in a new message
if len(new_links_list) > 0:
await message.reply(
content=new_links_list,
silent=True
)
return

@check(check=not_a_bot)
@message_context_menu(name="View Fixed Links")
async def fix_links(self, ctx: ContextMenuContext):
message: Message = ctx.target
# If there are embeds in the original message try to fix the links
if len(message.embeds) > 0:
new_links = await self.get_new_links(ctx)
new_links = await self.get_new_links(message.embeds)
new_links_list = "\n".join([i for i in new_links])

# If any links were fixed, return them in a new message
if len(new_links_list) > 0:
await ctx.send(
content=new_links_list,
components=[self.close_button],
silent=True,
ephemeral=True
)
return
# Else, fallback to "no valid link found" message
Expand All @@ -77,7 +100,10 @@ async def fix_links(self, ctx: ContextMenuContext):
@slash_command(name="help")
async def help(self, ctx: ContextMenuContext):
await ctx.send(
content='Right-click/long-press a message with a social media or news link,\
content='Right-click/long-press a message with a social media or news link, \
go to "Apps", then click "View Fixed Links".',
ephemeral=True,
components=[
self.close_button
]
)
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
activity=Activity(
name="with social media links", type=ActivityType.PLAYING
),
intents=Intents.GUILDS | Intents.GUILD_MESSAGES,
intents=Intents.GUILDS | Intents.GUILD_MESSAGES | Intents.MESSAGE_CONTENT,
debug_scope=MISSING,
send_command_tracebacks=False
)
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "1.0.0"
description = ""
authors = ["synth3tk <[email protected]>"]
readme = "README.md"
package-mode = false

[tool.poetry.scripts]
main = "main:main"
Expand Down

0 comments on commit 05e6c04

Please sign in to comment.