From 087606bd40c9c0d35d78895eba6a67e0113e0e15 Mon Sep 17 00:00:00 2001 From: Jeremy Zhang Date: Sat, 1 Jul 2017 06:52:21 +0000 Subject: [PATCH] Implemented Webhooks to make sending messages look more real, as an option --- discordbot/titanembeds/bot.py | 4 ++ discordbot/titanembeds/database/__init__.py | 15 +++++++ discordbot/titanembeds/database/guilds.py | 4 +- ...e8_added_webhooks_column_to_guild_table.py | 28 +++++++++++++ webapp/titanembeds/blueprints/api/api.py | 41 +++++++++++++++---- webapp/titanembeds/database/guilds.py | 4 +- webapp/titanembeds/discordrest.py | 17 ++++++++ webapp/titanembeds/static/js/embed.js | 4 ++ 8 files changed, 106 insertions(+), 11 deletions(-) create mode 100644 webapp/alembic/versions/9bf2adbc33e8_added_webhooks_column_to_guild_table.py diff --git a/discordbot/titanembeds/bot.py b/discordbot/titanembeds/bot.py index 25d4129..a6eb05e 100644 --- a/discordbot/titanembeds/bot.py +++ b/discordbot/titanembeds/bot.py @@ -212,3 +212,7 @@ class Titan(discord.Client): await self.database.update_guild(before[0].server) else: await self.database.update_guild(after[0].server) + + async def on_webhooks_update(self, server): + await self.wait_until_dbonline() + await self.database.update_guild(server) diff --git a/discordbot/titanembeds/database/__init__.py b/discordbot/titanembeds/database/__init__.py index 2d2df33..d2bbf39 100644 --- a/discordbot/titanembeds/database/__init__.py +++ b/discordbot/titanembeds/database/__init__.py @@ -119,6 +119,7 @@ class DatabaseInterface(object): session.commit() async def update_guild(self, guild): + server_webhooks = await self.bot.get_server_webhooks(guild) async with threadpool(): with self.get_session() as session: gui = session.query(Guilds).filter(Guilds.guild_id == guild.id).first() @@ -128,6 +129,7 @@ class DatabaseInterface(object): guild.name, json.dumps(self.get_roles_list(guild.roles)), json.dumps(self.get_channels_list(guild.channels)), + json.dumps(self.get_webhooks_list(server_webhooks)), json.dumps(self.get_emojis_list(guild.emojis)), guild.owner_id, guild.icon @@ -137,11 +139,24 @@ class DatabaseInterface(object): gui.name = guild.name gui.roles = json.dumps(self.get_roles_list(guild.roles)) gui.channels = json.dumps(self.get_channels_list(guild.channels)) + gui.webhooks = json.dumps(self.get_webhooks_list(server_webhooks)) gui.emojis = json.dumps(self.get_emojis_list(guild.emojis)) gui.owner_id = guild.owner_id gui.icon = guild.icon session.commit() + def get_webhooks_list(self, guild_webhooks): + webhooks = [] + for webhook in guild_webhooks: + webhooks.append({ + "id": webhook.id, + "guild_id": webhook.server.id, + "channel_id": webhook.channel.id, + "name": webhook.name, + "token": webhook.token, + }) + return webhooks + def get_emojis_list(self, guildemojis): emojis = [] for emote in guildemojis: diff --git a/discordbot/titanembeds/database/guilds.py b/discordbot/titanembeds/database/guilds.py index 54dfe69..f8c0169 100644 --- a/discordbot/titanembeds/database/guilds.py +++ b/discordbot/titanembeds/database/guilds.py @@ -12,12 +12,13 @@ class Guilds(Base): mentions_limit = db.Column(db.Integer) # If there is a limit on the number of mentions in a msg roles = db.Column(db.Text()) # Guild Roles channels = db.Column(db.Text()) # Guild channels + webhooks = db.Column(db.Text()) # Guild webhooks emojis = db.Column(db.Text()) # Guild Emojis owner_id = db.Column(db.String(255)) # Snowflake of the owner icon = db.Column(db.String(255)) # The icon string, null if none discordio = db.Column(db.String(255)) # Custom Discord.io Invite Link - def __init__(self, guild_id, name, roles, channels, emojis, owner_id, icon): + def __init__(self, guild_id, name, roles, channels, webhooks, emojis, owner_id, icon): self.guild_id = guild_id self.name = name self.unauth_users = True # defaults to true @@ -27,6 +28,7 @@ class Guilds(Base): self.mentions_limit = -1 # -1 = unlimited mentions self.roles = roles self.channels = channels + self.webhooks = webhooks self.emojis = emojis self.owner_id = owner_id self.icon = icon diff --git a/webapp/alembic/versions/9bf2adbc33e8_added_webhooks_column_to_guild_table.py b/webapp/alembic/versions/9bf2adbc33e8_added_webhooks_column_to_guild_table.py new file mode 100644 index 0000000..4cafcdf --- /dev/null +++ b/webapp/alembic/versions/9bf2adbc33e8_added_webhooks_column_to_guild_table.py @@ -0,0 +1,28 @@ +"""Added webhooks column to guild table + +Revision ID: 9bf2adbc33e8 +Revises: b1124468bb2e +Create Date: 2017-06-30 07:24:10.700408 + +""" + +# revision identifiers, used by Alembic. +revision = '9bf2adbc33e8' +down_revision = 'b1124468bb2e' +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('webhooks', sa.Text(), nullable=False)) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('guilds', 'webhooks') + # ### end Alembic commands ### diff --git a/webapp/titanembeds/blueprints/api/api.py b/webapp/titanembeds/blueprints/api/api.py index 05df2e8..8602b26 100644 --- a/webapp/titanembeds/blueprints/api/api.py +++ b/webapp/titanembeds/blueprints/api/api.py @@ -2,7 +2,7 @@ from titanembeds.database import db, Guilds, UnauthenticatedUsers, Unauthenticat from titanembeds.decorators import valid_session_required, discord_users_only 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 from titanembeds.oauth import user_has_permission, generate_avatar_url, check_user_can_administrate_guild -from flask import Blueprint, abort, jsonify, session, request +from flask import Blueprint, abort, jsonify, session, request, url_for from sqlalchemy import and_ import random import requests @@ -108,7 +108,7 @@ def parse_emoji(textToParse, guild_id): return textToParse -def format_post_content(guild_id, message): +def format_post_content(guild_id, channel_id, message): illegal_post = False illegal_reasons = [] message = message.replace("<", "\<") @@ -134,11 +134,12 @@ def format_post_content(guild_id, message): for match in all_mentions: mention = "<@" + match[2: len(match) - 1] + ">" message = message.replace(match, mention, 1) - - if (session['unauthenticated']): - message = u"**[{}#{}]** {}".format(session['username'], session['user_id'], message) - else: - message = u"**<{}#{}>** {}".format(session['username'], session['discriminator'], message) # I would like to do a @ mention, but i am worried about notif spam + + if not get_channel_webhook_url(guild_id, channel_id): + if (session['unauthenticated']): + message = u"**[{}#{}]** {}".format(session['username'], session['user_id'], message) + else: + message = u"**<{}#{}>** {}".format(session['username'], session['discriminator'], message) # I would like to do a @ mention, but i am worried about notif spam return (message, illegal_post, illegal_reasons) def format_everyone_mention(channel, content): @@ -294,6 +295,18 @@ def get_guild_emojis(guild_id): dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first() return json.loads(dbguild.emojis) +# Returns webhook url if exists and can post w/webhooks, otherwise None +def get_channel_webhook_url(guild_id, channel_id): + dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first() + guild_webhooks = json.loads(dbguild.webhooks) + for webhook in guild_webhooks: + if channel_id == webhook["channel_id"] and (webhook["name"].lower().startswith("titan") or webhook["name"].lower().startswith("[titan]")): + return { + "id": webhook["id"], + "token": webhook["token"] + } + return None + @api.route("/fetch", methods=["GET"]) @valid_session_required(api=True) @rate_limiter.limit("2 per 2 second", key_func = channel_ratelimit_key) @@ -348,7 +361,7 @@ def post(): guild_id = request.form.get("guild_id") channel_id = request.form.get('channel_id') content = request.form.get('content') - content, illegal_post, illegal_reasons = format_post_content(guild_id, content) + content, illegal_post, illegal_reasons = format_post_content(guild_id, channel_id, content) if user_unauthenticated(): key = session['user_keys'][guild_id] else: @@ -365,7 +378,17 @@ def post(): status_code = 401 elif not illegal_post: content = format_everyone_mention(chan, content) - message = discord_api.create_message(channel_id, content) + webhook = get_channel_webhook_url(guild_id, channel_id) + if webhook: + if (session['unauthenticated']): + username = session["username"] + "#" + str(session["user_id"]) + avatar = url_for('static', filename='img/titanembeds_round.png', _external=True) + else: + username = session["username"] + "#" + str(session["discriminator"]) + avatar = session['avatar'] + message = discord_api.execute_webhook(webhook.get("id"), webhook.get("token"), username, avatar, content) + else: + message = discord_api.create_message(channel_id, content) status_code = message['code'] response = jsonify(message=message.get('content', message), status=status, illegal_reasons=illegal_reasons) response.status_code = status_code diff --git a/webapp/titanembeds/database/guilds.py b/webapp/titanembeds/database/guilds.py index db086e9..d8b0985 100644 --- a/webapp/titanembeds/database/guilds.py +++ b/webapp/titanembeds/database/guilds.py @@ -12,12 +12,13 @@ class Guilds(db.Model): mentions_limit = db.Column(db.Integer, nullable=False, default=11) # If there is a limit on the number of mentions in a msg roles = db.Column(db.Text(), nullable=False) # Guild Roles channels = db.Column(db.Text(), nullable=False) # Guild channels + webhooks = db.Column(db.Text(), nullable=False) # Guild webhooks emojis = db.Column(db.Text(), nullable=False) # Guild Emojis owner_id = db.Column(db.String(255), nullable=False) # Snowflake of the owner icon = db.Column(db.String(255)) # The icon string, null if none discordio = db.Column(db.String(255)) # Custom Discord.io Invite Link - def __init__(self, guild_id, name, roles, channels, emojis, owner_id, icon): + def __init__(self, guild_id, name, roles, channels, webhooks, emojis, owner_id, icon): self.guild_id = guild_id self.name = name self.unauth_users = True # defaults to true @@ -27,6 +28,7 @@ class Guilds(db.Model): self.mentions_limit = -1 # -1 = unlimited mentions self.roles = roles self.channels = channels + self.webhooks = webhooks self.emojis = emojis self.owner_id = owner_id self.icon = icon diff --git a/webapp/titanembeds/discordrest.py b/webapp/titanembeds/discordrest.py index 5414203..4739c41 100644 --- a/webapp/titanembeds/discordrest.py +++ b/webapp/titanembeds/discordrest.py @@ -139,3 +139,20 @@ class DiscordREST: self.modify_guild_embed(guild_id, enabled=True, channel_id=guild_id) widget = requests.get(_endpoint).json() return widget + + + ##################### + # Webhook + ##################### + + def execute_webhook(self, webhook_id, webhook_token, username, avatar, content, wait=True): + _endpoint = "/webhooks/{id}/{token}".format(id=webhook_id, token=webhook_token) + if wait: + _endpoint += "?wait=true" + payload = { + 'content': content, + 'avatar_url': avatar, + 'username': username + } + r = self.request("POST", _endpoint, data=payload) + return r \ No newline at end of file diff --git a/webapp/titanembeds/static/js/embed.js b/webapp/titanembeds/static/js/embed.js index 0feb4fc..c1e2819 100644 --- a/webapp/titanembeds/static/js/embed.js +++ b/webapp/titanembeds/static/js/embed.js @@ -474,6 +474,10 @@ message.content = message.content.substring(usernamefield.length+7); message.author.username = usernamefield.split("#")[0]; message.author.discriminator = usernamefield.split("#")[1]; + } else if (message.author.bot && message.author.discriminator == "0000" && message.author.username.substring(message.author.username.length-5, message.author.username.length-4) == "#") { + var namestr = message.author.username; + message.author.username = namestr.substring(0,namestr.length-5); + message.author.discriminator = namestr.substring(namestr.length-4); } return message; }