Files
custom_discordjs/structures/guilds/Channel.js
T

121 lines
3.0 KiB
JavaScript
Raw Normal View History

import axios from 'axios';
import {message} from '../messages/message.js';
2023-04-07 20:35:03 -04:00
import { DataManager } from '../DataManager.js';
2023-04-07 20:35:03 -04:00
export class Channel extends DataManager {
/** @type {String} */
id;
/** @type {String} */
name;
/** @type {String} */
last_message_id;
/** @type {Number} */
type;
/** @type {Number} */
position;
/** @type {Number} */
flags;
/** @type {String} */
parent_id;
/** @type {import('../guilds/Guild.js').def} */
guild;
/** @type {[{id: String, type: String, allow: Number, deny: Number, allow_new: String, deny_nwe: String}]} */
permission_overwrites;
/** @type {Number} */
rate_limit_per_user;
/** @type {Boolean} */
nsfw;
async getChannelData() {
2023-04-07 20:12:16 -04:00
const response = await this.client.axiosCustom.get(`/channels/${this.id}`);
const channelData = response.data;
for (const k in this) {
if (channelData[k]) {
this[k] = channelData[k];
}
}
}
2023-04-07 20:12:16 -04:00
constructor(channel, guild, client = null) {
super(client);
2023-04-07 19:13:15 -04:00
for (const k in this) {
if (channel[k]) this[k] = channel[k];
}
this.guild = guild;
}
/**
* @param {Object} inp
* @returns {Promise<message>}
*/
async send(inp) {
return new Promise(async (resolve) => {
2023-04-08 16:33:58 -04:00
const content = (typeof inp == 'string') ? inp : inp.content;
var embds = undefined;
if (inp.embeds) {
embds = [];
for (const i of inp.embeds) {
embds.push(i.toJSON());
}
}
2023-04-08 16:33:58 -04:00
const toSend = (typeof inp == 'string') ? { content: inp } : structuredClone(inp);
toSend["embeds"] = embds;
toSend["message_reference"] = inp.message_reference || undefined;
const response = await this.client.axiosCustom.post(`/channels/${this.id}/messages`, toSend);
2023-04-07 20:12:16 -04:00
resolve(new message(response.data, this.client));
});
}
/**
* @returns {Promise<Map<String, message>}
* @param {{name: String, around: String, after: String, limit: Number}} configs
*/
async getMessages(configs) {
return new Promise(async (resolve) => {
var strconf = "?";
for (const i in configs) {
2023-04-07 20:12:16 -04:00
strconf += `${i}=${configs[i]}&`;
}
2023-04-07 20:12:16 -04:00
const response = await this.client.axiosCustom.get(`/channels/${this.id}/messages${strconf}`);
const msgMap = new Map();
for (const msgKey in response.data) {
2023-04-07 20:12:16 -04:00
const m = new message(response.data[msgKey], this.client, this.guild);
msgMap.set(m.id, m);
}
resolve(msgMap);
});
}
toObj() {
var obj = {};
for (const key in this) {
2023-04-07 20:35:03 -04:00
if (key != 'guild' && key != 'client') {
obj[key] = this[key];
}
}
return obj;
}
}