Implement autorole for embed users

This commit is contained in:
Jeremy Zhang 2018-07-22 06:46:03 +00:00
parent 856dd08167
commit 150e112387
9 changed files with 121 additions and 9 deletions

View File

@ -0,0 +1,30 @@
"""Added autorole columns to guild
Revision ID: 12267ce662e9
Revises: 87d043d7917e
Create Date: 2018-07-22 05:35:22.574854
"""
# revision identifiers, used by Alembic.
revision = '12267ce662e9'
down_revision = '87d043d7917e'
branch_labels = None
depends_on = None
from alembic import op
import sqlalchemy as sa
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('guilds', sa.Column('autorole_discord', sa.BigInteger(), nullable=True))
op.add_column('guilds', sa.Column('autorole_unauth', sa.BigInteger(), nullable=True))
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('guilds', 'autorole_unauth')
op.drop_column('guilds', 'autorole_discord')
# ### end Alembic commands ###

View File

@ -176,6 +176,7 @@ def administrate_guild(guild_id):
dbguild_dict = { dbguild_dict = {
"id": guild["id"], "id": guild["id"],
"name": guild["name"], "name": guild["name"],
"roles": guild["roles"],
"unauth_users": db_guild.unauth_users, "unauth_users": db_guild.unauth_users,
"visitor_view": db_guild.visitor_view, "visitor_view": db_guild.visitor_view,
"webhook_messages": db_guild.webhook_messages, "webhook_messages": db_guild.webhook_messages,
@ -191,6 +192,8 @@ def administrate_guild(guild_id):
"banned_words_enabled": db_guild.banned_words_enabled, "banned_words_enabled": db_guild.banned_words_enabled,
"banned_words_global_included": db_guild.banned_words_global_included, "banned_words_global_included": db_guild.banned_words_global_included,
"banned_words": json.loads(db_guild.banned_words), "banned_words": json.loads(db_guild.banned_words),
"autorole_unauth": db_guild.autorole_unauth,
"autorole_discord": db_guild.autorole_discord
} }
return render_template("administrate_guild.html.j2", guild=dbguild_dict, members=users, permissions=permissions, cosmetics=cosmetics) return render_template("administrate_guild.html.j2", guild=dbguild_dict, members=users, permissions=permissions, cosmetics=cosmetics)
@ -209,6 +212,8 @@ def update_administrate_guild(guild_id):
db_guild.max_message_length = request.form.get("max_message_length", db_guild.max_message_length) 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_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] db_guild.banned_words_global_included = request.form.get("banned_words_global_included", db_guild.banned_words_global_included) in ["true", True]
db_guild.autorole_unauth = request.form.get("autorole_unauth", db_guild.autorole_unauth, type=int)
db_guild.autorole_discord = request.form.get("autorole_discord", db_guild.autorole_discord, type=int)
invite_link = request.form.get("invite_link", db_guild.invite_link) invite_link = request.form.get("invite_link", db_guild.invite_link)
if invite_link != None and invite_link.strip() == "": if invite_link != None and invite_link.strip() == "":
invite_link = None invite_link = None
@ -244,6 +249,8 @@ def update_administrate_guild(guild_id):
banned_words_enabled=db_guild.banned_words_enabled, banned_words_enabled=db_guild.banned_words_enabled,
banned_words_global_included=db_guild.banned_words_global_included, banned_words_global_included=db_guild.banned_words_global_included,
banned_words=json.loads(db_guild.banned_words), banned_words=json.loads(db_guild.banned_words),
autorole_unauth=db_guild.autorole_unauth,
autorole_discord=db_guild.autorole_discord
) )
@admin.route("/guilds") @admin.route("/guilds")

View File

@ -1,6 +1,6 @@
from titanembeds.database import db, Guilds, UnauthenticatedUsers, UnauthenticatedBans, AuthenticatedUsers, get_administrators_list, get_badges, DiscordBotsOrgTransactions from titanembeds.database import db, Guilds, UnauthenticatedUsers, UnauthenticatedBans, AuthenticatedUsers, get_administrators_list, get_badges, DiscordBotsOrgTransactions
from titanembeds.decorators import valid_session_required, discord_users_only, abort_if_guild_disabled 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, redisqueue 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, redisqueue, get_forced_role
from titanembeds.oauth import user_has_permission, generate_avatar_url, check_user_can_administrate_guild from titanembeds.oauth import user_has_permission, generate_avatar_url, check_user_can_administrate_guild
import titanembeds.constants as constants import titanembeds.constants as constants
from flask import Blueprint, abort, jsonify, session, request, url_for from flask import Blueprint, abort, jsonify, session, request, url_for
@ -93,7 +93,8 @@ def format_everyone_mention(channel, content):
return content return content
def filter_guild_channel(guild_id, channel_id, force_everyone=False): def filter_guild_channel(guild_id, channel_id, force_everyone=False):
channels = get_guild_channels(guild_id, force_everyone) forced_role = get_forced_role(guild_id)
channels = get_guild_channels(guild_id, force_everyone, forced_role)
for chan in channels: for chan in channels:
if chan["channel"]["id"] == channel_id: if chan["channel"]["id"] == channel_id:
return chan return chan
@ -426,7 +427,8 @@ def get_guild_guest_icon(guild_id):
def process_query_guild(guild_id, visitor=False): def process_query_guild(guild_id, visitor=False):
widget = discord_api.get_widget(guild_id) widget = discord_api.get_widget(guild_id)
channels = get_guild_channels(guild_id, visitor) forced_role = get_forced_role(guild_id)
channels = get_guild_channels(guild_id, visitor, forced_role=forced_role)
if widget.get("success", True): if widget.get("success", True):
discordmembers = get_online_discord_users(guild_id, widget) discordmembers = get_online_discord_users(guild_id, widget)
else: else:

View File

@ -1,4 +1,4 @@
from titanembeds.utils import socketio, guild_accepts_visitors, get_client_ipaddr, discord_api, check_user_in_guild, get_guild_channels, update_user_status, guild_webhooks_enabled, redis_store, redisqueue from titanembeds.utils import socketio, guild_accepts_visitors, get_client_ipaddr, discord_api, check_user_in_guild, get_guild_channels, update_user_status, guild_webhooks_enabled, redis_store, redisqueue, get_forced_role
from titanembeds.database import db from titanembeds.database import db
from flask_socketio import Namespace, emit, disconnect, join_room, leave_room from flask_socketio import Namespace, emit, disconnect, join_room, leave_room
import functools import functools
@ -21,10 +21,11 @@ class Gateway(Namespace):
return return
session["socket_guild_id"] = guild_id session["socket_guild_id"] = guild_id
channels = [] channels = []
forced_role = get_forced_role(guild_id)
if guild_accepts_visitors(guild_id) and not check_user_in_guild(guild_id): if guild_accepts_visitors(guild_id) and not check_user_in_guild(guild_id):
channels = get_guild_channels(guild_id, force_everyone=True) channels = get_guild_channels(guild_id, force_everyone=True, forced_role=forced_role)
else: else:
channels = get_guild_channels(guild_id) channels = get_guild_channels(guild_id, forced_role=forced_role)
join_room("GUILD_"+guild_id) join_room("GUILD_"+guild_id)
for chan in channels: for chan in channels:
if chan["read"]: if chan["read"]:
@ -96,10 +97,11 @@ class Gateway(Namespace):
guild_id = data["guild_id"] guild_id = data["guild_id"]
visitor_mode = data["visitor_mode"] visitor_mode = data["visitor_mode"]
channels = None channels = None
forced_role = get_forced_role(guild_id)
if visitor_mode or session.get("unauthenticated", True): if visitor_mode or session.get("unauthenticated", True):
channels = get_guild_channels(guild_id, True) channels = get_guild_channels(guild_id, True, forced_role=forced_role)
else: else:
channels = get_guild_channels(guild_id) channels = get_guild_channels(guild_id, forced_role=forced_role)
for chan in channels: for chan in channels:
if chan["read"]: if chan["read"]:
join_room("CHANNEL_"+chan["channel"]["id"]) join_room("CHANNEL_"+chan["channel"]["id"])

View File

@ -213,6 +213,7 @@ def administrate_guild(guild_id):
dbguild_dict = { dbguild_dict = {
"id": db_guild.guild_id, "id": db_guild.guild_id,
"name": guild["name"], "name": guild["name"],
"roles": guild["roles"],
"unauth_users": db_guild.unauth_users, "unauth_users": db_guild.unauth_users,
"visitor_view": db_guild.visitor_view, "visitor_view": db_guild.visitor_view,
"webhook_messages": db_guild.webhook_messages, "webhook_messages": db_guild.webhook_messages,
@ -228,6 +229,8 @@ def administrate_guild(guild_id):
"banned_words_enabled": db_guild.banned_words_enabled, "banned_words_enabled": db_guild.banned_words_enabled,
"banned_words_global_included": db_guild.banned_words_global_included, "banned_words_global_included": db_guild.banned_words_global_included,
"banned_words": json.loads(db_guild.banned_words), "banned_words": json.loads(db_guild.banned_words),
"autorole_unauth": db_guild.autorole_unauth,
"autorole_discord": db_guild.autorole_discord
} }
return render_template("administrate_guild.html.j2", guild=dbguild_dict, members=users, permissions=permissions, cosmetics=cosmetics, disabled=(guild_id in list_disabled_guilds())) return render_template("administrate_guild.html.j2", guild=dbguild_dict, members=users, permissions=permissions, cosmetics=cosmetics, disabled=(guild_id in list_disabled_guilds()))
@ -254,6 +257,8 @@ def update_administrate_guild(guild_id):
db_guild.max_message_length = request.form.get("max_message_length", db_guild.max_message_length) 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_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] db_guild.banned_words_global_included = request.form.get("banned_words_global_included", db_guild.banned_words_global_included) in ["true", True]
db_guild.autorole_unauth = request.form.get("autorole_unauth", db_guild.autorole_unauth, type=int)
db_guild.autorole_discord = request.form.get("autorole_discord", db_guild.autorole_discord, type=int)
invite_link = request.form.get("invite_link", db_guild.invite_link) invite_link = request.form.get("invite_link", db_guild.invite_link)
if invite_link != None and invite_link.strip() == "": if invite_link != None and invite_link.strip() == "":
@ -293,6 +298,8 @@ def update_administrate_guild(guild_id):
banned_words_enabled=db_guild.banned_words_enabled, banned_words_enabled=db_guild.banned_words_enabled,
banned_words_global_included=db_guild.banned_words_global_included, banned_words_global_included=db_guild.banned_words_global_included,
banned_words=json.loads(db_guild.banned_words), banned_words=json.loads(db_guild.banned_words),
autorole_unauth=db_guild.autorole_unauth,
autorole_discord=db_guild.autorole_discord
) )
@user.route("/add-bot/<guild_id>") @user.route("/add-bot/<guild_id>")

View File

@ -17,6 +17,8 @@ class Guilds(db.Model):
banned_words_enabled = db.Column(db.Boolean(), nullable=False, server_default="0") # If banned words are enforced banned_words_enabled = db.Column(db.Boolean(), nullable=False, server_default="0") # If banned words are enforced
banned_words_global_included = db.Column(db.Boolean(), nullable=False, server_default="0") # Add global banned words to the list banned_words_global_included = db.Column(db.Boolean(), nullable=False, server_default="0") # Add global banned words to the list
banned_words = db.Column(db.Text(), nullable=False, server_default="[]") # JSON list of strings to block from sending banned_words = db.Column(db.Text(), nullable=False, server_default="[]") # JSON list of strings to block from sending
autorole_unauth = db.Column(db.BigInteger, nullable=True, server_default=None) # Automatic Role inherit for unauthenticated users
autorole_discord = db.Column(db.BigInteger, nullable=True, server_default=None) # Automatic Role inherit for discord users
def __init__(self, guild_id): def __init__(self, guild_id):
self.guild_id = guild_id self.guild_id = guild_id

View File

@ -1,4 +1,5 @@
$('.chips').material_chip(); $('.chips').material_chip();
$('select').material_select();
$('#unauth_users').change(function() { $('#unauth_users').change(function() {
var pathname = window.location.pathname; var pathname = window.location.pathname;
@ -154,6 +155,24 @@ function add_delete_banned_words(action, word) {
}); });
} }
$("#autorole_unauth").change(function () {
var pathname = window.location.pathname;
var value = $(this).val();
var payload = {"autorole_unauth": value}
$.post(pathname, payload, function(data) {
Materialize.toast('Updated Guest AutoRole setting!', 2000)
});
});
$("#autorole_discord").change(function () {
var pathname = window.location.pathname;
var value = $(this).val();
var payload = {"autorole_discord": value}
$.post(pathname, payload, function(data) {
Materialize.toast('Updated Discord AutoRole setting!', 2000)
});
});
function initiate_ban(guild_id, user_id) { function initiate_ban(guild_id, user_id) {
var reason = prompt("Please enter your reason for ban"); var reason = prompt("Please enter your reason for ban");
var payload = { var payload = {

View File

@ -186,6 +186,34 @@
</div> </div>
<p>(Hit enter after each word to add to list)</p> <p>(Hit enter after each word to add to list)</p>
<div id="banned_words" class="chips"></div> <div id="banned_words" class="chips"></div>
<p class="flow-text">AutoRole Embed Users</p>
<p>A "hidden" role may be applied and inherited to users using the embed. Enabling this will allow the user to have different permission on the embed than in Discord. May be used so that the embed users see different channels for example.</p>
<div class="col s12">
<div class="input-field col m6 s12">
<select id="autorole_unauth">
<option value="0" {% if not guild["autorole_unauth"] %}selected{% endif %}>No Role</option>
{% for role in guild["roles"]|sort(attribute="position", reverse=True) %}
{% if role.id|int != guild.id|int %}
<option value="{{ role.id }}" {% if role.color %}style="color: #{{ '{0:0{1}x}'.format(role.color, 6) }};"{% endif %} {% if role.id|int == guild["autorole_unauth"]|int %}selected{% endif %}>{{ role.name }}</option>
{% endif %}
{% endfor %}
</select>
<label>Guests/Visitors Auto Role</label>
</div>
<div class="input-field col m6 s12">
<select id="autorole_discord">
<option value="0" {% if not guild["autorole_unauth"] %}selected{% endif %}>No Role</option>
{% for role in guild["roles"]|sort(attribute="position", reverse=True) %}
{% if role.id|int != guild.id|int %}
<option value="{{ role.id }}" {% if role.color %}style="color: #{{ '{0:0{1}x}'.format(role.color, 6) }};"{% endif %} {% if role.id|int == guild["autorole_discord"]|int %}selected{% endif %}>{{ role.name }}</option>
{% endif %}
{% endfor %}
</select>
<label>Discord Users Auto Role</label>
</div>
</div>
</div> </div>
</div> </div>
</div> </div>

View File

@ -200,13 +200,15 @@ def get_member_roles(guild_id, user_id):
role_converted.append(str(role)) role_converted.append(str(role))
return role_converted return role_converted
def get_guild_channels(guild_id, force_everyone=False): def get_guild_channels(guild_id, force_everyone=False, forced_role=0):
if user_unauthenticated() or force_everyone: if user_unauthenticated() or force_everyone:
member_roles = [guild_id] #equivilant to @everyone role member_roles = [guild_id] #equivilant to @everyone role
else: else:
member_roles = get_member_roles(guild_id, session['user_id']) member_roles = get_member_roles(guild_id, session['user_id'])
if guild_id not in member_roles: if guild_id not in member_roles:
member_roles.append(guild_id) member_roles.append(guild_id)
if forced_role:
member_roles.append(str(forced_role))
bot_member_roles = get_member_roles(guild_id, config["client-id"]) bot_member_roles = get_member_roles(guild_id, config["client-id"])
if guild_id not in bot_member_roles: if guild_id not in bot_member_roles:
bot_member_roles.append(guild_id) bot_member_roles.append(guild_id)
@ -239,6 +241,11 @@ def get_channel_permission(channel, guild_id, guild_owner, guild_roles, member_r
return result return result
channel_perm = 0 channel_perm = 0
role_positions = {}
for role in guild_roles:
role_positions[str(role["id"])] = role["position"]
member_roles = sorted(member_roles, key=lambda x: role_positions.get(str(x), -1), reverse=True)
# @everyone # @everyone
for role in guild_roles: for role in guild_roles:
if role["id"] == guild_id: if role["id"] == guild_id:
@ -291,6 +298,14 @@ def get_channel_permission(channel, guild_id, guild_owner, guild_roles, member_r
result["mention_everyone"] = False result["mention_everyone"] = False
return result return result
def get_forced_role(guild_id):
dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first()
if not session.get("unauthenticated", True):
forced_role = dbguild.autorole_discord
else:
forced_role = dbguild.autorole_unauth
return forced_role
def bot_can_create_webhooks(guild): def bot_can_create_webhooks(guild):
perm = 0 perm = 0
guild_roles = guild["roles"] guild_roles = guild["roles"]