-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrivacyBot.py
59 lines (45 loc) · 2.13 KB
/
PrivacyBot.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
# Imports
from urllib.parse import urlparse
from discord.ext import commands
# Create bot
token = "TOKEN"
client = commands.Bot(command_prefix=";")
# External functions
def replace_links(messagecontent): # Replace links with privacy-friendly links
originalUrls = [] # Normal URLs
redirectedUrls = [] # Privacy-friendly URLs
words = messagecontent.split(" ")[0].split("\n") # Words in the message
redirectDictionary = { # "normal.link": "private.link"
"youtu.be": "redirect.invidious.io",
"youtube.com": "redirect.invidious.io",
"twitter.com": "nitter.net",
"instagram.com": "bibliogram.snopyta.org",
"reddit.com": "libredd.it"
}
for word in words: # For every word,
if "http" in word: # Indicates that it's a link
originalUrls.append(word) # Append to list of normal URLs
for url in originalUrls: # For every normal URL,
urlInfo = urlparse(url) # retrieve website name
website = urlInfo.netloc
if website in redirectDictionary.keys(): # If there's a private alternative,
redirectedUrl = url.replace(website, redirectDictionary[website]) # replace URL with private version
redirectedUrls.append(redirectedUrl) # Add private URl to list
replyString = "**Privacy friendly versions:**\n\n" # Send a message containing
for i in range(0, len(redirectedUrls)): # private URLs
replyString += redirectedUrls[i] + "\n"
return replyString, redirectedUrls
# Client events
@client.event
async def on_ready():
print("Privacy bot online.")
@client.event
async def on_message(message):
author = message.author
if author == client.user:
return
else:
replyContent, redirectedUrls = replace_links(message.content) # Reply + list of privacy-friendly URLs
if redirectedUrls: # If there are any privacy-friendly alternatives
await message.reply(replyContent, mention_author=False) # Reply with replyContent + don't mention them
client.run(token)