示例#1
0
文件: gateway.py 项目: advaith1/Titan
 def on_disconnect(self):
     if "user_keys" not in session:
         return
     if "socket_guild_id" not in session:
         disconnect()
     else:
         guild_id = session["socket_guild_id"]
         msg = {}
         if session["unauthenticated"]:
             msg = {
                 "unauthenticated": True,
                 "username": session["username"],
                 "discriminator": session["user_id"]
             }
         else:
             msg = {"unauthenticated": False, "id": str(session["user_id"])}
         emit("embed_user_disconnect", msg, room="GUILD_" + guild_id)
         if guild_webhooks_enabled(guild_id):  # Delete webhooks
             dbguild = db.session.query(Guilds).filter(
                 Guilds.guild_id == guild_id).first()
             guild_webhooks = json.loads(dbguild.webhooks)
             name = "[Titan] "
             username = session["username"]
             if len(username) > 19:
                 username = username[:19]
             if session["unauthenticated"]:
                 name = name + username + "#" + str(session["user_id"])
             else:
                 name = name + username + "#" + str(
                     session["discriminator"])
             for webhook in guild_webhooks:
                 if webhook["name"] == name:
                     discord_api.delete_webhook(webhook["id"],
                                                webhook["token"])
     self.teardown_db_session()
示例#2
0
 def on_disconnect(self):
     if "user_keys" not in session:
         self.teardown_db_session()
         return
     if "socket_guild_id" not in session:
         self.teardown_db_session()
         return
     else:
         guild_id = session["socket_guild_id"]
         msg = {}
         if session["unauthenticated"]:
             msg = {
                 "unauthenticated": True,
                 "username": session["username"],
                 "discriminator": session["user_id"]
             }
         else:
             msg = {"unauthenticated": False, "id": str(session["user_id"])}
         emit("embed_user_disconnect", msg, room="GUILD_" + guild_id)
         if guild_webhooks_enabled(guild_id):  # Delete webhooks
             guild_webhooks = redisqueue.get_guild(guild_id)["webhooks"]
             name = "[Titan] "
             username = session["username"]
             if len(username) > 19:
                 username = username[:19]
             if session["unauthenticated"]:
                 name = name + username + "#" + str(session["user_id"])
             else:
                 name = name + username + "#" + str(
                     session["discriminator"])
             for webhook in guild_webhooks:
                 if webhook["name"] == name:
                     discord_api.delete_webhook(webhook["id"],
                                                webhook["token"])
     self.teardown_db_session()
示例#3
0
文件: api.py 项目: jay121-git/Titan
def get_channel_webhook_url(guild_id, channel_id):
    if not guild_webhooks_enabled(guild_id):
        return None
    guild = redisqueue.get_guild(guild_id)
    guild_webhooks = guild["webhooks"]
    name = "[Titan] "
    username = session["username"]
    if len(username) > 19:
        username = username[:19]
    if user_unauthenticated():
        name = name + username + "#" + str(session["user_id"])
    else:
        name = name + username + "#" + str(session["discriminator"])
    for webhook in guild_webhooks:
        if channel_id == webhook["channel_id"] and webhook["name"] == name:
            return {
                "id": webhook["id"],
                "token": webhook["token"],
                "name": webhook.get("name"),
                "guild_id": webhook.get("guild_id"),
                "channel_id": webhook.get("channel_id")
            }
    webhook = discord_api.create_webhook(channel_id, name)
    if webhook and "content" in webhook:
        return webhook["content"]
    else:
        return None
示例#4
0
文件: api.py 项目: Guardian820/Titan
def format_post_content(guild_id, channel_id, message, dbUser):
    illegal_post = False
    illegal_reasons = []
    message = message.replace("<", "\<")
    message = message.replace(">", "\>")
    message = parse_emoji(message, guild_id)

    dbguild = db.session.query(Guilds).filter(
        Guilds.guild_id == guild_id).first()

    max_len = get_post_content_max_len(guild_id)
    if len(message) > max_len:
        illegal_post = True
        illegal_reasons.append(
            "Exceeded the following message length: {} characters".format(
                max_len))

    links = re.findall(
        'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+',
        message)
    if not dbguild.chat_links and len(links) > 0:
        illegal_post = True
        illegal_reasons.append("Links is not allowed.")
    elif dbguild.chat_links and not dbguild.bracket_links:
        for link in links:
            newlink = "<" + link + ">"
            message = message.replace(link, newlink)

    mention_pattern = re.compile(r'\[@[0-9]+\]')
    all_mentions = re.findall(mention_pattern, message)
    if dbguild.mentions_limit != -1 and len(
            all_mentions) > dbguild.mentions_limit:
        illegal_post = True
        illegal_reasons.append("Mentions is capped at the following limit: " +
                               str(dbguild.mentions_limit))
    for match in all_mentions:
        mention = "<@" + match[2:len(match) - 1] + ">"
        message = message.replace(match, mention, 1)

    if not guild_webhooks_enabled(guild_id):
        if (session['unauthenticated']):
            message = u"**[{}#{}]** {}".format(session['username'],
                                               session['user_id'], message)
        else:
            username = session['username']
            if dbUser:
                if dbUser.nickname:
                    username = dbUser.nickname
            message = u"**<{}#{}>** {}".format(
                username, session['discriminator'], message
            )  # I would like to do a @ mention, but i am worried about notify spam
    return (message, illegal_post, illegal_reasons)
示例#5
0
文件: api.py 项目: jay121-git/Titan
def delete_webhook_if_too_much(webhook):
    if not webhook:
        return
    guild_id = webhook["guild_id"]
    if guild_webhooks_enabled(guild_id):
        guild = redisqueue.get_guild(guild_id)
        guild_webhooks = guild["webhooks"]
        total_wh_cnt = len(guild_webhooks)
        titan_wh_cnt = 0
        for wh in guild_webhooks:
            if wh["name"].startswith("[Titan] "):
                titan_wh_cnt = titan_wh_cnt + 1
        if titan_wh_cnt > 0 and total_wh_cnt >= 8:
            try:
                discord_api.delete_webhook(webhook["id"], webhook["token"])
            except:
                pass  # not my problem now
示例#6
0
def get_channel_webhook_url(guild_id, channel_id):
    if not guild_webhooks_enabled(guild_id):
        return None
    dbguild = db.session.query(Guilds).filter(
        Guilds.guild_id == guild_id).first()
    guild_webhooks = json.loads(dbguild.webhooks)
    name = "[Titan] "
    username = session["username"]
    if len(username) > 19:
        username = username[:19]
    if user_unauthenticated():
        name = name + username + "#" + str(session["user_id"])
    else:
        name = name + username + "#" + str(session["discriminator"])
    for webhook in guild_webhooks:
        if channel_id == webhook["channel_id"] and webhook["name"] == name:
            return {"id": webhook["id"], "token": webhook["token"]}
    webhook = discord_api.create_webhook(channel_id, name)
    return webhook["content"]