Add the ability to disallow words (banned words)

This commit is contained in:
Jeremy Zhang
2018-07-09 01:37:35 +00:00
parent f0f1f226e3
commit ccf9aed9ec
11 changed files with 601 additions and 0 deletions

View File

@ -183,6 +183,9 @@ def administrate_guild(guild_id):
"guest_icon": db_guild.guest_icon if db_guild.guest_icon != None else "",
"post_timeout": db_guild.post_timeout,
"max_message_length": db_guild.max_message_length,
"banned_words_enabled": db_guild.banned_words_enabled,
"banned_words_global_included": db_guild.banned_words_global_included,
"banned_words": json.loads(db_guild.banned_words),
}
return render_template("administrate_guild.html.j2", guild=dbguild_dict, members=users, permissions=permissions, cosmetics=cosmetics)
@ -199,6 +202,8 @@ def update_administrate_guild(guild_id):
db_guild.unauth_captcha = request.form.get("unauth_captcha", db_guild.unauth_captcha) in ["true", True]
db_guild.post_timeout = request.form.get("post_timeout", db_guild.post_timeout)
db_guild.max_message_length = request.form.get("max_message_length", db_guild.max_message_length)
db_guild.banned_words_enabled = request.form.get("banned_words_enabled", db_guild.banned_words_enabled) in ["true", True]
db_guild.banned_words_global_included = request.form.get("banned_words_global_included", db_guild.banned_words_global_included) in ["true", True]
invite_link = request.form.get("invite_link", db_guild.invite_link)
if invite_link != None and invite_link.strip() == "":
invite_link = None
@ -207,6 +212,15 @@ def update_administrate_guild(guild_id):
if guest_icon != None and guest_icon.strip() == "":
guest_icon = None
db_guild.guest_icon = guest_icon
banned_word = request.form.get("banned_word", None)
if banned_word:
delete_banned_word = request.form.get("delete_banned_word", False) in ["true", True]
banned_words = set(json.loads(db_guild.banned_words))
if delete_banned_word:
banned_words.discard(banned_word)
else:
banned_words.add(banned_word)
db_guild.banned_words = json.dumps(list(banned_words))
db.session.commit()
emit("guest_icon_change", {"guest_icon": guest_icon if guest_icon else url_for('static', filename='img/titanembeds_square.png')}, room="GUILD_"+guild_id, namespace="/gateway")
return jsonify(
@ -222,6 +236,9 @@ def update_administrate_guild(guild_id):
unauth_captcha=db_guild.unauth_captcha,
post_timeout=db_guild.post_timeout,
max_message_length=db_guild.max_message_length,
banned_words_enabled=db_guild.banned_words_enabled,
banned_words_global_included=db_guild.banned_words_global_included,
banned_words=json.loads(db_guild.banned_words),
)
@admin.route("/guilds")

View File

@ -2,6 +2,7 @@ from titanembeds.database import db, Guilds, UnauthenticatedUsers, Unauthenticat
from titanembeds.decorators import valid_session_required, discord_users_only, abort_if_guild_disabled
from titanembeds.utils import check_guild_existance, guild_accepts_visitors, guild_query_unauth_users_bool, get_client_ipaddr, discord_api, rate_limiter, channel_ratelimit_key, guild_ratelimit_key, user_unauthenticated, checkUserRevoke, checkUserBanned, update_user_status, check_user_in_guild, get_guild_channels, guild_webhooks_enabled, guild_unauthcaptcha_enabled, get_member_roles, get_online_embed_user_keys, redis_store
from titanembeds.oauth import user_has_permission, generate_avatar_url, check_user_can_administrate_guild
import titanembeds.constants as constants
from flask import Blueprint, abort, jsonify, session, request, url_for
from flask import current_app as app
from flask_socketio import emit
@ -61,6 +62,17 @@ def format_post_content(guild_id, channel_id, message, dbUser):
mention = "<@" + match[2: len(match) - 1] + ">"
message = message.replace(match, mention, 1)
if dbguild.banned_words_enabled:
banned_words = set(json.loads(dbguild.banned_words))
if dbguild.banned_words_global_included:
banned_words = banned_words.union(set(constants.GLOBAL_BANNED_WORDS))
for word in banned_words:
word_boundaried = r"\b%s\b" % word
regex = re.compile(word_boundaried, re.IGNORECASE)
if regex.match(message):
illegal_post = True
illegal_reasons.append("The following word is prohibited: " + word)
if not guild_webhooks_enabled(guild_id):
if (session['unauthenticated']):
message = u"**[{}#{}]** {}".format(session['username'], session['user_id'], message)

View File

@ -219,6 +219,9 @@ def administrate_guild(guild_id):
"guest_icon": db_guild.guest_icon if db_guild.guest_icon != None else "",
"post_timeout": db_guild.post_timeout,
"max_message_length": db_guild.max_message_length,
"banned_words_enabled": db_guild.banned_words_enabled,
"banned_words_global_included": db_guild.banned_words_global_included,
"banned_words": json.loads(db_guild.banned_words),
}
return render_template("administrate_guild.html.j2", guild=dbguild_dict, members=users, permissions=permissions, cosmetics=cosmetics, disabled=(guild_id in list_disabled_guilds()))
@ -243,6 +246,8 @@ def update_administrate_guild(guild_id):
db_guild.unauth_captcha = request.form.get("unauth_captcha", db_guild.unauth_captcha) in ["true", True]
db_guild.post_timeout = request.form.get("post_timeout", db_guild.post_timeout)
db_guild.max_message_length = request.form.get("max_message_length", db_guild.max_message_length)
db_guild.banned_words_enabled = request.form.get("banned_words_enabled", db_guild.banned_words_enabled) in ["true", True]
db_guild.banned_words_global_included = request.form.get("banned_words_global_included", db_guild.banned_words_global_included) in ["true", True]
invite_link = request.form.get("invite_link", db_guild.invite_link)
if invite_link != None and invite_link.strip() == "":
@ -254,6 +259,16 @@ def update_administrate_guild(guild_id):
guest_icon = None
db_guild.guest_icon = guest_icon
banned_word = request.form.get("banned_word", None)
if banned_word:
delete_banned_word = request.form.get("delete_banned_word", False) in ["true", True]
banned_words = set(json.loads(db_guild.banned_words))
if delete_banned_word:
banned_words.discard(banned_word)
else:
banned_words.add(banned_word)
db_guild.banned_words = json.dumps(list(banned_words))
db.session.commit()
emit("guest_icon_change", {"guest_icon": guest_icon if guest_icon else url_for('static', filename='img/titanembeds_square.png')}, room="GUILD_"+guild_id, namespace="/gateway")
return jsonify(
@ -269,6 +284,9 @@ def update_administrate_guild(guild_id):
unauth_captcha=db_guild.unauth_captcha,
post_timeout=db_guild.post_timeout,
max_message_length=db_guild.max_message_length,
banned_words_enabled=db_guild.banned_words_enabled,
banned_words_global_included=db_guild.banned_words_global_included,
banned_words=json.loads(db_guild.banned_words),
)
@user.route("/add-bot/<guild_id>")