Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[VxTwit] Add new cog #645

Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cogs/vxtwitconverter/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from redbot.core.bot import Red
from .vxtwitconverter import VxTwitConverter


async def setup(bot: Red):
"""Add the cog to the bot."""
await bot.add_cog(VxTwitConverter(bot))
22 changes: 22 additions & 0 deletions cogs/vxtwitconverter/core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import logging
import os

from redbot.core import data_manager
from redbot.core.bot import Red


class Core:
def __init__(self, bot: Red):
super().__init__()
self.bot = bot

# Initialize logger, and save to cog folder.
save_folder = data_manager.cog_data_path(cog_instance=self)
self.logger = logging.getLogger("red.luicogs.vxtwitconverter")
if not self.logger.handlers:
log_path = os.path.join(save_folder, "info.log")
handler = logging.FileHandler(filename=log_path, encoding="utf-8", mode="a")
handler.setFormatter(
logging.Formatter("%(asctime)s %(message)s", datefmt="[%d/%m/%Y %H:%M:%S]")
)
self.logger.addHandler(handler)
10 changes: 10 additions & 0 deletions cogs/vxtwitconverter/eventHandlers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from discord import Message

from cogs.vxtwitconverter.eventsCore import EventCore
xXJadeRabbitXx marked this conversation as resolved.
Show resolved Hide resolved
from redbot.core import commands


class EventHandlers(EventCore):
@commands.Cog.listener("on_message")
async def twit_replacer(self, message: Message):
await self._twit_replacer(message)
42 changes: 42 additions & 0 deletions cogs/vxtwitconverter/eventsCore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from discord import Message, channel
from urlextract import URLExtract

from cogs.vxtwitconverter.core import Core
xXJadeRabbitXx marked this conversation as resolved.
Show resolved Hide resolved


class EventCore(Core):
xXJadeRabbitXx marked this conversation as resolved.
Show resolved Hide resolved
async def _twit_replacer(self, message: Message):
# skips if the message is sent by any bot
if message.author.bot:
return

# skips if message is in dm
if isinstance(message.channel, channel.DMChannel):
return

# skips if the message has no embeds
if not any(embed.video for embed in message.embeds):
return

# the actual code part
vx_twit_links = [
result.replace("https://twitter.com", "https://vxtwitter.com")
for result in URLExtract().find_urls(message.content)
if "https://twitter.com" in result
]

# if we can't find any twitter links
if not vx_twit_links:
self.logger.debug("Embed found, but cannot find any valid links in the message")
return

# constructs the message and replies with a mention
ok = await message.reply(
"OwO what's this?\n"
"*notices your terrible twitter embeds*\n"
"Here's a better alternative:\n" + "\n".join(vx_twit_links),
)

# Remove embeds from user message if reply is successful
if ok:
await message.edit(suppress=True)
11 changes: 11 additions & 0 deletions cogs/vxtwitconverter/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name" : "VxTwitConverter",
"author" : ["JadeRabbit#9310"],
"short" : "twitter embed fix when?",
"description" : "Converts Twitter link to VxTwitter for better video embeds",
"install_msg" : "VxTwitConverter installed",
"requirements" : ["urlextract"],
"tags" : ["server"],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want tags twitter and vxtwitter here?

"permissions" : ["manage_messages"],
"end_user_data_statement": "no data is stored"
}
8 changes: 8 additions & 0 deletions cogs/vxtwitconverter/vxtwitconverter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from cogs.vxtwitconverter.eventHandlers import EventHandlers
xXJadeRabbitXx marked this conversation as resolved.
Show resolved Hide resolved
from redbot.core import commands


class VxTwitConverter(commands.Cog, EventHandlers):
"""Converts Twitter link to VxTwitter for better video embeds"""

pass