2022-03-19 10:37:45 +00:00
|
|
|
'use strict';
|
|
|
|
|
2022-04-16 10:44:43 +00:00
|
|
|
const { Collection } = require('@discordjs/collection');
|
2022-03-19 10:37:45 +00:00
|
|
|
const { Channel } = require('./Channel');
|
2022-04-16 10:44:43 +00:00
|
|
|
const Invite = require('./Invite');
|
|
|
|
const User = require('./User');
|
|
|
|
const TextBasedChannel = require('./interfaces/TextBasedChannel');
|
2022-03-19 10:37:45 +00:00
|
|
|
const { Error } = require('../errors');
|
2022-04-03 04:53:09 +00:00
|
|
|
const MessageManager = require('../managers/MessageManager');
|
|
|
|
const DataResolver = require('../util/DataResolver');
|
2022-03-19 10:37:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a Partial Group DM Channel on Discord.
|
|
|
|
* @extends {Channel}
|
|
|
|
*/
|
|
|
|
class PartialGroupDMChannel extends Channel {
|
|
|
|
constructor(client, data) {
|
|
|
|
super(client, data);
|
|
|
|
/**
|
|
|
|
* The name of this Group DM Channel
|
|
|
|
* @type {?string}
|
|
|
|
*/
|
|
|
|
this.name = data.name;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The hash of the channel icon
|
|
|
|
* @type {?string}
|
|
|
|
*/
|
|
|
|
this.icon = data.icon;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recipient data received in a {@link PartialGroupDMChannel}.
|
|
|
|
* @typedef {Object} PartialRecipient
|
|
|
|
* @property {string} username The username of the recipient
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The recipients of this Group DM Channel.
|
|
|
|
* @type {PartialRecipient[]}
|
|
|
|
*/
|
2022-04-03 04:53:09 +00:00
|
|
|
this.recipients = new Collection();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Messages data
|
|
|
|
* @type {Collection}
|
|
|
|
*/
|
|
|
|
this.messages = new MessageManager(this);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Last Message ID
|
|
|
|
* @type {?snowflake<Message.id>}
|
|
|
|
*/
|
|
|
|
this.lastMessageId = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Last Pin Timestamp
|
|
|
|
* @type {UnixTimestamp}
|
|
|
|
*/
|
|
|
|
this.lastPinTimestamp = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The owner of this Group DM Channel
|
|
|
|
* @type {?User}
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
this.owner = client.users.cache.get(data.owner_id);
|
|
|
|
this.ownerId = data.owner_id;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invites fetch
|
|
|
|
*/
|
|
|
|
this.invites = new Collection();
|
|
|
|
|
|
|
|
this._setup(client, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-16 10:44:43 +00:00
|
|
|
*
|
2022-04-16 12:01:05 +00:00
|
|
|
* @param {Discord.Client} client Discord Bot Client
|
|
|
|
* @param {Object} data Channel Data
|
2022-04-03 04:53:09 +00:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_setup(client, data) {
|
|
|
|
if ('recipients' in data) {
|
|
|
|
Promise.all(
|
2022-04-16 12:01:05 +00:00
|
|
|
data.recipients.map(recipient =>
|
|
|
|
this.recipients.set(recipient.id, client.users.cache.get(data.owner_id) || recipient),
|
|
|
|
),
|
2022-04-03 04:53:09 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if ('last_pin_timestamp' in data) {
|
|
|
|
const date = new Date(data.last_pin_timestamp);
|
|
|
|
this.lastPinTimestamp = date.getTime();
|
|
|
|
}
|
|
|
|
if ('last_message_id' in data) {
|
|
|
|
this.lastMessageId = data.last_message_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-16 10:44:43 +00:00
|
|
|
*
|
2022-04-03 04:53:09 +00:00
|
|
|
* @param {Object} data name, icon
|
2022-04-16 12:01:05 +00:00
|
|
|
* @returns {any} any data .-.
|
2022-04-03 04:53:09 +00:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
async edit(data) {
|
|
|
|
const _data = {};
|
|
|
|
if ('name' in data) _data.name = data.name?.trim() ?? null;
|
2022-04-16 10:44:43 +00:00
|
|
|
if (typeof data.icon !== 'undefined') {
|
2022-04-03 04:53:09 +00:00
|
|
|
_data.icon = await DataResolver.resolveImage(data.icon);
|
2022-04-16 10:44:43 +00:00
|
|
|
}
|
2022-04-03 04:53:09 +00:00
|
|
|
const newData = await this.client.api.channels(this.id).patch({
|
|
|
|
data: _data,
|
|
|
|
});
|
|
|
|
|
|
|
|
return this.client.actions.ChannelUpdate.handle(newData).updated;
|
2022-03-19 10:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The URL to this channel's icon.
|
2022-03-24 10:55:32 +00:00
|
|
|
* @param {StaticImageURLOptions} [options={}] Options for the Image URL
|
2022-03-19 10:37:45 +00:00
|
|
|
* @returns {?string}
|
|
|
|
*/
|
2022-03-24 10:55:32 +00:00
|
|
|
iconURL({ format, size } = {}) {
|
2022-04-16 10:44:43 +00:00
|
|
|
return this.icon && this.client.rest.cdn.GDMIcon(this.id, this.icon, format, size);
|
2022-03-19 10:37:45 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 04:53:09 +00:00
|
|
|
async addMember(user) {
|
2022-04-16 10:44:43 +00:00
|
|
|
if (this.ownerId !== this.client.user.id) {
|
2022-04-03 04:53:09 +00:00
|
|
|
return Promise.reject(new Error('NOT_OWNER_GROUP_DM_CHANNEL'));
|
2022-04-16 10:44:43 +00:00
|
|
|
}
|
2022-04-16 12:01:05 +00:00
|
|
|
if (!(user instanceof User)) {
|
2022-04-16 10:44:43 +00:00
|
|
|
return Promise.reject(new TypeError('User is not an instance of Discord.User'));
|
|
|
|
}
|
2022-04-03 04:53:09 +00:00
|
|
|
if (this.recipients.get(user.id)) return Promise.reject(new Error('USER_ALREADY_IN_GROUP_DM_CHANNEL'));
|
|
|
|
//
|
|
|
|
await this.client.api.channels[this.id].recipients[user.id].put();
|
|
|
|
this.recipients.set(user.id, user);
|
|
|
|
return this;
|
2022-03-19 10:37:45 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 04:53:09 +00:00
|
|
|
async removeMember(user) {
|
2022-04-16 10:44:43 +00:00
|
|
|
if (this.ownerId !== this.client.user.id) {
|
2022-04-03 04:53:09 +00:00
|
|
|
return Promise.reject(new Error('NOT_OWNER_GROUP_DM_CHANNEL'));
|
2022-04-16 10:44:43 +00:00
|
|
|
}
|
2022-04-16 12:01:05 +00:00
|
|
|
if (!(user instanceof User)) {
|
2022-04-16 10:44:43 +00:00
|
|
|
return Promise.reject(new TypeError('User is not an instance of Discord.User'));
|
|
|
|
}
|
2022-04-03 04:53:09 +00:00
|
|
|
if (!this.recipients.get(user.id)) return Promise.reject(new Error('USER_NOT_IN_GROUP_DM_CHANNEL'));
|
|
|
|
await this.client.api.channels[this.id].recipients[user.id].delete();
|
|
|
|
this.recipients.delete(user.id);
|
|
|
|
return this;
|
2022-03-19 10:37:45 +00:00
|
|
|
}
|
2022-04-03 04:53:09 +00:00
|
|
|
|
|
|
|
setName(name) {
|
|
|
|
return this.edit({ name });
|
|
|
|
}
|
|
|
|
|
|
|
|
setIcon(icon) {
|
|
|
|
return this.edit({ icon });
|
|
|
|
}
|
|
|
|
|
|
|
|
async getInvite() {
|
|
|
|
const inviteCode = await this.client.api.channels(this.id).invites.post({
|
2022-04-16 10:44:43 +00:00
|
|
|
data: {
|
|
|
|
max_age: 86400,
|
|
|
|
},
|
|
|
|
});
|
2022-04-03 04:53:09 +00:00
|
|
|
const invite = new Invite(this.client, inviteCode);
|
|
|
|
this.invites.set(invite.code, invite);
|
|
|
|
return invite;
|
|
|
|
}
|
|
|
|
|
|
|
|
async fetchInvite(force = false) {
|
2022-04-16 10:44:43 +00:00
|
|
|
if (this.ownerId !== this.client.user.id) {
|
2022-04-03 04:53:09 +00:00
|
|
|
return Promise.reject(new Error('NOT_OWNER_GROUP_DM_CHANNEL'));
|
2022-04-16 10:44:43 +00:00
|
|
|
}
|
2022-04-03 04:53:09 +00:00
|
|
|
if (!force && this.invites.size) return this.invites;
|
|
|
|
const invites = await this.client.api.channels(this.id).invites.get();
|
|
|
|
await Promise.all(invites.map(invite => this.invites.set(invite.code, new Invite(this.client, invite))));
|
|
|
|
return this.invites;
|
|
|
|
}
|
|
|
|
|
|
|
|
async removeInvite(invite) {
|
2022-04-16 10:44:43 +00:00
|
|
|
if (this.ownerId !== this.client.user.id) {
|
2022-04-03 04:53:09 +00:00
|
|
|
return Promise.reject(new Error('NOT_OWNER_GROUP_DM_CHANNEL'));
|
2022-04-16 10:44:43 +00:00
|
|
|
}
|
2022-04-16 12:01:05 +00:00
|
|
|
if (!(invite instanceof Invite)) {
|
2022-04-03 04:53:09 +00:00
|
|
|
return Promise.reject(new TypeError('Invite is not an instance of Discord.Invite'));
|
2022-04-16 10:44:43 +00:00
|
|
|
}
|
2022-04-03 04:53:09 +00:00
|
|
|
await this.client.api.channels(this.id).invites[invite.code].delete();
|
|
|
|
this.invites.delete(invite.code);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// These are here only for documentation purposes - they are implemented by TextBasedChannel
|
|
|
|
/* eslint-disable no-empty-function */
|
2022-04-16 10:44:43 +00:00
|
|
|
get lastMessage() {}
|
|
|
|
get lastPinAt() {}
|
|
|
|
send() {}
|
|
|
|
sendTyping() {}
|
2022-03-19 10:37:45 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 04:53:09 +00:00
|
|
|
TextBasedChannel.applyToClass(PartialGroupDMChannel, false);
|
|
|
|
|
2022-03-19 10:37:45 +00:00
|
|
|
module.exports = PartialGroupDMChannel;
|