Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions Main.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"cogs.quotes",
"cogs.reminder",
"cogs.roles",
"cogs.sanitizer",
"cogs.score",
"cogs.subscribers", # Do not remove this terminating comma.
]
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ You must set certain values in the `config.ini` file, in particular your Discor
* `HangmanNormalWin`: Value of normal hangman win.
* `HangmanCoolWin`: Value of cool hangman win.
* `HangmanTimeOut`: Time before a hangman game will time out if not interacted with.
* `[Sanitation]`:
Comment thread
namemcguffin marked this conversation as resolved.
Outdated
* `TikTokSanitationOptOutRole`: Role used to opt out of tiktok link sanitation.
* `TikTokSilentRole`: Role used to no longer receive DMs explaining tiktok link sanitation.
* `[AssignableRoles]`:
* `Pronouns`: Comma separated list of pronoun roles in server.
* `Fields`: Comma separated list of field of study roles in server.
Expand Down
83 changes: 83 additions & 0 deletions cogs/sanitizer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Copyright (C) idoneam (2016-2021)
#
# This file is part of Canary
#
# Canary is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Canary is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Canary. If not, see <https://www.gnu.org/licenses/>.
import discord
Comment thread
namemcguffin marked this conversation as resolved.
from discord import utils
from discord.ext import commands
import re
import requests

TIKTOK_SHORTLINK = re.compile(r"https?:\/\/vm\.tiktok\.com\/[A-Za-z0-9]*")
TIKTOK_MOBILE = re.compile(
r"(https?:\/\/m\.tiktok\.com\/v\/[0-9]*)\.html\?[A-Za-z0-9_&=%\.\?-]*")
TIKTOK_DESKTOP = re.compile(
r"(https?:\/\/www\.tiktok\.com\/@[A-Za-z0-9_\.]*\/video\/[0-9]*)\?[A-Za-z0-9_&=%\.\?-]*"
)


def unroll_tiktok(link):
return requests.head(link,
headers={
"User-Agent": "Mozilla/5.0 (X11)"
},
allow_redirects=True).url


class Sanitizer(commands.Cog):
def __init__(self, bot):
self.bot = bot

@commands.Cog.listener("on_message")
async def tiktok_link_sanitizer(self, msg):
if (not isinstance(msg.author, discord.Member)) or utils.get(
msg.author.roles,
name=self.bot.config.sanitation["tt_optout"]):
return
replace: bool = False
msg_txt: str = str(msg.content)
for short in TIKTOK_SHORTLINK.finditer(msg_txt):
short_match = short.group()
msg_txt = msg_txt.replace(short_match, unroll_tiktok(short_match))
replace = True
for mobile in TIKTOK_MOBILE.finditer(msg_txt):
full, clean = mobile.group(0, 1)
msg_txt = msg_txt.replace(full, unroll_tiktok(clean))
replace = True
for desktop in TIKTOK_DESKTOP.finditer(msg_txt):
full, clean = desktop.group(0, 1)
msg_txt = msg_txt.replace(full, clean)
replace = True
if replace:
await msg.delete()
await msg.channel.send(f"from: `{msg.author}`\n>>> {msg_txt}")
if not utils.get(msg.author.roles,
name=self.bot.config.sanitation["tt_silent"]):
dm_channel = msg.author.dm_channel or await msg.author.create_dm(
)
await dm_channel.send(
f"WARNING: a message you sent contained a tiktok link"
" that could potentially contain sensitive information."
"\nas such, it has been deleted and a sanitized version of"
" the message was resent.\nto opt out of this feature, feel"
f" free to request the `{self.bot.config.sanitation['tt_optout']}`"
"role.\nto still have this feature enabled but to no longer receive"
f" these message, feel free to request the `{self.bot.config.sanitation['tt_silent']}`"
f" role.\nhere is the message in question:\n>>> {msg.content}"
)


def setup(bot):
bot.add_cog(Sanitizer(bot))
4 changes: 4 additions & 0 deletions config/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ HangmanNormalWin = 10
HangmanCoolWin = 20
HangmanTimeOut = 600

[Sanitation]
TikTokSanitationOptOutRole = unsafetiktoker
TikTokSilentRole = safetiktoker

[AssignableRoles]
Pronouns = She/Her, He/Him, They/Them
Fields = Accounting, Agriculture, Anatomy and Cell Biology, Anthropology, Architecture, Biochemistry, Bioengineering, Biology, Bioresource Engineering, Chemical Engineering, Chemistry, Civil Engineering, Classics, cogito, Commerce, Computer Engineering, Computer Science, Computer Science/Biology, Cultural Studies, Desautels, Economics, Electrical Engineering, English, Experimental Medicine, Finance, Geography, History, Human Genetics, Indigenous Studies, International Development Studies, Jewish Studies, linguini, mac kid, Materials Engineering, Math, MBA, Mechanical Engineering, Medicine, Microbiology and Immunology, Neuroscience, Nursing, Pharmacology, Philosophy, Physical Therapy, Physics, Physiology, Political Science, Psychiatry, Psychology, Public Health, Social Work, Sociology, Software Engineering, Statistics, Theology, Urban Systems
Expand Down
5 changes: 5 additions & 0 deletions config/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ def __init__(self):
"hm_timeout": int(config["Games"]["HangmanTimeOut"])
}

self.sanitation = {
"tt_optout": config["Sanitation"]["TikTokSanitationOptOutRole"],
"tt_silent": config["Sanitation"]["TikTokSilentRole"]
}

# Assignable Roles
roles = {
"pronouns": config["AssignableRoles"]["Pronouns"],
Expand Down
47 changes: 45 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ urllib3 = ">=1.25.3"
feedparser = "^6.0.2"
regex = "^2021.4.4"
googletrans = "==4.0.0rc1"
Pillow = "^8.2.0"
Comment thread
namemcguffin marked this conversation as resolved.
Outdated

[tool.poetry.dev-dependencies]
yapf = "==0.31.0"
Expand Down