-
Notifications
You must be signed in to change notification settings - Fork 2
/
fixtwitter.py
81 lines (72 loc) · 3.04 KB
/
fixtwitter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import discord
from discord.ext import commands
import asyncio
from config import DELETE
class FixTwitter(commands.Cog):
replace: list[tuple[str, str]] = [
("https://twitter.com/", "https://vxtwitter.com/"),
("https://x.com/", "https://vxtwitter.com/"),
("https://www.tiktok.com/", "https://www.vxtiktok.com/"),
("https://www.instagram.com/", "https://www.ddinstagram.com/"),
]
def __init__(self, bot: commands.Bot):
self.bot = bot
self.ping_priv = discord.AllowedMentions(
everyone=False, users=False, roles=False, replied_user=False
)
self.user_post_to_fixed: dict[int, discord.Message] = (
{}
) # (id of original message, bot response message), use to delete bot response if original is deleted
self.fixed_to_user_post: dict[int, discord.Message] = (
{}
) # delete bot response if 'X' react happens
@commands.Cog.listener()
async def on_message(self, msg: discord.Message):
if msg.author.bot:
return
content = msg.clean_content
for match, substitute in self.replace:
content = content.replace(match, substitute)
if content == msg.clean_content:
return
try:
fixed = await msg.reply(content, mention_author=False)
self.user_post_to_fixed[msg.id] = fixed
self.fixed_to_user_post[fixed.id] = msg
await fixed.add_reaction(DELETE)
except discord.HTTPException:
return # If we cannot post a fixed link, return, since we do not want to suppress embeds on the post
try:
await msg.edit(suppress=True)
except discord.errors.Forbidden:
pass # For example, forbidden from removing embeds in a post that happened in DMs
await asyncio.sleep(
300
) # five minutes during which deletion of msg results in deletion of response
self.user_post_to_fixed.pop(msg.id, None)
await fixed.remove_reaction(DELETE, msg.guild.get_member(self.bot.user.id))
self.fixed_to_user_post.pop(fixed.id, None)
@commands.Cog.listener()
async def on_message_delete(self, msg: discord.Message):
if msg.id in self.user_post_to_fixed:
bot_response = self.user_post_to_fixed[msg.id]
await bot_response.delete()
self.user_post_to_fixed.pop(msg.id, None)
@commands.Cog.listener()
async def on_reaction_add(self, react: discord.Reaction, user: discord.User):
if user.bot:
return
if react.emoji != DELETE:
return
if react.message.id not in self.fixed_to_user_post:
return
user_post = self.fixed_to_user_post[react.message.id]
if user_post.author != user:
return
try:
await user_post.edit(suppress=False)
except discord.errors.Forbidden:
pass # eg. in DMs
finally:
self.fixed_to_user_post.pop(react.message.id, None)
await react.message.delete()