Separate messages into their types

This commit is contained in:
Samuel Elliott 2018-04-14 19:13:26 +01:00
parent 6d64d17b7d
commit a1c6f5d5fd
No known key found for this signature in database
GPG Key ID: 8420C7CDE43DC4D6
5 changed files with 143 additions and 46 deletions

View File

@ -69,7 +69,7 @@ export default class DiscordApi {
*/
static get guilds() {
const guilds = Modules.GuildStore.getGuilds();
return List.from(Object.entries(guilds), ([i, g]) => new Guild(g));
return List.from(Object.entries(guilds), ([i, g]) => Guild.from(g));
}
/**
@ -85,7 +85,7 @@ export default class DiscordApi {
*/
static get users() {
const users = Modules.UserStore.getUsers();
return List.from(Object.entries(users), ([i, u]) => new User(u));
return List.from(Object.entries(users), ([i, u]) => User.from(u));
}
/**
@ -100,7 +100,7 @@ export default class DiscordApi {
*/
static get sortedGuilds() {
const guilds = Modules.SortedGuildStore.getSortedGuilds();
return List.from(guilds, g => new Guild(g));
return List.from(guilds, g => Guild.from(g));
}
/**
@ -115,7 +115,7 @@ export default class DiscordApi {
*/
static get currentGuild() {
const guild = Modules.GuildStore.getGuild(Modules.SelectedGuildStore.getGuildId());
if (guild) return new Guild(guild);
if (guild) return Guild.from(guild);
}
/**
@ -131,7 +131,7 @@ export default class DiscordApi {
*/
static get currentUser() {
const user = Modules.UserStore.getCurrentUser();
if (user) return new User(user);
if (user) return User.from(user);
}
/**

View File

@ -65,7 +65,7 @@ export class Channel {
let response = {};
if (parse) response = await Modules.MessageActions._sendMessage(this.id, Modules.MessageParser.parse(this.discordObject, content));
else response = await Modules.MessageActions._sendMessage(this.id, {content});
return new Message(Modules.MessageStore.getMessage(this.id, response.body.id));
return Message.from(Modules.MessageStore.getMessage(this.id, response.body.id));
}
/**
@ -73,7 +73,7 @@ export class Channel {
*/
get messages() {
const messages = Modules.MessageStore.getMessages(this.id).toArray();
return List.from(messages, m => new Message(m));
return List.from(messages, m => Message.from(m));
}
/**
@ -97,7 +97,7 @@ export class Channel {
async sendInvite(code) {
if (this.assertPermissions) this.assertPermissions('SEND_MESSAGES', Modules.DiscordPermissions.VIEW_CHANNEL | Modules.DiscordPermissions.SEND_MESSAGES);
const response = Modules.MessageActions.sendInvite(this.id, code);
return new Message(Modules.MessageStore.getMessage(this.id, response.body.id));
return Message.from(Modules.MessageStore.getMessage(this.id, response.body.id));
}
/**
@ -196,7 +196,7 @@ export class GuildChannel extends Channel {
get guild() {
const guild = Modules.GuildStore.getGuild(this.guild_id);
if (guild) return new Guild(guild);
if (guild) return Guild.from(guild);
}
get default_channel() {

View File

@ -81,9 +81,13 @@ export class Guild {
this.discordObject = data;
}
static from(data) {
return new Guild(data);
}
static fromId(id) {
const guild = Modules.GuildStore.getGuild(id);
if (guild) return new Guild(guild);
if (guild) return Guild.from(guild);
}
static get Role() { return Role }

View File

@ -97,38 +97,43 @@ export class Message {
this.discordObject = data;
}
static from(data) {
switch (data.type) {
default: return new Message(data);
case 0: return new DefaultMessage(data);
case 1: return new RecipientAddMessage(data);
case 2: return new RecipientRemoveMessage(data);
case 3: return new CallMessage(data);
case 4: return new GroupChannelNameChangeMessage(data);
case 5: return new GroupChannelIconChangeMessage(data);
case 6: return new MessagePinnedMessage(data);
case 7: return new GuildMemberJoinMessage(data);
}
}
static get DefaultMessage() { return DefaultMessage }
static get RecipientAddMessage() { return RecipientAddMessage }
static get RecipientRemoveMessage() { return RecipientRemoveMessage }
static get CallMessage() { return CallMessage }
static get GroupChannelNameChangeMessage() { return GroupChannelNameChangeMessage }
static get GroupChannelIconChangeMessage() { return GroupChannelIconChangeMessage }
static get MessagePinnedMessage() { return MessagePinnedMessage }
static get GuildMemberJoinMessage() { return GuildMemberJoinMessage }
static get Reaction() { return Reaction }
static get Embed() { return Embed }
get id() { return this.discordObject.id }
get channel_id() { return this.discordObject.channel_id }
get webhook_id() { return this.discordObject.webhookId }
get nonce() { return this.discordObject.nonce }
get type() { return this.discordObject.type }
get content() { return this.discordObject.content }
get content_parsed() { return this.discordObject.contentParsed }
get invite_codes() { return this.discordObject.invites }
get attachments() { return this.discordObject.attachments }
get mentions() { return this.discordObject.mentions }
get mention_roles() { return this.discordObject.mentionRoles }
get mention_everyone() { return this.discordObject.mentionEveryone }
get timestamp() { return this.discordObject.timestamp }
get edited_timestamp() { return this.discordObject.editedTimestamp }
get state() { return this.discordObject.state }
get tts() { return this.discordObject.tts }
get mentioned() { return this.discordObject.mentioned }
get bot() { return this.discordObject.bot }
get blocked() { return this.discordObject.blocked }
get pinned() { return this.discordObject.pinned }
get nick() { return this.discordObject.nick }
get colour_string() { return this.discordObject.colorString }
get application() { return this.discordObject.application }
get activity() { return this.discordObject.activity }
get call() { return this.discordObject.call }
get author() {
if (this.discordObject.author)
return new User(this.discordObject.author);
if (this.webhook_id) return this.discordObject.author;
if (this.discordObject.author) return User.from(this.discordObject.author);
}
get channel() {
@ -139,6 +144,51 @@ export class Message {
if (this.channel) return this.channel.guild;
}
/**
* Deletes the message.
* TODO: how do we know if the message was deleted successfully?
*/
delete() {
Modules.MessageActions.deleteMessage(this.channel_id, this.id);
}
/**
* Jumps to the message.
*/
jumpTo(flash = true) {
Modules.MessageActions.jumpToMessage(this.channel_id, this.id, flash);
}
}
export class DefaultMessage extends Message {
get webhook_id() { return this.discordObject.webhookId }
get type() { return 'DEFAULT' }
get content() { return this.discordObject.content }
get content_parsed() { return this.discordObject.contentParsed }
get invite_codes() { return this.discordObject.invites }
// get embeds() { return this.discordObject.embeds }
get attachments() { return this.discordObject.attachments }
get mention_ids() { return this.discordObject.mentions }
get mention_role_ids() { return this.discordObject.mentionRoles }
get mention_everyone() { return this.discordObject.mentionEveryone }
get edited_timestamp() { return this.discordObject.editedTimestamp }
get tts() { return this.discordObject.tts }
get mentioned() { return this.discordObject.mentioned }
get bot() { return this.discordObject.bot }
get blocked() { return this.discordObject.blocked }
get pinned() { return this.discordObject.pinned }
get activity() { return this.discordObject.activity }
get application() { return this.discordObject.application }
get mentions() {
return List.from(this.mention_ids, id => User.fromId(id));
}
get mention_roles() {
return List.from(this.mention_role_ids, id => this.guild.roles.find(r => r.id === id));
}
get embeds() {
return List.from(this.discordObject.embeds, r => new Embed(r, this.id, this.channel_id));
}
@ -179,20 +229,59 @@ export class Message {
endEdit() {
Modules.MessageActions.endEditMessage();
}
/**
* Deletes the message.
* TODO: how do we know if the message was deleted successfully?
*/
delete() {
Modules.MessageActions.deleteMessage(this.channel_id, this.id);
}
/**
* Jumps to the message.
*/
jumpTo(flash = true) {
Modules.MessageActions.jumpToMessage(this.channel_id, this.id, flash);
}
}
export class RecipientAddMessage extends Message {
get type() { return 'RECIPIENT_ADD' }
get added_user_id() { return this.discordObject.mentions[0] }
get added_user() {
return User.fromId(this.added_user_id);
}
}
export class RecipientRemoveMessage extends Message {
get type() { return 'RECIPIENT_REMOVE' }
get removed_user_id() { return this.discordObject.mentions[0] }
get removed_user() {
return User.fromId(this.removed_user_id);
}
get user_left() {
return this.author === this.removed_user;
}
}
export class CallMessage extends Message {
get type() { return 'CALL' }
get mention_ids() { return this.discordObject.mentions }
get call() { return this.discordObject.call }
get ended_timestamp() { return this.call.endedTimestamp }
get mentions() {
return List.from(this.mention_ids, id => User.fromId(id));
}
get participants() {
return List.from(this.call.participants, id => User.fromId(id));
}
}
export class GroupChannelNameChangeMessage extends Message {
get type() { return 'CHANNEL_NAME_CHANGE' }
get new_name() { return this.discordObject.content }
}
export class GroupChannelIconChangeMessage extends Message {
get type() { return 'CHANNEL_ICON_CHANGE' }
}
export class MessagePinnedMessage extends Message {
get type() { return 'CHANNEL_PINNED_MESSAGE' }
}
export class GuildMemberJoinMessage extends Message {
get type() { return 'GUILD_MEMBER_JOIN' }
}

View File

@ -24,9 +24,13 @@ export class User {
this.discordObject = data;
}
static from(data) {
return new User(data);
}
static fromId(id) {
const user = Modules.UserStore.getUser(id);
if (user) return new User(user);
if (user) return User.from(user);
}
get id() { return this.discordObject.id }