Skip to content

Commit fe2de32

Browse files
authored
[VxTwit] Add ddinstagram.com replacements (#656)
* [VxTwit] Add `ddinstagram.com` replacements This commit adds event listeners to replace instagram.com links with ddinstagram.com. It is using the same toggle as VxTwit. Since instagram.com embeds don't have videos or photos, we will need to replace all embeds. As such, the `valid` method was modified to not check for videos in embeds: this is deferred to a later check for the VxTwitter case. This commit doesn't change the cog name; this will be done in a follow up. * [VxTwit] Use Enum for SocialMedia * [VxTwit] Remove unnecessary debug statement * [VxTwit] Sort the imports * [VxTwit] Move INSTA_REGEX_MATCH to constants.py * [VxTwit] Refactor urls_to_string Co-authored-by: Dat Quach <quachtridat@yahoo.com.vn> * [VxTwit] Fix docstrings * [VxTwit] Compile regex pattern Co-authored-by: Dat Quach <quachtridat@yahoo.com.vn> * [VxTwit] Add on_message_edit listener for instagram
1 parent 5bacc23 commit fe2de32

File tree

4 files changed

+109
-16
lines changed

4 files changed

+109
-16
lines changed

cogs/vxtwitconverter/constants.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1-
KEY_ENABLED = "enabled"
1+
import enum
2+
import re
23

4+
KEY_ENABLED = "enabled"
35
DEFAULT_GUILD = {KEY_ENABLED: False}
6+
INSTA_REGEX_PATTERN = re.compile(r"https://(?:www\.)?(instagram.com)")
7+
8+
9+
class SocialMedia(enum.Enum):
10+
INSTAGRAM = "Instagram"
11+
TWITTER = "Twitter"

cogs/vxtwitconverter/eventHandlers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ class EventHandlers(EventsCore):
99
@commands.Cog.listener("on_message")
1010
async def twit_replacer(self, message: Message):
1111
await self._on_message_twit_replacer(message)
12+
await self._on_message_insta_replacer(message)
1213

1314
@commands.Cog.listener("on_message_edit")
1415
async def twit_edit_replacer(self, message_before: Message, message_after):
1516
await self._on_edit_twit_replacer(message_before, message_after)
17+
await self._on_edit_insta_replacer(message_before, message_after)

cogs/vxtwitconverter/eventsCore.py

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,65 @@
11
from discord import Message
22

3-
from .constants import KEY_ENABLED
3+
from .constants import KEY_ENABLED, SocialMedia
44
from .core import Core
5-
from .helpers import convert_to_vx_twitter_url, urls_to_string, valid
5+
from .helpers import convert_to_ddinsta_url, convert_to_vx_twitter_url, urls_to_string, valid
66

77

88
class EventsCore(Core):
9+
async def _on_message_insta_replacer(self, message: Message):
10+
if not valid(message):
11+
return
12+
13+
if not await self.config.guild(message.guild).get_attr(KEY_ENABLED)():
14+
self.logger.debug(
15+
"VxTwit disabled for guild %s (%s), skipping", message.guild.name, message.guild.id
16+
)
17+
return
18+
19+
ddinsta_urls = convert_to_ddinsta_url(message.embeds)
20+
21+
if not ddinsta_urls:
22+
return
23+
24+
# constructs the message and replies with a mention
25+
ok = await message.reply(urls_to_string(ddinsta_urls, SocialMedia.INSTAGRAM))
26+
27+
# Remove embeds from user message if reply is successful
28+
if ok:
29+
await message.edit(suppress=True)
30+
31+
async def _on_edit_insta_replacer(self, message_before: Message, message_after: Message):
32+
if not valid(message_after):
33+
return
34+
35+
if not await self.config.guild(message_after.guild).get_attr(KEY_ENABLED)():
36+
self.logger.debug(
37+
"VxTwit disabled for guild %s (%s), skipping",
38+
message_after.guild.name,
39+
message_after.guild.id,
40+
)
41+
return
42+
43+
new_embeds = [
44+
embed for embed in message_after.embeds if embed not in message_before.embeds
45+
]
46+
47+
# skips if the message has no new embeds
48+
if not new_embeds:
49+
return
50+
51+
ddinsta_urls = convert_to_ddinsta_url(new_embeds)
52+
53+
if not ddinsta_urls:
54+
return
55+
56+
# constructs the message and replies with a mention
57+
ok = await message_after.reply(urls_to_string(ddinsta_urls, SocialMedia.INSTAGRAM))
58+
59+
# Remove embeds from user message if reply is successful
60+
if ok:
61+
await message_after.edit(suppress=True)
62+
963
async def _on_message_twit_replacer(self, message: Message):
1064
if not valid(message):
1165
return
@@ -24,7 +78,7 @@ async def _on_message_twit_replacer(self, message: Message):
2478
return
2579

2680
# constructs the message and replies with a mention
27-
ok = await message.reply(urls_to_string(vx_twtter_urls))
81+
ok = await message.reply(urls_to_string(vx_twtter_urls, SocialMedia.TWITTER))
2882

2983
# Remove embeds from user message if reply is successful
3084
if ok:
@@ -60,7 +114,7 @@ async def _on_edit_twit_replacer(self, message_before: Message, message_after: M
60114
return
61115

62116
# constructs the message and replies with a mention
63-
ok = await message_after.reply(urls_to_string(vx_twtter_urls))
117+
ok = await message_after.reply(urls_to_string(vx_twtter_urls, SocialMedia.TWITTER))
64118

65119
# Remove embeds from user message if reply is successful
66120
if ok:

cogs/vxtwitconverter/helpers.py

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,38 @@
1+
import re
2+
13
from discord import Embed, Message, channel
24

5+
from .constants import INSTA_REGEX_PATTERN, SocialMedia
6+
7+
8+
def convert_to_ddinsta_url(embeds: list[Embed]):
9+
"""
10+
Parameters
11+
----------
12+
embeds: list of Discord embeds
13+
14+
Returns
15+
-------
16+
filtered list of Instagram URLs that have been converted to ddinstagram
17+
"""
18+
19+
# pulls only video embeds from list of embeds
20+
urls = [entry.url for entry in embeds]
21+
22+
ddinsta_urls = [
23+
re.sub(INSTA_REGEX_PATTERN, r"https://dd\1", result)
24+
for result in urls
25+
if re.match(INSTA_REGEX_PATTERN, result)
26+
]
27+
28+
return ddinsta_urls
29+
330

431
def convert_to_vx_twitter_url(embeds: list[Embed]):
532
"""
633
Parameters
734
----------
8-
embeds: list of discord embeds
35+
embeds: list of Discord embeds
936
1037
Returns
1138
-------
@@ -24,23 +51,25 @@ def convert_to_vx_twitter_url(embeds: list[Embed]):
2451
return vxtwitter_urls
2552

2653

27-
def urls_to_string(vx_twit_links: list[str]):
54+
def urls_to_string(links: list[str], socialMedia: SocialMedia):
2855
"""
2956
Parameters
3057
----------
31-
vx_twit_links: list of urls
58+
links: list[str]
59+
A list of urls
60+
socialMedia: SocialMedia
61+
The social media to replace.
3262
3363
Returns
3464
-------
3565
Formatted output
3666
"""
37-
38-
return "".join(
67+
return "\n".join(
3968
[
40-
"OwO what's this?\n",
41-
"*notices your terrible twitter embeds*\n",
42-
"Here's a better alternative:\n",
43-
"\n".join(vx_twit_links),
69+
"OwO what's this?",
70+
f"*notices your terrible {socialMedia.value} embeds*",
71+
"Here's a better alternative:",
72+
*links,
4473
]
4574
)
4675

@@ -53,7 +82,7 @@ def valid(message: Message):
5382
5483
Returns
5584
-------
56-
True if the message is from a human in a guild and contains video embeds
85+
True if the message is from a human in a guild and contains embeds
5786
False otherwise
5887
"""
5988

@@ -66,7 +95,7 @@ def valid(message: Message):
6695
return False
6796

6897
# skips if the message has no embeds
69-
if not any(embed.video for embed in message.embeds):
98+
if not message.embeds:
7099
return False
71100

72101
return True

0 commit comments

Comments
 (0)