mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 08:33:30 +01:00
Added DM support, DM Creation and DM Deletion
This commit is contained in:
@@ -2,7 +2,8 @@
|
||||
|
||||
const Constants = require('../../util/Constants');
|
||||
const Structure = name => require('../../structures/' + name);
|
||||
|
||||
const User = Structure('User');
|
||||
const GuildMember = Structure('GuildMember');
|
||||
const Message = Structure('Message');
|
||||
|
||||
class RESTMethods{
|
||||
@@ -39,13 +40,24 @@ class RESTMethods{
|
||||
|
||||
SendMessage(channel, content, tts, nonce) {
|
||||
return new Promise((resolve, reject) => {
|
||||
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);
|
||||
|
||||
if (channel instanceof User || channel instanceof GuildMember) {
|
||||
this.CreateDM(channel).then(chan => {
|
||||
channel = chan;
|
||||
req();
|
||||
})
|
||||
.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) {
|
||||
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)
|
||||
.then(data => {
|
||||
data.id = channel.id;
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const TextBasedChannel = require('./interface/TextBasedChannel');
|
||||
|
||||
class GuildMember {
|
||||
constructor(guild, data) {
|
||||
this.client = guild.client;
|
||||
@@ -56,6 +58,12 @@ class GuildMember {
|
||||
get id() {
|
||||
return this.user.id;
|
||||
}
|
||||
|
||||
deleteDM() {
|
||||
return this.client.rest.methods.DeleteChannel(this);
|
||||
}
|
||||
}
|
||||
|
||||
TextBasedChannel.applyToClass(GuildMember);
|
||||
|
||||
module.exports = GuildMember;
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const TextBasedChannel = require('./interface/TextBasedChannel');
|
||||
|
||||
class User {
|
||||
constructor(client, data) {
|
||||
this.client = client;
|
||||
@@ -22,6 +24,10 @@ class User {
|
||||
return `<@${this.id}>`;
|
||||
}
|
||||
|
||||
deleteDM() {
|
||||
return this.client.rest.methods.DeleteChannel(this);
|
||||
}
|
||||
|
||||
equals(user) {
|
||||
let base = (
|
||||
this.username === user.username &&
|
||||
@@ -45,4 +51,6 @@ class User {
|
||||
}
|
||||
}
|
||||
|
||||
TextBasedChannel.applyToClass(User);
|
||||
|
||||
module.exports = User;
|
||||
|
||||
@@ -11,10 +11,5 @@ function sendTTSMessage(content, options) {
|
||||
}
|
||||
|
||||
exports.applyToClass = structure => {
|
||||
if (structure.name !== 'TextChannel' && structure.name !== 'DMChannel') {
|
||||
throw new Error(structure + ' cannot implement TextBasedChannel');
|
||||
}
|
||||
|
||||
structure.prototype.sendMessage = sendMessage;
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user