2017-08-20 21:56:54 +02:00
|
|
|
import discord
|
2018-07-16 05:50:31 +02:00
|
|
|
import time
|
|
|
|
from email import utils as emailutils
|
|
|
|
|
|
|
|
def format_datetime(datetimeobj):
|
2021-06-01 23:12:31 +02:00
|
|
|
return emailutils.format_datetime(datetimeobj) # https://stackoverflow.com/questions/3453177/convert-python-datetime-to-rfc-2822
|
2018-07-16 05:50:31 +02:00
|
|
|
|
|
|
|
def get_formatted_message(message):
|
|
|
|
edit_ts = message.edited_at
|
|
|
|
if not edit_ts:
|
|
|
|
edit_ts = None
|
|
|
|
else:
|
|
|
|
edit_ts = format_datetime(edit_ts)
|
2019-10-17 09:50:34 +02:00
|
|
|
msg_type = message.type
|
|
|
|
if isinstance(msg_type, int):
|
|
|
|
msg_type = int(msg_type)
|
|
|
|
else:
|
|
|
|
msg_type = message.type.value
|
2018-07-16 05:50:31 +02:00
|
|
|
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,
|
2019-10-17 09:50:34 +02:00
|
|
|
"type": msg_type,
|
2018-07-16 05:50:31 +02:00
|
|
|
}
|
|
|
|
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
|
2018-07-27 10:57:54 +02:00
|
|
|
if hasattr(message, "reactions"):
|
|
|
|
msg["reactions"] = get_message_reactions(message.reactions)
|
2018-07-16 05:50:31 +02:00
|
|
|
return msg
|
|
|
|
|
|
|
|
def get_formatted_user(user):
|
|
|
|
userobj = {
|
|
|
|
"avatar": user.avatar,
|
2019-10-08 07:34:39 +02:00
|
|
|
"avatar_url": str(user.avatar_url_as(static_format="png", size=512)),
|
2018-07-16 05:50:31 +02:00
|
|
|
"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,
|
2018-07-18 01:47:30 +02:00
|
|
|
"bot": user.bot,
|
|
|
|
"roles": []
|
2018-07-16 05:50:31 +02:00
|
|
|
}
|
|
|
|
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:
|
2018-07-18 02:38:37 +02:00
|
|
|
userobj["roles"].append(str(role.id))
|
2018-07-18 01:47:30 +02:00
|
|
|
if role.hoist and userobj["hoist-role"] == None:
|
2018-07-16 05:50:31 +02:00
|
|
|
userobj["hoist-role"] = {
|
|
|
|
"id": str(role.id),
|
|
|
|
"name": role.name,
|
|
|
|
"position": role.position,
|
|
|
|
}
|
|
|
|
return userobj
|
2017-08-20 21:56:54 +02:00
|
|
|
|
|
|
|
def get_message_author(message):
|
2018-03-22 23:55:09 +01:00
|
|
|
if not hasattr(message, "author"):
|
|
|
|
return {}
|
2017-08-20 21:56:54 +02:00
|
|
|
author = message.author
|
|
|
|
obj = {
|
|
|
|
"username": author.name,
|
|
|
|
"discriminator": author.discriminator,
|
|
|
|
"bot": author.bot,
|
2018-03-22 23:55:09 +01:00
|
|
|
"id": str(author.id),
|
2017-08-20 21:56:54 +02:00
|
|
|
"avatar": author.avatar
|
|
|
|
}
|
|
|
|
return obj
|
2018-07-16 05:50:31 +02:00
|
|
|
|
|
|
|
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),
|
2019-10-08 07:52:11 +02:00
|
|
|
"url": str(emo.url),
|
2018-07-16 05:50:31 +02:00
|
|
|
})
|
|
|
|
return emotes
|
|
|
|
|
2018-07-21 01:10:24 +02:00
|
|
|
def get_formatted_guild(guild, webhooks=[]):
|
2018-07-16 05:50:31 +02:00
|
|
|
guil = {
|
|
|
|
"id": str(guild.id),
|
|
|
|
"name": guild.name,
|
|
|
|
"icon": guild.icon,
|
2019-10-08 07:34:39 +02:00
|
|
|
"icon_url": str(guild.icon_url),
|
2018-07-21 01:10:24 +02:00
|
|
|
"owner_id": guild.owner_id,
|
|
|
|
"roles": get_roles_list(guild.roles),
|
|
|
|
"channels": get_channels_list(guild.channels),
|
|
|
|
"webhooks": get_webhooks_list(webhooks),
|
|
|
|
"emojis": get_emojis_list(guild.emojis)
|
2018-07-16 05:50:31 +02:00
|
|
|
}
|
|
|
|
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
|
2017-08-20 21:56:54 +02:00
|
|
|
|
|
|
|
def get_message_mentions(mentions):
|
|
|
|
ments = []
|
|
|
|
for author in mentions:
|
|
|
|
ments.append({
|
|
|
|
"username": author.name,
|
|
|
|
"discriminator": author.discriminator,
|
|
|
|
"bot": author.bot,
|
2018-03-22 23:55:09 +01:00
|
|
|
"id": str(author.id),
|
2017-08-20 21:56:54 +02:00
|
|
|
"avatar": author.avatar
|
|
|
|
})
|
|
|
|
return ments
|
|
|
|
|
|
|
|
def get_webhooks_list(guild_webhooks):
|
|
|
|
webhooks = []
|
|
|
|
for webhook in guild_webhooks:
|
2018-10-28 02:07:41 +01:00
|
|
|
if not webhook.channel or not webhook.guild:
|
|
|
|
continue
|
2017-08-20 21:56:54 +02:00
|
|
|
webhooks.append({
|
2018-03-22 23:55:09 +01:00
|
|
|
"id": str(webhook.id),
|
|
|
|
"guild_id": str(webhook.guild.id),
|
|
|
|
"channel_id": str(webhook.channel.id),
|
2017-08-20 21:56:54 +02:00
|
|
|
"name": webhook.name,
|
|
|
|
"token": webhook.token,
|
|
|
|
})
|
|
|
|
return webhooks
|
|
|
|
|
|
|
|
def get_emojis_list(guildemojis):
|
|
|
|
emojis = []
|
|
|
|
for emote in guildemojis:
|
|
|
|
emojis.append({
|
2018-03-22 23:55:09 +01:00
|
|
|
"id": str(emote.id),
|
2017-08-20 21:56:54 +02:00
|
|
|
"name": emote.name,
|
|
|
|
"require_colons": emote.require_colons,
|
|
|
|
"managed": emote.managed,
|
|
|
|
"roles": list_role_ids(emote.roles),
|
2019-10-08 07:34:39 +02:00
|
|
|
"url": str(emote.url),
|
2018-03-01 08:57:45 +01:00
|
|
|
"animated": emote.animated
|
2017-08-20 21:56:54 +02:00
|
|
|
})
|
|
|
|
return emojis
|
|
|
|
|
|
|
|
def get_roles_list(guildroles):
|
|
|
|
roles = []
|
|
|
|
for role in guildroles:
|
|
|
|
roles.append({
|
2018-03-22 23:55:09 +01:00
|
|
|
"id": str(role.id),
|
2017-08-20 21:56:54 +02:00
|
|
|
"name": role.name,
|
|
|
|
"color": role.color.value,
|
|
|
|
"hoist": role.hoist,
|
|
|
|
"position": role.position,
|
|
|
|
"permissions": role.permissions.value
|
|
|
|
})
|
|
|
|
return roles
|
|
|
|
|
|
|
|
def get_channels_list(guildchannels):
|
|
|
|
channels = []
|
|
|
|
for channel in guildchannels:
|
2018-03-22 23:55:09 +01:00
|
|
|
if isinstance(channel, discord.channel.TextChannel) or isinstance(channel, discord.channel.CategoryChannel):
|
2017-08-20 21:56:54 +02:00
|
|
|
overwrites = []
|
2018-03-22 23:55:09 +01:00
|
|
|
isTextChannel = isinstance(channel, discord.channel.TextChannel)
|
2019-10-08 09:18:11 +02:00
|
|
|
for target, overwrite in channel.overwrites.items():
|
2018-10-09 23:13:19 +02:00
|
|
|
if not target:
|
|
|
|
continue
|
2017-08-20 21:56:54 +02:00
|
|
|
if isinstance(target, discord.Role):
|
|
|
|
type = "role"
|
|
|
|
else:
|
|
|
|
type = "member"
|
|
|
|
allow, deny = overwrite.pair()
|
|
|
|
allow = allow.value
|
|
|
|
deny = deny.value
|
|
|
|
overwrites.append({
|
2018-03-22 23:55:09 +01:00
|
|
|
"id": str(target.id),
|
2017-08-20 21:56:54 +02:00
|
|
|
"type": type,
|
|
|
|
"allow": allow,
|
|
|
|
"deny": deny,
|
|
|
|
})
|
2018-03-22 23:55:09 +01:00
|
|
|
parent = channel.category
|
2017-09-09 23:46:00 +02:00
|
|
|
if parent:
|
2018-03-22 23:55:09 +01:00
|
|
|
parent = str(parent.id)
|
2017-08-20 21:56:54 +02:00
|
|
|
channels.append({
|
2018-03-22 23:55:09 +01:00
|
|
|
"id": str(channel.id),
|
2017-08-20 21:56:54 +02:00
|
|
|
"name": channel.name,
|
2018-03-22 23:55:09 +01:00
|
|
|
"topic": channel.topic if isTextChannel else None,
|
2017-08-20 21:56:54 +02:00
|
|
|
"position": channel.position,
|
2018-03-22 23:55:09 +01:00
|
|
|
"type": "text" if isTextChannel else "category",
|
2017-09-09 23:46:00 +02:00
|
|
|
"permission_overwrites": overwrites,
|
|
|
|
"parent_id": parent,
|
2018-03-22 23:55:09 +01:00
|
|
|
"nsfw": channel.is_nsfw(),
|
2017-08-20 21:56:54 +02:00
|
|
|
})
|
2017-08-22 08:57:30 +02:00
|
|
|
return channels
|
|
|
|
|
|
|
|
def list_role_ids(usr_roles):
|
|
|
|
ids = []
|
|
|
|
for role in usr_roles:
|
2018-03-22 23:55:09 +01:00
|
|
|
ids.append(str(role.id))
|
|
|
|
return ids
|
|
|
|
|
|
|
|
def get_attachments_list(attachments):
|
|
|
|
attr = []
|
|
|
|
for attach in attachments:
|
|
|
|
a = {
|
|
|
|
"id": str(attach.id),
|
|
|
|
"size": attach.size,
|
|
|
|
"filename": attach.filename,
|
|
|
|
"url": attach.url,
|
|
|
|
"proxy_url": attach.proxy_url,
|
|
|
|
}
|
|
|
|
if attach.height:
|
|
|
|
a["height"] = attach.height
|
|
|
|
if attach.width:
|
|
|
|
a["width"] = attach.width
|
|
|
|
attr.append(a)
|
|
|
|
return attr
|
|
|
|
|
|
|
|
def get_embeds_list(embeds):
|
|
|
|
em = []
|
|
|
|
for e in embeds:
|
|
|
|
em.append(e.to_dict())
|
2018-07-27 10:57:54 +02:00
|
|
|
return em
|
|
|
|
|
|
|
|
def get_message_reactions(reactions):
|
|
|
|
reacts = []
|
|
|
|
for reaction in reactions:
|
|
|
|
reacts.append({
|
|
|
|
"emoji": get_partial_emoji(reaction.emoji),
|
|
|
|
"count": reaction.count
|
|
|
|
})
|
|
|
|
return reacts
|
|
|
|
|
|
|
|
def get_partial_emoji(emoji):
|
|
|
|
emote = {
|
|
|
|
"animated": False,
|
|
|
|
"id": None,
|
|
|
|
"name": str(emoji)
|
|
|
|
}
|
|
|
|
if isinstance(emoji, str):
|
|
|
|
return emote
|
|
|
|
emote["animated"] = emoji.animated
|
|
|
|
emote["id"] = str(emoji.id)
|
|
|
|
emote["name"] = emoji.name
|
|
|
|
return emote
|