Changed class format to BaseClass

This commit is contained in:
ION606
2023-04-07 20:12:16 -04:00
parent e6b32cc115
commit b6d35c8e55
11 changed files with 97 additions and 227 deletions
+2 -2
View File
@@ -148,7 +148,7 @@ export class Client extends EventEmitter {
if (!isUser) token = "Bot " + token;
this.axiosCustom = axios.create({
baseURL: "https://discord.com/api/",
headers: { Authorization: this.#token }
headers: { Authorization: token }
});
return new Promise((resolve, reject) => {
@@ -180,7 +180,7 @@ export class Client extends EventEmitter {
this.ws.on('message', async (msg) => {
const data = JSON.parse(msg.toString());
const response = await handleResponses(data, token, this.id);
const response = await handleResponses(data, token, this);
if (response.op == 10) { return this.#startHeartBeat(response.heartBeat, token); }
+4 -4
View File
@@ -9,7 +9,7 @@ import Guild from '../guilds/Guild.js';
* @param {Object} msg
* @returns {Promise<Boolean>}
*/
export default async function handleEvents(msgObj, token, id) {
export default async function handleEvents(msgObj, token, client) {
return new Promise((resolve, reject) => {
const op = msgObj["op"];
const t = msgObj["t"];
@@ -23,16 +23,16 @@ export default async function handleEvents(msgObj, token, id) {
break;
case gateWayEvents.MessageCreate:
const msg = new message(msgObj["d"], token);
const msg = new message(msgObj["d"], client);
resolve({op: op, t: t, message: msg});
break;
case gateWayEvents.InteractionCreate:
resolve({op: op, t: t, interaction: new Interaction(msgObj["d"], token, id)});
resolve({op: op, t: t, interaction: new Interaction(msgObj["d"], client)});
break;
case gateWayEvents.GuildCreate:
resolve({op: op, t: t, guild: new Guild(msgObj["d"], token)});
resolve({op: op, t: t, guild: new Guild(msgObj["d"], client)});
break;
case gateWayEvents.ThreadCreate:
+10 -29
View File
@@ -36,16 +36,9 @@ export class Channel extends BaseStruct {
/** @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 response = await this.client.axiosCustom.get(`/channels/${this.id}`);
const channelData = response.data;
for (const k in this) {
@@ -56,10 +49,9 @@ export class Channel extends BaseStruct {
}
constructor(channel, guild, token = null) {
super();
constructor(channel, guild, client = null) {
super(client);
this.#token = token;
for (const k in this) {
if (channel[k]) this[k] = channel[k];
}
@@ -74,11 +66,6 @@ export class Channel extends BaseStruct {
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) {
@@ -88,13 +75,13 @@ export class Channel extends BaseStruct {
}
}
const response = await axios.post(`https://discord.com/api/channels/${this.id}/messages`, {
const response = await this.client.axiosCustom.post(`/channels/${this.id}/messages`, {
content: toSend,
message_reference: inp.message_reference || undefined,
embeds: embds
}, config);
});
resolve(new message(response.data, this.#token));
resolve(new message(response.data, this.client));
});
}
@@ -105,22 +92,16 @@ export class Channel extends BaseStruct {
*/
async getMessages(configs) {
return new Promise(async (resolve) => {
const config = {
headers: {
Authorization: this.#token
}
}
var strconf = "?";
for (const i in configs) {
console.log(i);
strconf += `${i}=${configs[i]}&`;
}
const response = await axios.get(`https://discord.com/api/channels/${this.id}/messages`, config);
const response = await this.client.axiosCustom.get(`/channels/${this.id}/messages${strconf}`);
const msgMap = new Map();
for (const msgKey in response.data) {
const m = new message(response.data[msgKey], this.#token, this.guild);
const m = new message(response.data[msgKey], this.client, this.guild);
msgMap.set(m.id, m);
}
resolve(msgMap);
@@ -130,7 +111,7 @@ export class Channel extends BaseStruct {
toObj() {
var obj = {};
for (const key in this) {
if (key != '#token' && key != 'guild') {
if (key != 'guild') {
obj[key] = this[key];
}
}
+8 -29
View File
@@ -5,8 +5,6 @@ import { BaseStruct } from '../baseStruct.js';
export class GuildChannelManager extends BaseStruct {
#token;
/** @type {Guild} */
guild;
@@ -21,15 +19,9 @@ export class GuildChannelManager extends BaseStruct {
return new Promise(async (resolve) => {
try {
if (!channel.name) throw "Please provide a channel name";
const config = {
headers: {
Authorization: this.#token
}
}
const response = await axios.post(`https://discord.com/api/guilds/${this.guild.id}/channels`, channel, config);
const newChannel = new Channel(response.data, this.guild, this.#token);
const response = await this.client.axiosCustom.post(`/guilds/${this.guild.id}/channels`, channel);
const newChannel = new Channel(response.data, this.guild, this.client);
this.cache.set(newChannel.id, newChannel);
resolve(newChannel);
} catch (err) {
@@ -50,14 +42,8 @@ export class GuildChannelManager extends BaseStruct {
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);
const response = await this.client.axiosCustom.delete(`/channels/${cid}`);
const newChannel = new Channel(response.data, this.guild, this.client);
this.cache.delete(cid);
resolve(newChannel);
} catch (err) {
@@ -76,15 +62,9 @@ export class GuildChannelManager extends BaseStruct {
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);
const response = await this.client.axiosCustom.patch(`/channels/${cid}`, opts);
const newChannel = new Channel(response.data, this.guild, this.client);
this.cache.set(newChannel.id, newChannel);
resolve(newChannel);
} catch (err) {
@@ -93,10 +73,9 @@ export class GuildChannelManager extends BaseStruct {
});
}
constructor(token, guild, newCache) {
super();
constructor(client, guild, newCache) {
super(client);
this.#token = token;
this.guild = guild;
this.cache = newCache;
}
+11 -33
View File
@@ -4,9 +4,7 @@ import Guild from "./Guild.js";
import { BaseStruct } from "../baseStruct.js";
export class guildSticker extends BaseStruct {
#token;
export class guildSticker extends BaseStruct {
/** @type {String} */
id;
@@ -58,44 +56,29 @@ export class guildSticker extends BaseStruct {
*/
async modify(params) {
return new Promise(async (resolve) => {
const config = {
headers: {
Authorization: this.#token
}
}
const response = await axios.patch(`https://discord.com/api/guilds/${this.guild.id}/stickers/${this.id}`, params, config);
const response = await this.client.axiosCustom.patch(`/guilds/${this.guild.id}/stickers/${this.id}`, params);
this.#construcorHelper(response.data);
resolve(true);
});
}
async delete() {
return new Promise(async (resolve) => {
const config = {
headers: {
Authorization: this.#token
}
}
await axios.delete(`https://discord.com/api/guilds/${this.guild.id}/stickers/${this.id}`, config);
await this.client.axiosCustom.delete(`/guilds/${this.guild.id}/stickers/${this.id}`);
resolve(true);
});
}
constructor(o, guild, token) {
this.#token = token;
constructor(o, guild, client) {
super(client);
this.guild = guild;
this.#construcorHelper(o);
}
}
export class guildStickerManager {
#token;
export class guildStickerManager extends BaseStruct{
/** @type {Map<String, guildSticker>} */
cache;
@@ -114,13 +97,8 @@ export class guildStickerManager {
return new Promise(async (resolve) => {
if (!sticker.name || !sticker.description || !sticker.tags) return resolve(undefined);
const config = {
headers: {
Authorization: this.#token
}
}
const response = await axios.post(`https://discord.com/api/guilds/${this.guild.id}/stickers/${this.id}`, config);
const response = await this.client.axiosCustom.post(`https://discord.com/api/guilds/${this.guild.id}/stickers/${this.id}`);
resolve(new guildSticker(response.data));
});
@@ -129,11 +107,11 @@ export class guildStickerManager {
/**
* @param {Guild} guild
* @param {Object[]} o
* @param {String} token
*/
constructor(guild, o, token) {
constructor(guild, o, client) {
super(client);
this.guild = guild;
this.#token = token;
for (const stickerRaw of o) {
const sticker = new guildSticker(stickerRaw);
this.cache.set(sticker.id, sticker);
+8 -14
View File
@@ -1,5 +1,6 @@
import axios from "axios";
import { Channel } from "./Channel.js";
import { BaseStruct } from "../baseStruct.js";
export class Thread extends Channel {
/** @type {Number} */
@@ -12,8 +13,8 @@ export class Thread extends Channel {
message_count;
constructor(o, guild, token) {
super(o, guild, token);
constructor(o, guild, client) {
super(o, guild, client);
this.last_message_sent = o['total_message_sent'];
this.thread_metadata = o['thread_metadata'];
this.message_count = o['thread_metadata'];
@@ -21,8 +22,7 @@ export class Thread extends Channel {
}
export class ThreadManager {
#token;
export class ThreadManager extends BaseStruct {
/** @type {Map<String, Thread>} */
cache;
@@ -49,14 +49,8 @@ async delete(thread, reason=null) {
return new Promise(async (resolve) => {
try {
if (!this.cache.has(thread.id)) throw "This thread does not exist!";
const config = {
headers: {
Authorization: this.#token
}
}
await axios.delete(`https://discord.com/api/channels/${thread.id}`, config);
await this.client.axiosCustom.delete(`/channels/${thread.id}`);
const newChannel = this.cache.get(thread.id);
this.cache.delete(thread.id);
resolve(newChannel);
@@ -68,12 +62,12 @@ async delete(thread, reason=null) {
constructor(o, guild, token) {
constructor(o, guild, client) {
super(client);
this.cache = new Map();
this.#token = token;
for (const k of o) {
const newThread = new Thread(k, guild, token);
const newThread = new Thread(k, guild, client);
this.cache.set(newThread.id, newThread);
}
}
+17 -31
View File
@@ -12,8 +12,6 @@ import { BaseStruct } from '../baseStruct.js';
//See https://discord.com/developers/docs/resources/guild
export default class Guild extends BaseStruct {
#token;
//#region Vars
/** @type {String[]} */
embeded_activities;
@@ -159,33 +157,27 @@ export default class Guild extends BaseStruct {
//#endregion
async #getChannels(token) {
const config = {
headers: {
Authorization: token
}
}
const response = await axios.get(`https://discord.com/api/guilds/${this.id}/channels`, config);
async #getChannels() {
const response = await this.client.axiosCustom.get(`/guilds/${this.id}/channels`);
const m = new Map();
for (const channel of response.data) {
if (channel.type == 4) continue;
m.set(channel.id, new Channel(channel, this, token));
m.set(channel.id, new Channel(channel, this, this.client));
}
this.channels = new GuildChannelManager(token, this, m);
this.channels = new GuildChannelManager(this.client, this, m);
}
async #getMembers(membersObj, token) {
async #getMembers(membersObj) {
for (const m of membersObj) {
var tempRoles = [];
for (const rid of m["roles"]) {
tempRoles.push(this.roles.cache.get(rid));
}
const roleTemp = new guildMemberRoleManager(tempRoles, m["user"]["id"], token);
const roleTemp = new guildMemberRoleManager(tempRoles, m["user"]["id"], this.client);
roleTemp.guild = this;
const mem = new member(m, roleTemp);
@@ -197,17 +189,11 @@ export default class Guild extends BaseStruct {
* @returns {Promise<guildInvite[]>}
*/
getInvites() {
return new Promise(async (resolve, reject) => {
const config = {
headers: {
Authorization: this.#token
}
}
const response = await axios.get(`https://discord.com/api/guilds/${this.id}/invites`, config);
return new Promise(async (resolve, reject) => {
const response = await this.client.axiosCustom.get(`/guilds/${this.id}/invites`);
const invites = [];
for (const i of response.data) {
invites.push(new guildInvite(i, this, this.#token));
invites.push(new guildInvite(i, this, this.client));
}
resolve(invites);
@@ -218,12 +204,12 @@ export default class Guild extends BaseStruct {
* @param {Object} o
* @param {String} token
*/
constructor(o, token) {
super();
constructor(o, client) {
super(client);
this.members = new Map();
this.channels = new Map();
this.stickers = [];
this.#token = token;
for (const field in this) {
if (o[field] == undefined || field == "channels" || field == "members") continue;
@@ -233,21 +219,21 @@ export default class Guild extends BaseStruct {
for (const r of o[field]) {
temp.push(new guildRole(r));
}
this.roles = new guildRoleManager(temp, false, token);
this.roles = new guildRoleManager(temp, false, this.client);
this.roles.guild = this;
}
else if (field == 'stickers') {
this.stickers = new guildStickerManager(this, o[field], token);
this.stickers = new guildStickerManager(this, o[field], this.client);
}
else if (field == 'threads') {
this.threads = new ThreadManager(o[field], this, token);
this.threads = new ThreadManager(o[field], this, this.client);
}
else {
this[field] = o[field];
}
}
this.#getMembers(o["members"], token);
this.#getChannels(token);
this.#getMembers(o["members"]);
this.#getChannels();
}
}
+3 -5
View File
@@ -40,8 +40,7 @@ export default class invite extends BaseStruct {
async delete() {
return new Promise(async (resolve) => {
try {
const headers = { Authorization: this.#token }
const response = await axios.delete(`https://discord.com/api/invites/${this.code}`, { headers });
const response = await this.client.axiosCustom.delete(`/invites/${this.code}`);
resolve(response.data);
} catch (err) {
throw err.data;
@@ -49,10 +48,9 @@ export default class invite extends BaseStruct {
});
}
constructor(o, guild, token) {
super();
constructor(o, guild, client) {
super(client);
this.#token = token;
for (const k in this) {
if (o[k]) {
if (k == 'guild') { this.guild = guild }
+11 -25
View File
@@ -100,7 +100,6 @@ export class newGuildRoleObj {
export class guildMemberRoleManager extends BaseStruct {
#uid;
#token;
/** @type {Guild} */
guild;
@@ -115,16 +114,9 @@ export class guildMemberRoleManager extends BaseStruct {
return new Promise(async (resolve, reject) => {
const rid = (typeof role == 'string') ? role : role.id;
if (!this.cache.has(rid)) throw "USER DOESN'T HAVE THIS ROLE";
const config = {
headers: {
Authorization: this.#token
}
}
this.cache.delete(rid);
const response = await axios.delete(`https://discord.com/api/guilds/${this.guild.id}/members/${this.#uid}/roles/${rid}`, config);
const response = await this.client.axiosCustom.delete(`/guilds/${this.guild.id}/members/${this.#uid}/roles/${rid}`);
resolve(response);
});
@@ -141,10 +133,9 @@ export class guildMemberRoleManager extends BaseStruct {
if (this.cache.has(rid)) throw "USER ALREADY HAS THIS ROLE";
if (!grole) throw "ROLE NOT FOUND";
const headers = { Authorization: this.#token }
this.cache.set(rid, grole);
const response = await axios.put(`https://discord.com/api/guilds/${this.guild.id}/members/${this.#uid}/roles/${rid}`, {}, { headers });
const response = await this.client.axiosCustom.put(`/guilds/${this.guild.id}/members/${this.#uid}/roles/${rid}`, {});
resolve(response);
});
@@ -161,9 +152,9 @@ export class guildMemberRoleManager extends BaseStruct {
* @param {Array<guildRole>} roles
* @param {String} uid UID or GuildId
*/
constructor(roles, uid, token) {
super();
this.#token = token;
constructor(roles, uid, client) {
super(client);
this.#uid = uid;
this.cache = new Map();
roles.forEach((gr) => this.cache.set(gr.id, gr));
@@ -173,8 +164,7 @@ export class guildMemberRoleManager extends BaseStruct {
export class guildRoleManager extends BaseStruct {
#uid;
#token;
/** @type {Guild} */
guild;
@@ -205,11 +195,9 @@ export class guildRoleManager extends BaseStruct {
try {
const mrole = [...this.cache.values()].find(r => (r.name == role.name));
if (mrole) throw "ROLE ALREADY EXISTS!";
const headers = { Authorization: this.#token }
// this.cache.set(rid, grole);
const response = await axios.post(`https://discord.com/api/guilds/${this.guild.id}/roles`, role.toObj(), { headers });
const response = await this.client.axiosCustom.post(`/guilds/${this.guild.id}/roles`, role.toObj());
const newRole = new guildRole(response.data);
this.cache.set(newRole.id, newRole);
@@ -231,11 +219,9 @@ export class guildRoleManager extends BaseStruct {
try {
const grole = this.guild.roles.cache?.get(role.id);
if (!grole) throw "ROLE DOES NOT EXIST!";
const headers = { Authorization: this.#token }
// this.cache.set(rid, grole);
const response = await axios.delete(`https://discord.com/api/guilds/${this.guild.id}/roles/${role.id}`, { headers });
const response = await this.client.axiosCustom.delete(`/guilds/${this.guild.id}/roles/${role.id}`);
resolve(true);
} catch (err) {
throw err;
@@ -249,9 +235,9 @@ export class guildRoleManager extends BaseStruct {
* @param {Array<guildRole>} roles
* @param {String} uid UID or GuildId
*/
constructor(roles, uid, token) {
super();
this.#token = token;
constructor(roles, uid, client) {
super(client);
this.#uid = uid;
this.cache = new Map();
roles.forEach((gr) => this.cache.set(gr.id, gr));
+15 -35
View File
@@ -7,7 +7,7 @@ import Guild from '../guilds/Guild.js';
import { BaseStruct } from '../baseStruct.js';
class interactionOptions extends BaseStruct {
class interactionOptions {
/** @type {String} */
name;
@@ -37,9 +37,6 @@ export class Interaction extends BaseStruct {
/** @type {String} */
#token;
/** @type {{token: String, id: String}} */
#application;
/** @type {String} */
id;
@@ -70,12 +67,6 @@ export class Interaction extends BaseStruct {
return new Promise(async (resolve, reject) => {
const toSend = (typeof inp == 'string') ? inp : inp.content;
const config = {
headers: {
Authorization: this.#application.token
}
}
var embds = undefined;
if (inp.embeds) {
embds = [];
@@ -84,16 +75,16 @@ export class Interaction extends BaseStruct {
}
}
const response = await axios.post(`https://discord.com/api/interactions/${this.id}/${this.#token}/callback`, {
const response = await this.client.axiosCustom.post(`/interactions/${this.id}/${this.#token}/callback`, {
type: 4,
data: {
content: toSend,
embeds: embds,
flags: (inp.ephemeral) ? (1 << 6) : undefined
}
}, config);
});
resolve(new message(response.data, this.#application.token));
resolve(new message(response.data, this.client));
});
}
@@ -103,12 +94,6 @@ export class Interaction extends BaseStruct {
try {
const toSend = (typeof inp == 'string') ? inp : inp.content;
const config = {
headers: {
Authorization: this.#application.token
}
}
var embds = undefined;
if (inp.embeds) {
embds = [];
@@ -117,15 +102,15 @@ export class Interaction extends BaseStruct {
}
}
const response = await axios.patch(`https://discord.com/api/webhooks/${this.#application.id}/${this.#token}/messages/@original`, {
const response = await this.client.axiosCustom.patch(`/webhooks/${this.client.id}/${this.#token}/messages/@original`, {
content: toSend,
embeds: embds,
flags: (inp.ephemeral) ? (1 << 6) : undefined
}, config);
});
// const response = await axios.get(`https://discord.com/api/webhooks/${this.#application.id}/${this.#token}/messages/@original`, config);
resolve(new message(response.data, this.#application.token));
resolve(new message(response.data, this.client));
} catch(err) {
reject(err);
}
@@ -142,12 +127,6 @@ export class Interaction extends BaseStruct {
try {
const toSend = (typeof inp == 'string') ? inp : inp.content;
const config = {
headers: {
Authorization: this.#application.token
}
}
var embds = undefined;
if (inp.embeds) {
embds = [];
@@ -156,15 +135,15 @@ export class Interaction extends BaseStruct {
}
}
const response = await axios.post(`https://discord.com/api/webhooks/${this.#application.id}/${this.#token}`, {
const response = await this.client.axiosCustom.post(`/webhooks/${this.client.id}/${this.#token}`, {
content: toSend,
embeds: embds,
flags: (inp.ephemeral) ? (1 << 6) : undefined
}, config);
});
// const response = await axios.get(`https://discord.com/api/webhooks/${this.#application.id}/${this.#token}/messages/@original`, config);
resolve(new message(response.data, this.#application.token));
resolve(new message(response.data, this.client));
} catch(err) {
reject(err);
}
@@ -178,7 +157,7 @@ export class Interaction extends BaseStruct {
async delete() {
return new Promise(async (resolve, reject) => {
try {
const response = await axios.delete(`https://discord.com/api/webhooks/${this.#application.id}/${this.#token}/messages/@original`);
const response = await this.client.axiosCustom.delete(`/webhooks/${this.client.id}/${this.#token}/messages/@original`);
resolve(response.status == 204);
} catch (err) {
@@ -191,8 +170,9 @@ export class Interaction extends BaseStruct {
/**
* @param {Object} intRaw
*/
constructor(intRaw, token, apid) {
this.#application = {token: token, id: apid};
constructor(intRaw, client) {
super(client);
this.#token = intRaw["token"];
for (const k in this) {
@@ -202,7 +182,7 @@ export class Interaction extends BaseStruct {
this.data = new interactionOptions(intRaw[k]);
} else {
if (k == 'channel_id') {
this.channel = new Channel(intRaw[k], this.guild, this.#application.token);
this.channel = new Channel(intRaw[k], this.guild, client);
}
this[k] = intRaw[k];
+8 -20
View File
@@ -2,9 +2,10 @@ import author from './User.js';
import axios from 'axios';
import { Channel } from '../guilds/Channel.js';
import Guild from '../guilds/Guild.js';
import { BaseStruct } from '../baseStruct.js';
export class message {
export class message extends BaseStruct {
/** @type {author} */
author;
@@ -56,9 +57,6 @@ export class message {
/** @type {Channel} */
channel;
/** @type {String} */
#token;
/**
* @param {String} content
@@ -84,12 +82,7 @@ export class message {
delete() {
return new Promise(async (resolve, reject) => {
try {
const config = {
headers: {
Authorization: this.#token
}
}
const response = await axios.delete(`https://discord.com/api/channels/${this.channel.id}/messages/${this.id}`, config);
const response = await this.client.axiosCustom.delete(`/channels/${this.channel.id}/messages/${this.id}`);
resolve(response.data);
} catch(err) {
@@ -106,14 +99,8 @@ export class message {
async edit(inp) {
return new Promise(async (resolve, reject) => {
const newMsg = (typeof inp == "string") ? {content: inp} : inp;
const config = {
headers: {
Authorization: this.#token
}
}
axios.patch(`https://discord.com/api/channels/${this.channel.id}/messages/${this.id}`, newMsg, config).then((response) => {
this.client.axiosCustom.patch(`/channels/${this.channel.id}/messages/${this.id}`, newMsg).then((response) => {
resolve(response.data);
}).catch((err) => {
reject(`REQUEST FAILED WITH CODE ${err.response.data.code} AND THE FOLLOWING REASON:\n${err.response.data.message}`);
@@ -125,8 +112,9 @@ export class message {
/**
* @param {Object} msgRaw
*/
constructor(msgRaw, token, guild) {
this.#token = token;
constructor(msgRaw, client, guild) {
super(client);
this.guild = guild;
for (const k in this) {
@@ -139,7 +127,7 @@ export class message {
}
else {
if (k == 'channel_id') {
this.channel = new Channel({id: msgRaw[k]}, this.guild, this.#token);
this.channel = new Channel({id: msgRaw[k]}, this.guild, client);
}
this[k] = msgRaw[k];