diff --git a/arsene_wenger/cogs/facts.py b/arsene_wenger/cogs/facts.py index 5bd029e..d4e617f 100644 --- a/arsene_wenger/cogs/facts.py +++ b/arsene_wenger/cogs/facts.py @@ -2,7 +2,9 @@ A cog to spit back random lines from files """ +import asyncio import random +from pathlib import Path import discord from discord import app_commands @@ -21,9 +23,12 @@ def __init__(self, bot): @commands.command(name="wengersucks") async def wengerSucks(self, ctx): - with open("wengerSucks.txt", "r", encoding="utf-8") as f: - content = f.read() + def _read(): + path = Path(__file__).parent.parent / "wengerSucks.txt" + with open(path, "r", encoding="utf-8") as f: + return f.read() + content = await asyncio.to_thread(_read) for message in content.split("\n\n"): return await ctx.send(message) @@ -33,7 +38,7 @@ async def wengerSucks(self, ctx): ) async def wengerFact(self, interaction: discord.Interaction): await interaction.response.send_message( - embed=await self.getManagerFact("wenger") + embed=self.getManagerFact("wenger") ) @app_commands.command( @@ -41,7 +46,7 @@ async def wengerFact(self, interaction: discord.Interaction): description="Get a random fact about our former manager Unai Emery.", ) async def unaiFact(self, interaction: discord.Interaction): - await interaction.response.send_message(embed=await self.getManagerFact("unai")) + await interaction.response.send_message(embed=self.getManagerFact("unai")) @app_commands.command( name="artetafact", @@ -49,16 +54,16 @@ async def unaiFact(self, interaction: discord.Interaction): ) async def artetaFact(self, interaction: discord.Interaction): await interaction.response.send_message( - embed=await self.getManagerFact("arteta") + embed=self.getManagerFact("arteta") ) - async def getManagerFact(self, manager): + def getManagerFact(self, manager): """ Get a random fact and return it as a nice embed :param manager: :return: """ - facts = f"facts/{manager}Facts.txt" + facts = Path(__file__).parent.parent / "facts" / f"{manager}Facts.txt" try: with open(facts, "r", encoding="utf-8") as f: content = f.readlines() @@ -73,6 +78,7 @@ async def getManagerFact(self, manager): return embed except FileNotFoundError: print(f"{getTimestamp()}\nError reading file {facts}") + return None async def setup(bot): @@ -82,4 +88,4 @@ async def setup(bot): This function is necessary for every cog file, multiple classes in the same file all need adding and each file must have their own setup function. """ - await bot.add_cog(FactsCog(bot)) + await bot.add_cog(FactsCog(bot)) \ No newline at end of file diff --git a/arsene_wenger/cogs/fixtures.py b/arsene_wenger/cogs/fixtures.py index e26e933..fb43111 100644 --- a/arsene_wenger/cogs/fixtures.py +++ b/arsene_wenger/cogs/fixtures.py @@ -29,6 +29,7 @@ class Fixture(BaseModel): competition: str scoreline: str = "" result: Literal["W", "L", "D"] | None = None + @computed_field @property def location_tag(self) -> str: diff --git a/arsene_wenger/cogs/link_fixer.py b/arsene_wenger/cogs/link_fixer.py index 8de5c23..76365d7 100644 --- a/arsene_wenger/cogs/link_fixer.py +++ b/arsene_wenger/cogs/link_fixer.py @@ -27,6 +27,18 @@ def find_urls(self, message, domain): pattern = re.compile(rf"https?://(?:www\.)?(?:{domain})/\S+") return re.findall(pattern, message) + def twitter_web_viewer_url(self, url): + """Extra url for twitter and x links that takes the original URL's number and converts it to + a twitter web viewer url. + example: if new_url is "https://www.fxtwitter.com/samimokbel_bbc/status/2092649709462564884?s=46" + the function should return "https://twitterwebviewer.com/?tweet=2092649709462564884" + """ + match = re.search(r"status/(\d+)", url) + if match: + tweet_id = match.group(1) + return f"https://twitterwebviewer.com/?tweet={tweet_id}" + return None + @commands.Cog.listener() async def on_message(self, message): if message.author.bot: @@ -44,9 +56,21 @@ async def on_message(self, message): url = original_urls[0] new_url = self.rewrite_media_url(url, domain) await message.edit(suppress=True) - await message.reply( - f"Fx'ed that for you! {new_url}", mention_author=False - ) + if "fxtwitter.com" in new_url: + twitter_web_viewer = self.twitter_web_viewer_url(new_url) + if twitter_web_viewer: + await message.reply( + f"Fx'ed that for you! {new_url}\nTwitter Web Viewer version: <{twitter_web_viewer}>", + mention_author=False, + ) + else: + await message.reply( + f"Fx'ed that for you! {new_url}", mention_author=False + ) + else: + await message.reply( + f"Fx'ed that for you! {new_url}", mention_author=False + ) async def setup(bot):