Added channels, fixed interactions and added guids

This commit is contained in:
ION606
2023-04-04 22:06:12 -04:00
parent 658b45feee
commit 1a048a01db
10 changed files with 240 additions and 119 deletions
+137
View File
@@ -0,0 +1,137 @@
import axios from 'axios';
import {message} from '../messages/message.js';
export class Channel {
/** @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;
/** @type {String} */
#token;
async getChannelData() {
const headers = {
Authorization: this.#token
}
const response = await axios.get(`https://discord.com/api/channels/${this.id}`, { headers });
const channelData = response.data;
for (const k in this) {
if (channelData[k]) {
this[k] = channelData[k];
}
}
}
constructor(channel, guild, token = null) {
this.#token = token;
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) => {
const toSend = (typeof inp == 'string') ? inp : inp.content;
const config = {
headers: {
Authorization: this.#token
}
}
var embds = undefined;
if (inp.embeds) {
embds = [];
for (const i of inp.embeds) {
embds.push(i.toJSON());
}
}
const response = await axios.post(`https://discord.com/api/channels/${this.id}/messages`, {
content: toSend,
message_reference: inp.message_reference || undefined,
embeds: embds
}, config);
resolve(new message(response.data, this.#token));
});
}
/**
* @returns {Promise<Map<String, message>}
* @param {{name: String, around: String, after: String, limit: Number}} configs
*/
async getMessages(configs) {
return new Promise(async (resolve) => {
const config = {
headers: {
Authorization: this.#token
}
}
var strconf = "?";
for (const i in configs) {
console.log(i);
}
const response = await axios.get(`https://discord.com/api/channels/${this.id}/messages`, config);
const msgMap = new Map();
for (const msgKey in response.data) {
const m = new message(response.data[msgKey], this.#token, this.guild);
msgMap.set(m.id, m);
}
resolve(msgMap);
});
}
toObj() {
var obj = {};
for (const key in this) {
if (key != '#token' && key != 'guild') {
obj[key] = this[key];
}
}
return obj;
}
}
+56 -1
View File
@@ -1,5 +1,5 @@
import axios from 'axios';
import {Channel} from '../messages/message.js';
import { Channel } from './Channel.js';
import Guild from './Guild.js';
export class GuildChannelManager {
@@ -37,6 +37,61 @@ export class GuildChannelManager {
}
/**
* @description returns the deleted channel if successful
* @param {String} cid
* @param {String} reason
* @returns {promise<Channel>}
*/
async delete(cid, reason=null) {
return new Promise(async (resolve) => {
try {
if (!this.cache.has(cid)) throw "This channel does not exist!";
const config = {
headers: {
Authorization: this.#token
}
}
const response = await axios.delete(`https://discord.com/api/channels/${cid}`, config);
const newChannel = new Channel(response.data, this.guild, this.#token);
this.cache.delete(cid);
resolve(newChannel);
} catch (err) {
throw err;
}
});
}
/**
* @returns {Promise<Channel>}
* @param {String} cid
* @param {{name: String, Type: String, position: Number, topic: String, nsfw: Boolean, userLimit : Number, }} opts
*/
async edit(cid, opts) {
return new Promise(async (resolve) => {
try {
if (!this.cache.has(cid)) throw "This channel does not exist!";
if (Object.keys(opts).length == 0) return resolve(this);
const config = {
headers: {
Authorization: this.#token
}
}
const response = await axios.patch(`https://discord.com/api/channels/${cid}`, opts, config);
const newChannel = new Channel(response.data, this.guild, this.#token);
this.cache.set(newChannel.id, newChannel);
resolve(newChannel);
} catch (err) {
throw err;
}
});
}
constructor(token, guild, newCache) {
this.#token = token;
this.guild = guild;
+1 -1
View File
@@ -5,7 +5,7 @@ import GuildEmoji from './guildEmoji.js';
import guildInvite from './guildInvite.js';
import { guildSticker, guildStickerManager } from './GuildStickers.js';
import { GuildChannelManager } from './GuildChannelManager.js';
import { Channel } from '../messages/message.js';
import { Channel } from './Channel.js';
//See https://discord.com/developers/docs/resources/guild
+1 -1
View File
@@ -1,6 +1,6 @@
import author from '../messages/User.js';
import Guild from './Guild.js'
import { Channel } from '../messages/message.js';
import { Channel } from './Channel.js';
import axios from 'axios';