mirror of
https://github.com/ION606/custom_discordjs.git
synced 2026-05-14 22:26:54 +00:00
Added invite functionality and changed the EJS
This commit is contained in:
+60
-23
@@ -1,12 +1,14 @@
|
||||
const axios = require('axios');
|
||||
const member = require('./member.js');
|
||||
const guildRole = require('./guildRoles.js');
|
||||
const GuildEmoji = require('./guildEmoji.js');
|
||||
const { Channel } = require('../messages/message.js');
|
||||
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';
|
||||
|
||||
//See https://discord.com/developers/docs/resources/guild
|
||||
|
||||
class Guild {
|
||||
export default class Guild {
|
||||
#token;
|
||||
|
||||
/** @type {String[]} */
|
||||
embeded_activities;
|
||||
@@ -26,7 +28,7 @@ class Guild {
|
||||
/** @type {Boolean} */
|
||||
nsfw;
|
||||
|
||||
/** @type {member[]} */
|
||||
/** @type {Map<String, member>} */
|
||||
members;
|
||||
|
||||
/** @type {String} */
|
||||
@@ -44,7 +46,7 @@ class Guild {
|
||||
/** @type {Map<String, member>} */
|
||||
members;
|
||||
|
||||
/** @type {guildRole[]} */
|
||||
/** @type {guildRoleManager} */
|
||||
roles;
|
||||
|
||||
/** @type {String} */
|
||||
@@ -107,39 +109,74 @@ class Guild {
|
||||
|
||||
for (const channel of response.data) {
|
||||
if (channel.type == 4) continue;
|
||||
this.channels.set(channel.id, new Channel(token, channel.id));
|
||||
this.channels.set(channel.id, new Channel(token, channel, this));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async #getMembers(membersObj, token) {
|
||||
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);
|
||||
roleTemp.guild = this;
|
||||
const mem = new member(m, roleTemp);
|
||||
|
||||
this.members.set(mem.user.id, mem);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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);
|
||||
const invites = [];
|
||||
for (const i of response.data) {
|
||||
invites.push(new guildInvite(i, this, this.#token));
|
||||
}
|
||||
|
||||
resolve(invites);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Object} o
|
||||
* @param {String} token
|
||||
*/
|
||||
constructor(o, token) {
|
||||
this.members = new Map();
|
||||
this.channels = new Map();
|
||||
this.roles = [];
|
||||
this.stickers = [];
|
||||
this.#token = token;
|
||||
|
||||
for (const field in this) {
|
||||
if (o[field] == undefined || field == "channels") continue;
|
||||
if (o[field] == undefined || field == "channels" || field == "members") continue;
|
||||
|
||||
if (field == 'members') {
|
||||
for (const m of o[field]) {
|
||||
const mem = new member(m);
|
||||
this.members.set(mem.user.id);
|
||||
}
|
||||
}
|
||||
else if (field == 'roles') {
|
||||
if (field == 'roles') {
|
||||
var temp = [];
|
||||
for (const r of o[field]) {
|
||||
this.roles.push(new guildRole(r));
|
||||
temp.push(new guildRole(r));
|
||||
}
|
||||
this.roles = new guildRoleManager(temp, false, token);
|
||||
this.roles.guild = this;
|
||||
}
|
||||
else {
|
||||
this[field] = o[field];
|
||||
}
|
||||
}
|
||||
|
||||
this.#getMembers(o["members"], token);
|
||||
this.#getChannels(token);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
module.exports = Guild;
|
||||
@@ -1,4 +1,4 @@
|
||||
class GuildEmoji {
|
||||
export default class GuildEmoji {
|
||||
/** @type {Number} */
|
||||
version;
|
||||
|
||||
@@ -22,7 +22,4 @@ class GuildEmoji {
|
||||
|
||||
/** @type {Boolean} */
|
||||
animated;
|
||||
}
|
||||
|
||||
|
||||
module.exports = GuildEmoji;
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import author from '../messages/author.js';
|
||||
import Guild from './Guild.js'
|
||||
import { Channel } from '../messages/message.js';
|
||||
import axios from 'axios';
|
||||
|
||||
|
||||
export default class invite {
|
||||
#token;
|
||||
|
||||
code;
|
||||
|
||||
/** @type {Guild} */
|
||||
guild;
|
||||
|
||||
/** @type {Channel?} */
|
||||
channel;
|
||||
|
||||
/** @type {member?} */
|
||||
inviter;
|
||||
|
||||
/** @type {EpochTimeStamp} */
|
||||
expires_at;
|
||||
|
||||
/** @type {Number} */
|
||||
uses;
|
||||
|
||||
/** @type {Number} */
|
||||
max_uses;
|
||||
|
||||
/** @type {Number} */
|
||||
max_age;
|
||||
|
||||
/** @type {Boolean} */
|
||||
temporary;
|
||||
|
||||
/** @type {String} */
|
||||
created_at;
|
||||
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
constructor(o, guild, token) {
|
||||
this.#token = token;
|
||||
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 { this[k] = o[k]; }
|
||||
} else { this[k] = 0; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,11 @@
|
||||
class guildRole {
|
||||
import axios from 'axios';
|
||||
import Guild from './Guild.js';
|
||||
|
||||
// Maybe add support for this
|
||||
// https://discord.com/developers/docs/resources/guild#modify-guild-role-positions
|
||||
|
||||
|
||||
export class guildRole {
|
||||
/** @type {Number} */
|
||||
version;
|
||||
|
||||
@@ -47,5 +54,203 @@ class guildRole {
|
||||
}
|
||||
}
|
||||
|
||||
export class newGuildRoleObj {
|
||||
/** @type {String} */
|
||||
name;
|
||||
|
||||
module.exports = guildRole;
|
||||
/** @type {WHAT} */
|
||||
permissions;
|
||||
|
||||
/** @type {String} */
|
||||
color;
|
||||
|
||||
/** @type {Boolean} */
|
||||
hoist;
|
||||
|
||||
/** @type {WHAT} */
|
||||
icon;
|
||||
|
||||
/** @type {String} */
|
||||
unicode_emoji;
|
||||
|
||||
/** @type {Boolean} */
|
||||
mentionable;
|
||||
|
||||
/**
|
||||
* @param {{ name: String, permissions?: any, color?: Number, hoist>: Boolean, icon?: String, unicode_emoji?: String, mentionable?: Boolean }} o
|
||||
*/
|
||||
constructor(o = undefined) {
|
||||
for (const f in this) {
|
||||
if (f in o) {
|
||||
this[f] = o[f];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
toObj() {
|
||||
let obj = {};
|
||||
for (const f in this) {
|
||||
obj[f] = this[f];
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export class guildMemberRoleManager {
|
||||
#uid;
|
||||
#token;
|
||||
|
||||
/** @type {Guild} */
|
||||
guild;
|
||||
|
||||
/** @type {Map<String, guildRole>} */
|
||||
cache;
|
||||
|
||||
/**
|
||||
* @param {String | guildRole} role
|
||||
*/
|
||||
async remove(role) {
|
||||
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);
|
||||
|
||||
resolve(response);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {String | guildRole} role
|
||||
* @returns { import('axios').AxiosResponse | String }
|
||||
*/
|
||||
async add(role) {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const rid = (typeof role == 'string') ? role : role.id;
|
||||
const grole = this.guild.roles.cache?.get(rid)
|
||||
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 });
|
||||
|
||||
resolve(response);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {String | guildRole} role
|
||||
*/
|
||||
has(role) {
|
||||
return this.cache.has(role);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Array<guildRole>} roles
|
||||
* @param {String} uid UID or GuildId
|
||||
*/
|
||||
constructor(roles, uid, token) {
|
||||
this.#token = token;
|
||||
this.#uid = uid;
|
||||
this.cache = new Map();
|
||||
roles.forEach((gr) => this.cache.set(gr.id, gr));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export class guildRoleManager {
|
||||
#uid;
|
||||
#token;
|
||||
|
||||
/** @type {Guild} */
|
||||
guild;
|
||||
|
||||
/** @type {Map<String, guildRole>} */
|
||||
cache;
|
||||
|
||||
/**
|
||||
* @param {String | guildRole} role
|
||||
*/
|
||||
has(role) {
|
||||
const rid = (typeof role == 'string') ? role : role.id;
|
||||
return this.cache.has(rid);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {String} name
|
||||
*/
|
||||
findByName(name) {
|
||||
return [...this.cache.values()].find(r => (r.name == name));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {newGuildRoleObj} role
|
||||
* @returns {Promise<guildRole>}
|
||||
*/
|
||||
create(role) {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
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 newRole = new guildRole(response.data);
|
||||
this.cache.set(newRole.id, newRole);
|
||||
resolve(newRole);
|
||||
} catch (err) {
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @description returns true if succeeded and throws error otherwise
|
||||
* @param {guildRole} role
|
||||
* @returns {Promise<Boolean>}
|
||||
*/
|
||||
delete(role) {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
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 });
|
||||
resolve(true);
|
||||
} catch (err) {
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param {Array<guildRole>} roles
|
||||
* @param {String} uid UID or GuildId
|
||||
*/
|
||||
constructor(roles, uid, token) {
|
||||
this.#token = token;
|
||||
this.#uid = uid;
|
||||
this.cache = new Map();
|
||||
roles.forEach((gr) => this.cache.set(gr.id, gr));
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,13 @@
|
||||
class member {
|
||||
import axios from 'axios';
|
||||
import {guildRole, guildMemberRoleManager} from "./guildRoles.js";
|
||||
// https://discord.com/developers/docs/resources/guild#modify-guild-member
|
||||
|
||||
|
||||
export default class member {
|
||||
/** @type {Object} */
|
||||
user;
|
||||
|
||||
/** @type {Object[]} */
|
||||
/** @type {guildMemberRoleManager} */
|
||||
roles;
|
||||
|
||||
/** @type {String} */
|
||||
@@ -32,14 +37,12 @@ class member {
|
||||
/** @type {String} */
|
||||
avatar;
|
||||
|
||||
constructor(o) {
|
||||
constructor(o, roles) {
|
||||
this.roles = roles;
|
||||
for (const k in this) {
|
||||
if (o[k]) {
|
||||
if (o[k] && k != 'roles') {
|
||||
this[k] = o[k];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
module.exports = member;
|
||||
}
|
||||
Reference in New Issue
Block a user