Implemented Webhooks to make sending messages look more real, as an option

This commit is contained in:
Jeremy Zhang 2017-07-01 06:52:21 +00:00
parent 3c82b3b6b0
commit 087606bd40
8 changed files with 106 additions and 11 deletions

View File

@ -212,3 +212,7 @@ class Titan(discord.Client):
await self.database.update_guild(before[0].server) await self.database.update_guild(before[0].server)
else: else:
await self.database.update_guild(after[0].server) 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)

View File

@ -119,6 +119,7 @@ class DatabaseInterface(object):
session.commit() session.commit()
async def update_guild(self, guild): async def update_guild(self, guild):
server_webhooks = await self.bot.get_server_webhooks(guild)
async with threadpool(): async with threadpool():
with self.get_session() as session: with self.get_session() as session:
gui = session.query(Guilds).filter(Guilds.guild_id == guild.id).first() gui = session.query(Guilds).filter(Guilds.guild_id == guild.id).first()
@ -128,6 +129,7 @@ class DatabaseInterface(object):
guild.name, guild.name,
json.dumps(self.get_roles_list(guild.roles)), json.dumps(self.get_roles_list(guild.roles)),
json.dumps(self.get_channels_list(guild.channels)), json.dumps(self.get_channels_list(guild.channels)),
json.dumps(self.get_webhooks_list(server_webhooks)),
json.dumps(self.get_emojis_list(guild.emojis)), json.dumps(self.get_emojis_list(guild.emojis)),
guild.owner_id, guild.owner_id,
guild.icon guild.icon
@ -137,11 +139,24 @@ class DatabaseInterface(object):
gui.name = guild.name gui.name = guild.name
gui.roles = json.dumps(self.get_roles_list(guild.roles)) gui.roles = json.dumps(self.get_roles_list(guild.roles))
gui.channels = json.dumps(self.get_channels_list(guild.channels)) 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.emojis = json.dumps(self.get_emojis_list(guild.emojis))
gui.owner_id = guild.owner_id gui.owner_id = guild.owner_id
gui.icon = guild.icon gui.icon = guild.icon
session.commit() 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): def get_emojis_list(self, guildemojis):
emojis = [] emojis = []
for emote in guildemojis: for emote in guildemojis:

View File

@ -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 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 roles = db.Column(db.Text()) # Guild Roles
channels = db.Column(db.Text()) # Guild channels channels = db.Column(db.Text()) # Guild channels
webhooks = db.Column(db.Text()) # Guild webhooks
emojis = db.Column(db.Text()) # Guild Emojis emojis = db.Column(db.Text()) # Guild Emojis
owner_id = db.Column(db.String(255)) # Snowflake of the owner owner_id = db.Column(db.String(255)) # Snowflake of the owner
icon = db.Column(db.String(255)) # The icon string, null if none icon = db.Column(db.String(255)) # The icon string, null if none
discordio = db.Column(db.String(255)) # Custom Discord.io Invite Link 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.guild_id = guild_id
self.name = name self.name = name
self.unauth_users = True # defaults to true self.unauth_users = True # defaults to true
@ -27,6 +28,7 @@ class Guilds(Base):
self.mentions_limit = -1 # -1 = unlimited mentions self.mentions_limit = -1 # -1 = unlimited mentions
self.roles = roles self.roles = roles
self.channels = channels self.channels = channels
self.webhooks = webhooks
self.emojis = emojis self.emojis = emojis
self.owner_id = owner_id self.owner_id = owner_id
self.icon = icon self.icon = icon

View File

@ -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 ###

View File

@ -2,7 +2,7 @@ from titanembeds.database import db, Guilds, UnauthenticatedUsers, Unauthenticat
from titanembeds.decorators import valid_session_required, discord_users_only 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.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 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_ from sqlalchemy import and_
import random import random
import requests import requests
@ -108,7 +108,7 @@ def parse_emoji(textToParse, guild_id):
return textToParse return textToParse
def format_post_content(guild_id, message): def format_post_content(guild_id, channel_id, message):
illegal_post = False illegal_post = False
illegal_reasons = [] illegal_reasons = []
message = message.replace("<", "\<") message = message.replace("<", "\<")
@ -135,6 +135,7 @@ def format_post_content(guild_id, message):
mention = "<@" + match[2: len(match) - 1] + ">" mention = "<@" + match[2: len(match) - 1] + ">"
message = message.replace(match, mention, 1) message = message.replace(match, mention, 1)
if not get_channel_webhook_url(guild_id, channel_id):
if (session['unauthenticated']): if (session['unauthenticated']):
message = u"**[{}#{}]** {}".format(session['username'], session['user_id'], message) message = u"**[{}#{}]** {}".format(session['username'], session['user_id'], message)
else: else:
@ -294,6 +295,18 @@ def get_guild_emojis(guild_id):
dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first() dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first()
return json.loads(dbguild.emojis) 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"]) @api.route("/fetch", methods=["GET"])
@valid_session_required(api=True) @valid_session_required(api=True)
@rate_limiter.limit("2 per 2 second", key_func = channel_ratelimit_key) @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") guild_id = request.form.get("guild_id")
channel_id = request.form.get('channel_id') channel_id = request.form.get('channel_id')
content = request.form.get('content') 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(): if user_unauthenticated():
key = session['user_keys'][guild_id] key = session['user_keys'][guild_id]
else: else:
@ -365,6 +378,16 @@ def post():
status_code = 401 status_code = 401
elif not illegal_post: elif not illegal_post:
content = format_everyone_mention(chan, content) content = format_everyone_mention(chan, 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) message = discord_api.create_message(channel_id, content)
status_code = message['code'] status_code = message['code']
response = jsonify(message=message.get('content', message), status=status, illegal_reasons=illegal_reasons) response = jsonify(message=message.get('content', message), status=status, illegal_reasons=illegal_reasons)

View File

@ -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 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 roles = db.Column(db.Text(), nullable=False) # Guild Roles
channels = db.Column(db.Text(), nullable=False) # Guild channels 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 emojis = db.Column(db.Text(), nullable=False) # Guild Emojis
owner_id = db.Column(db.String(255), nullable=False) # Snowflake of the owner 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 icon = db.Column(db.String(255)) # The icon string, null if none
discordio = db.Column(db.String(255)) # Custom Discord.io Invite Link 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.guild_id = guild_id
self.name = name self.name = name
self.unauth_users = True # defaults to true self.unauth_users = True # defaults to true
@ -27,6 +28,7 @@ class Guilds(db.Model):
self.mentions_limit = -1 # -1 = unlimited mentions self.mentions_limit = -1 # -1 = unlimited mentions
self.roles = roles self.roles = roles
self.channels = channels self.channels = channels
self.webhooks = webhooks
self.emojis = emojis self.emojis = emojis
self.owner_id = owner_id self.owner_id = owner_id
self.icon = icon self.icon = icon

View File

@ -139,3 +139,20 @@ class DiscordREST:
self.modify_guild_embed(guild_id, enabled=True, channel_id=guild_id) self.modify_guild_embed(guild_id, enabled=True, channel_id=guild_id)
widget = requests.get(_endpoint).json() widget = requests.get(_endpoint).json()
return widget 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

View File

@ -474,6 +474,10 @@
message.content = message.content.substring(usernamefield.length+7); message.content = message.content.substring(usernamefield.length+7);
message.author.username = usernamefield.split("#")[0]; message.author.username = usernamefield.split("#")[0];
message.author.discriminator = usernamefield.split("#")[1]; 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; return message;
} }