Added DM support, DM Creation and DM Deletion

This commit is contained in:
hydrabolt
2016-05-03 17:50:33 +01:00
parent ab17375248
commit bc443df11d
5 changed files with 75 additions and 14 deletions

View File

@@ -2,7 +2,8 @@
const Constants = require('../../util/Constants'); const Constants = require('../../util/Constants');
const Structure = name => require('../../structures/' + name); const Structure = name => require('../../structures/' + name);
const User = Structure('User');
const GuildMember = Structure('GuildMember');
const Message = Structure('Message'); const Message = Structure('Message');
class RESTMethods{ class RESTMethods{
@@ -39,13 +40,24 @@ class RESTMethods{
SendMessage(channel, content, tts, nonce) { SendMessage(channel, content, tts, nonce) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.rest.makeRequest('post', Constants.Endpoints.CHANNEL_MESSAGES(channel.id), true, {
content, tts, nonce, if (channel instanceof User || channel instanceof GuildMember) {
}) this.CreateDM(channel).then(chan => {
.then(data => { channel = chan;
resolve(this.rest.client.actions.MessageCreate.handle(data).m); req();
}) })
.catch(reject); .catch(reject);
}
var _this = this;
function req() {
_this.rest.makeRequest('post', Constants.Endpoints.CHANNEL_MESSAGES(channel.id), true, {
content, tts, nonce,
})
.then(data => resolve(_this.rest.client.actions.MessageCreate.handle(data).m))
.catch(reject);
}
}); });
} }
@@ -87,8 +99,37 @@ class RESTMethods{
}); });
} }
GetExistingDM(recipient) {
let dmChannel = this.rest.client.store.getAsArray('channels')
.filter(channel => channel.recipient)
.filter(channel => channel.recipient.id === recipient.id);
return dmChannel[0];
}
CreateDM(recipient) {
return new Promise((resolve, reject) => {
let dmChannel = this.GetExistingDM(recipient);
if (dmChannel) {
return resolve(dmChannel);
}
this.rest.makeRequest('post', Constants.Endpoints.USER_CHANNELS(this.rest.client.store.user.id), true, {
recipient_id: recipient.id,
})
.then(data => resolve(this.rest.client.actions.ChannelCreate.handle(data).channel))
.catch(reject);
});
}
DeleteChannel(channel) { DeleteChannel(channel) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (channel instanceof User || channel instanceof GuildMember) {
channel = this.GetExistingDM(channel);
}
this.rest.makeRequest('del', Constants.Endpoints.CHANNEL(channel.id), true) this.rest.makeRequest('del', Constants.Endpoints.CHANNEL(channel.id), true)
.then(data => { .then(data => {
data.id = channel.id; data.id = channel.id;

View File

@@ -1,5 +1,7 @@
'use strict'; 'use strict';
const TextBasedChannel = require('./interface/TextBasedChannel');
class GuildMember { class GuildMember {
constructor(guild, data) { constructor(guild, data) {
this.client = guild.client; this.client = guild.client;
@@ -56,6 +58,12 @@ class GuildMember {
get id() { get id() {
return this.user.id; return this.user.id;
} }
deleteDM() {
return this.client.rest.methods.DeleteChannel(this);
}
} }
TextBasedChannel.applyToClass(GuildMember);
module.exports = GuildMember; module.exports = GuildMember;

View File

@@ -1,5 +1,7 @@
'use strict'; 'use strict';
const TextBasedChannel = require('./interface/TextBasedChannel');
class User { class User {
constructor(client, data) { constructor(client, data) {
this.client = client; this.client = client;
@@ -22,6 +24,10 @@ class User {
return `<@${this.id}>`; return `<@${this.id}>`;
} }
deleteDM() {
return this.client.rest.methods.DeleteChannel(this);
}
equals(user) { equals(user) {
let base = ( let base = (
this.username === user.username && this.username === user.username &&
@@ -45,4 +51,6 @@ class User {
} }
} }
TextBasedChannel.applyToClass(User);
module.exports = User; module.exports = User;

View File

@@ -11,10 +11,5 @@ function sendTTSMessage(content, options) {
} }
exports.applyToClass = structure => { exports.applyToClass = structure => {
if (structure.name !== 'TextChannel' && structure.name !== 'DMChannel') {
throw new Error(structure + ' cannot implement TextBasedChannel');
}
structure.prototype.sendMessage = sendMessage; structure.prototype.sendMessage = sendMessage;
}; };

View File

@@ -70,7 +70,7 @@ client.on('typingStop.', (channel, user, data) => {
}); });
client.on('message', message => { client.on('message', message => {
if (message.author.username === 'hydrabolt') { if (true) {
if (message.content === 'makechann') { if (message.content === 'makechann') {
if (message.channel.guild) { if (message.channel.guild) {
message.channel.guild.createChannel('hi', 'text').then(console.log); message.channel.guild.createChannel('hi', 'text').then(console.log);
@@ -114,6 +114,15 @@ client.on('message', message => {
m += `I am aware of ${message.guild.members.length} members`; m += `I am aware of ${message.guild.members.length} members`;
message.channel.sendMessage(m); message.channel.sendMessage(m);
} }
if (message.content === 'messageme!') {
message.author.sendMessage('oh, hi there!').catch(e => console.log(e.stack));
}
if (message.content === 'don\'t dm me') {
message.author.deleteDM();
}
} }
}); });