fixed guild channels and interactions

This commit is contained in:
ION606
2023-04-02 09:49:47 -04:00
parent f1415a9840
commit 658b45feee
10 changed files with 445 additions and 22 deletions
+142
View File
@@ -0,0 +1,142 @@
export class BaseGuild {
/** @type {String[]} */
embeded_activities;
/** @type {String} */
description;
/** @type {String} */
id;
/** @type {String} */
name;
/** @type {String} */
icon;
/** @type {Boolean} */
nsfw;
/** @type {Map<String, member>} */
members;
/** @type {String} */
hub_type;
/** @type {Number} */
max_video_channel_users;
/** @type {String[]} */
stickers;
/** @type {String} */
hub_type;
/** @type {Map<String, member>} */
members;
/** @type {guildRoleManager} */
roles;
/** @type {String} */
banner;
/** @type {String} */
application_id;
/** @type {String} */
owner_id;
/** @type {Map<String, Channel>} */
channels;
/** @type {String} */
home_header;
/** @type {Number} */
premium_tier;
/** @type {Number} */
nsfw_level;
/** @type {Number} */
verification_level;
/** @type {Number} */
mfa_level;
// /** @type {String} */ //FIXME
// threads;
/** @type {Number} */
system_channel_flags;
/** @type {String} */
safety_alerts_channel_id;
/** @type {object[]} */
presences;
/** @type {GuildEmoji[]} */
emojis;
/** @type {String} */
public_updates_channel_id;
/** @type {Object[]} */
stage_instances;
/** @type {String} */
afk_channel_id;
/** @type {Number} */
afk_timeout;
/** @type {Number} */
verification_level;
/** @type {Number} */
default_message_notifications;
/** @type {Number} */
explicit_content_filter;
/** @type {String} */
system_channel_id;
/** @type {Number} */
system_channel_flags;
/** @type {String} */
rules_channel_id;
/** @type {Number} */
max_presences;
/** @type {Number} */
max_members;
/** @type {String} */
vanity_url_code;
/** @type {Number} */
premium_tier;
/** @type {Number} */
premium_subscription_count;
/** @type {String} */
preferred_locale;
/** @type {String} */
public_updates_channel_id;
/** @type {Number} */
max_video_channel_users;
//Welcome screen
//see https://discord.com/developers/docs/resources/guild#welcome-screen-object
/** @type {guildStickerManager} */
stickers;
}
+45
View File
@@ -0,0 +1,45 @@
import axios from 'axios';
import {Channel} from '../messages/message.js';
import Guild from './Guild.js';
export class GuildChannelManager {
#token;
/** @type {Guild} */
guild;
/** @type {Map<String, Channel>} */
cache;
/**
* @returns {Promise<Channel>}
* @param {{name: String, type: Number, topic: String, position: Number, parent_id: String}} channel
*/
create(channel) {
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);
this.cache.set(newChannel.id, newChannel);
resolve(newChannel);
} catch (err) {
throw err;
}
});
}
constructor(token, guild, newCache) {
this.#token = token;
this.guild = guild;
this.cache = newCache;
}
}
+140
View File
@@ -0,0 +1,140 @@
import axios from "axios";
import user from "../messages/User.js";
import Guild from "./Guild.js";
export class guildSticker {
#token;
/** @type {String} */
id;
/** @type {String} */
pack_id;
/** @type {String} */
name;
/** @type {String} */
description;
/** @type {String} */
tags;
/** @type {String} */
asset;
/** @type {Number} */
type;
/** @type {Number} */
format_type;
/** @type {Boolean} */
available;
/** @type {Guild} */
guild;
/** @type {user} */
user;
/** @type {Number} */
sort_value;
#construcorHelper(o) {
for (const k in this) {
if (o[k]) this[k] = o[k];
else this[k] = 0;
}
}
/**
* @description modifies the guild sticker IN PLACE
* @param {{name: String, description: String, tags: String}} params
* @returns {Promise<Boolean>}
*/
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);
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);
resolve(true);
});
}
constructor(o, guild, token) {
this.#token = token;
this.guild = guild;
this.#construcorHelper(o);
}
}
export class guildStickerManager {
#token;
/** @type {Map<String, guildSticker>} */
cache;
/** @type {Guild} */
guild;
/**
* @description Creates the new stickers and adds it to cache
* @returns {Promise<guildSticker>}
* @param {{name: String, description: String, tags: String }} sticker
*/
async create(sticker) {
//HOW DO YOU DEAL WITH FILES????????
//see https://discord.com/developers/docs/reference#image-data maybe?
return Promise.reject(undefined);
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);
resolve(new guildSticker(response.data));
});
}
/**
* @param {Guild} guild
* @param {Object[]} o
* @param {String} token
*/
constructor(guild, o, token) {
this.guild = guild;
this.#token = token;
for (const stickerRaw of o) {
const sticker = new guildSticker(stickerRaw);
this.cache.set(sticker.id, sticker);
}
}
}
+69 -4
View File
@@ -2,14 +2,17 @@ import axios from 'axios';
import member from './member.js';
import {guildRole, guildRoleManager, guildMemberRoleManager} from './guildRoles.js';
import GuildEmoji from './guildEmoji.js';
import {Channel} from '../messages/message.js';
import guildInvite from './guildInvite.js';
import { guildSticker, guildStickerManager } from './GuildStickers.js';
import { GuildChannelManager } from './GuildChannelManager.js';
import { Channel } from '../messages/message.js';
//See https://discord.com/developers/docs/resources/guild
export default class Guild {
#token;
//#region Vars
/** @type {String[]} */
embeded_activities;
@@ -58,7 +61,7 @@ export default class Guild {
/** @type {String} */
owner_id;
/** @type {Map<String, Channel>} */
/** @type {GuildChannelManager} */
channels;
/** @type {String} */
@@ -95,7 +98,63 @@ export default class Guild {
public_updates_channel_id;
/** @type {Object[]} */
stage_instances
stage_instances;
/** @type {String} */
afk_channel_id;
/** @type {Number} */
afk_timeout;
/** @type {Number} */
verification_level;
/** @type {Number} */
default_message_notifications;
/** @type {Number} */
explicit_content_filter;
/** @type {String} */
system_channel_id;
/** @type {Number} */
system_channel_flags;
/** @type {String} */
rules_channel_id;
/** @type {Number} */
max_presences;
/** @type {Number} */
max_members;
/** @type {String} */
vanity_url_code;
/** @type {Number} */
premium_tier;
/** @type {Number} */
premium_subscription_count;
/** @type {String} */
preferred_locale;
/** @type {String} */
public_updates_channel_id;
/** @type {Number} */
max_video_channel_users;
//Welcome screen
//see https://discord.com/developers/docs/resources/guild#welcome-screen-object
/** @type {guildStickerManager} */
stickers;
//#endregion
async #getChannels(token) {
@@ -107,10 +166,13 @@ export default class Guild {
const response = await axios.get(`https://discord.com/api/guilds/${this.id}/channels`, config);
const m = new Map();
for (const channel of response.data) {
if (channel.type == 4) continue;
this.channels.set(channel.id, new Channel(token, channel, this));
m.set(channel.id, new Channel(channel, this, token));
}
this.channels = new GuildChannelManager(token, this, m);
}
@@ -171,6 +233,9 @@ export default class Guild {
this.roles = new guildRoleManager(temp, false, token);
this.roles.guild = this;
}
else if (field == 'stickers') {
this.stickers = new guildStickerManager(this, o[field], token);
}
else {
this[field] = o[field];
}
+10 -6
View File
@@ -1,4 +1,4 @@
import author from '../messages/author.js';
import author from '../messages/User.js';
import Guild from './Guild.js'
import { Channel } from '../messages/message.js';
import axios from 'axios';
@@ -38,9 +38,13 @@ export default class invite {
async delete() {
return new Promise(async (resolve) => {
// const headers = { Authorization: this.#token }
// const response = await axios.delete(`https://discord.com/api/guilds/${this.guild.id}/roles`, role.toObj(), { headers });
// resolve(response.data);
try {
const headers = { Authorization: this.#token }
const response = await axios.delete(`https://discord.com/api/invites/${this.code}`, { headers });
resolve(response.data);
} catch (err) {
throw err.data;
}
});
}
@@ -49,8 +53,8 @@ export default class invite {
for (const k in this) {
if (o[k]) {
if (k == 'guild') { this.guild = guild }
else if (k == 'channel') { this.channel = this.guild.channels.get(o[k]['id']); }
else if (k == 'inviter') { this.inviter = new author(o[k], null); }
// else if (k == 'channel') { this.channel = this.guild.channels.cache?.get(o[k]['id']); }
else if (k == 'inviter') { this.inviter = new author(o[k]); }
else { this[k] = o[k]; }
} else { this[k] = 0; }
}