Titan/webapp/titanembeds/redisqueue.py

92 lines
3.6 KiB
Python
Raw Normal View History

from titanembeds.utils import redis_store
import json
import time
class RedisQueue:
def __init__(self):
pass # Nothing really to initialize
def get(self, key, resource, params, *, data_type="str"):
key = "Queue" + key
data = self._get(key, data_type)
payload = {
"key": key,
"resource": resource,
"params": params
}
loop_count = 0
2018-07-17 01:34:13 +02:00
while (not data and data != "") and loop_count < 50:
if loop_count % 25 == 0:
redis_store.publish("discord-api-req", json.dumps(payload))
2018-07-17 01:34:13 +02:00
time.sleep(0.1)
data = self._get(key, data_type)
loop_count += 1
redis_store.expire(key, 60 * 5)
2018-07-17 01:34:13 +02:00
if data == None or data == "":
return None
if data_type == "set":
data = list(data)
data_parsed = []
for d in data:
data_parsed.append(json.loads(d))
return data_parsed
return json.loads(data)
def _get(self, key, data_type):
if data_type == "set":
return redis_store.smembers(key)
else:
return redis_store.get(key)
def get_channel_messages(self, guild_id, channel_id, after_snowflake=0):
key = "/channels/{}/messages".format(channel_id)
q = self.get(key, "get_channel_messages", {"channel_id": channel_id}, data_type="set")
msgs = []
snowflakes = []
guild_members = {}
for x in q:
if x["id"] in snowflakes or int(x["id"]) <= int(after_snowflake):
continue
snowflakes.append(x["id"])
message = {
"attachments": x["attachments"],
"timestamp": x["timestamp"],
"id": x["id"],
"edited_timestamp": x["edited_timestamp"],
"author": x["author"],
"content": x["content"],
"channel_id": str(x["channel_id"]),
"mentions": x["mentions"],
"embeds": x["embeds"],
}
if message["author"]["id"] not in guild_members:
2018-07-17 01:34:13 +02:00
member = self.get_guild_member(guild_id, message["author"]["id"])
guild_members[message["author"]["id"]] = member
else:
member = guild_members[message["author"]["id"]]
message["author"]["nickname"] = None
if member:
2018-07-17 01:34:13 +02:00
message["author"]["nickname"] = member["nick"]
message["author"]["avatar"] = member["avatar"]
message["author"]["discriminator"] = member["discriminator"]
message["author"]["username"] = member["username"]
for mention in message["mentions"]:
if mention["id"] not in guild_members:
2018-07-17 01:34:13 +02:00
author = self.get_guild_member(guild_id, mention["id"])
guild_members[mention["id"]] = author
else:
author = guild_members[mention["id"]]
mention["nickname"] = None
if author:
2018-07-17 01:34:13 +02:00
mention["nickname"] = author["nick"]
mention["avatar"] = author["avatar"]
mention["username"] = author["username"]
mention["discriminator"] = author["discriminator"]
msgs.append(message)
sorted_msgs = sorted(msgs, key=lambda k: k['id'], reverse=True)
2018-07-17 01:34:13 +02:00
return sorted_msgs
def get_guild_member(self, guild_id, user_id):
key = "/guilds/{}/members/{}".format(guild_id, user_id)
q = self.get(key, "get_guild_member", {"guild_id": guild_id, "user_id": user_id})
return q