Use redis to store discord api related objects, so far messages only

Keeping the table for now in case the redis method does not work as hoped
This commit is contained in:
Jeremy Zhang
2018-07-16 03:50:31 +00:00
parent 9faef4f761
commit bc129289fc
10 changed files with 346 additions and 139 deletions

View File

@ -1,4 +1,76 @@
import discord
import time
from email import utils as emailutils
def format_datetime(datetimeobj):
return emailutils.formatdate(time.mktime(datetimeobj.timetuple())) # https://stackoverflow.com/questions/3453177/convert-python-datetime-to-rfc-2822
def get_formatted_message(message):
edit_ts = message.edited_at
if not edit_ts:
edit_ts = None
else:
edit_ts = format_datetime(edit_ts)
msg = {
"id": str(message.id),
"channel_id": str(message.channel.id),
"content": message.content,
"author": get_message_author(message),
"timestamp": format_datetime(message.created_at),
"edited_timestamp": edit_ts,
}
if hasattr(message, "mentions"):
msg["mentions"] = get_message_mentions(message.mentions)
if hasattr(message, "attachments"):
msg["attachments"] = get_attachments_list(message.attachments)
if hasattr(message, "embeds"):
msg["embeds"] = get_embeds_list(message.embeds)
if hasattr(message, "author"):
nickname = None
if hasattr(message.author, 'nick') and message.author.nick:
nickname = message.author.nick
msg["author"]["nickname"] = nickname
if hasattr(message, "mentions"):
for mention in msg["mentions"]:
mention["nickname"] = None
member = message.guild.get_member(mention["id"])
if member:
mention["nickname"] = member.nick
return msg
def get_formatted_user(user):
userobj = {
"avatar": user.avatar,
"avatar_url": user.avatar_url,
"color": str(user.color)[1:],
"discriminator": user.discriminator,
"game": None,
"hoist-role": None,
"id": str(user.id),
"status": str(user.status),
"username": user.name,
"nick": None,
}
if userobj["color"] == "000000":
userobj["color"] = None
# if userobj["avatar_url"][len(userobj["avatar_url"])-15:] != ".jpg":
# userobj["avatar_url"] = userobj["avatar_url"][:len(userobj["avatar_url"])-14] + ".jpg"
if user.nick:
userobj["nick"] = user.nick
if hasattr(user, "activity") and user.activity:
userobj["activity"] = {
"name": user.activity.name
}
roles = sorted(user.roles, key=lambda k: k.position, reverse=True)
for role in roles:
if role.hoist:
userobj["hoist-role"] = {
"id": str(role.id),
"name": role.name,
"position": role.position,
}
break
return userobj
def get_message_author(message):
if not hasattr(message, "author"):
@ -12,6 +84,47 @@ def get_message_author(message):
"avatar": author.avatar
}
return obj
def get_formatted_emojis(emojis):
emotes = []
for emo in emojis:
emotes.append({
"id": str(emo.id),
"managed": emo.managed,
"name": emo.name,
"require_colons": emo.require_colons,
"roles": get_roles_list(emo.roles),
"url": emo.url,
})
return emotes
def get_formatted_guild(guild):
guil = {
"id": str(guild.id),
"name": guild.name,
"icon": guild.icon,
"icon_url": guild.icon_url,
}
return guil
def get_formatted_channel(channel):
chan = {
"id": str(channel.id),
"guild_id": str(channel.guild.id),
}
return chan
def get_formatted_role(role):
rol = {
"id": str(role.id),
"guild_id": str(role.guild.id),
"name": role.name,
"color": role.color.value,
"hoist": role.hoist,
"position": role.position,
"permissions": role.permissions.value,
}
return rol
def get_message_mentions(mentions):
ments = []