From 1a048a01db38c90a6a4fb07781476a8fcc87c8eb Mon Sep 17 00:00:00 2001 From: ION606 Date: Tue, 4 Apr 2023 22:06:12 -0400 Subject: [PATCH] Added channels, fixed interactions and added guids --- structures/client/client.js | 7 +- structures/guilds/Channel.js | 137 +++++++++++++++++++++++ structures/guilds/GuildChannelManager.js | 57 +++++++++- structures/guilds/guild.js | 2 +- structures/guilds/guildInvite.js | 2 +- structures/interactions/interaction.js | 29 ++++- structures/messages/message.js | 108 +----------------- tests/guildTests.js | 6 +- tests/interactionTests.js | 1 + tests/messageTests.js | 10 +- 10 files changed, 240 insertions(+), 119 deletions(-) create mode 100644 structures/guilds/Channel.js diff --git a/structures/client/client.js b/structures/client/client.js index f1227a6..2362ab7 100644 --- a/structures/client/client.js +++ b/structures/client/client.js @@ -149,7 +149,7 @@ export class Client extends EventEmitter { this.user_profile = response.profile; this.user_settings = response.config; this.id = response.profile.id; - console.log(response.guilds); + // console.log(response.guilds); this.ready(); } else if (response.t == gateWayEvents.MessageCreate) { @@ -171,9 +171,10 @@ export class Client extends EventEmitter { else if (response.t == gateWayEvents.GuildCreate) this.guildCreate(response.guild); else if (response.t == gateWayEvents.GuildDelete) this.guildDelete(response.guild); else if (response.t == gateWayEvents.GuildMemberAdd) this.guildMemberAdd(response.member); - else console.log(response.t); + // else console.log(response.t); } else { - console.log(response.t); + // commmented to avoid heartbeats + // console.log(response.t); } }); diff --git a/structures/guilds/Channel.js b/structures/guilds/Channel.js new file mode 100644 index 0000000..ae5116d --- /dev/null +++ b/structures/guilds/Channel.js @@ -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} + */ + 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} + * @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; + } +} \ No newline at end of file diff --git a/structures/guilds/GuildChannelManager.js b/structures/guilds/GuildChannelManager.js index c6bc017..fd11ada 100644 --- a/structures/guilds/GuildChannelManager.js +++ b/structures/guilds/GuildChannelManager.js @@ -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} + */ + 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} + * @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; diff --git a/structures/guilds/guild.js b/structures/guilds/guild.js index 0d50253..7b6bd2d 100644 --- a/structures/guilds/guild.js +++ b/structures/guilds/guild.js @@ -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 diff --git a/structures/guilds/guildInvite.js b/structures/guilds/guildInvite.js index cda53f1..c6516c4 100644 --- a/structures/guilds/guildInvite.js +++ b/structures/guilds/guildInvite.js @@ -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'; diff --git a/structures/interactions/interaction.js b/structures/interactions/interaction.js index b3cc91b..a66e0f4 100644 --- a/structures/interactions/interaction.js +++ b/structures/interactions/interaction.js @@ -1,9 +1,29 @@ import axios from 'axios'; import author from '../messages/User.js'; -import { Channel, message } from '../messages/message.js'; +import { message } from '../messages/message.js'; +import { Channel } from '../guilds/Channel.js'; import {Embed} from '../messages/embed.js'; import Guild from '../guilds/Guild.js'; +class interactionOptions { + /** @type {String} */ + name; + + /** @type {Number} */ + type; + + /** @type {[{value: any, type: Number, name: String}]} */ + options; + + /** @type {Boolean} */ + focused; + + constructor(o) { + for (const k in this) { + if (o[k]) this[k] = o[k]; + } + } +} export class Interaction { /** @type {author} */ @@ -36,6 +56,9 @@ export class Interaction { /** @type {String} */ guild_id; + /** @type {interactionOptions} */ + data; + /** * @param {{content: String, ephemeral?: Boolean, embeds: [Embed]} | String} inp @@ -173,7 +196,9 @@ export class Interaction { for (const k in this) { if (intRaw[k] != undefined) { if (k == "user") this[k] = new author(intRaw[k]); - else { + else if (k == 'data') { + this.data = new interactionOptions(intRaw[k]); + } else { if (k == 'channel_id') { this.channel = new Channel(intRaw[k], this.guild, this.#application.token); } diff --git a/structures/messages/message.js b/structures/messages/message.js index c0cf484..728eb16 100644 --- a/structures/messages/message.js +++ b/structures/messages/message.js @@ -1,112 +1,6 @@ import author from './User.js'; import axios from 'axios'; - - -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} - */ - 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)); - }); - } - - toObj() { - var obj = {}; - for (const key in this) { - if (key != '#token' && key != 'guild') { - obj[key] = this[key]; - } - } - - return obj; - } -} +import { Channel } from '../guilds/Channel.js'; export class message { diff --git a/tests/guildTests.js b/tests/guildTests.js index 2508760..b655dd5 100644 --- a/tests/guildTests.js +++ b/tests/guildTests.js @@ -1,6 +1,5 @@ import Guild from "../structures/guilds/Guild.js"; import { guildRole, newGuildRoleObj } from "../structures/guilds/guildRoles.js"; -import { Channel } from "../structures/messages/message.js"; import { Client } from "../structures/types.js"; const delay = ms => new Promise(resolve => setTimeout(resolve, ms)); @@ -43,4 +42,9 @@ export default async function temp(c) { } const newChannel = await guild.channels.create({name: "temptemp"}); + await delay(1000); + + guild.channels.edit(newChannel.id, {name: "NEW-NAME!"}); + // const delConf = await guild.channels.delete(newChannel.id); + // console.log(delConf); } \ No newline at end of file diff --git a/tests/interactionTests.js b/tests/interactionTests.js index 063c917..ae5941d 100644 --- a/tests/interactionTests.js +++ b/tests/interactionTests.js @@ -3,6 +3,7 @@ const delay = ms => new Promise(resolve => setTimeout(resolve, ms)); /** @param {Interaction} interaction */ export default async (interaction) => { + console.log(interaction.data); interaction.reply({content: "HELLO WORLD", ephemeral: true}); await delay(3000); interaction.update({content: "NOOOOOOOOOOOOOOOOOO"}); diff --git a/tests/messageTests.js b/tests/messageTests.js index a0ce796..a109257 100644 --- a/tests/messageTests.js +++ b/tests/messageTests.js @@ -10,15 +10,19 @@ export default async (message) => { .setDescription("dkjhfslkjdfhjldsjhfkzdjhflkdsjhfdsjhfkdsjf"); const response = await message.channel.send({ content: "FDJHKSJDFHLKJDSHFLKJSDHFKDSJHFD", embeds: [embd] }); - console.log(response); + // console.log(response); await delay(2000); const response2 = await message.reply({content: `You said "${message.content}"!`, embeds: [embd]}); - console.log(response2); + // console.log(response2); await delay(2000); const response3 = await response.edit("KAT"); - console.log(response3); + // console.log(response3); + + //Get channel messages + const messages = await response.channel.getMessages(); + console.log(messages); } else { console.log(message); }