Added invite functionality and changed the EJS

This commit is contained in:
ION606
2023-04-01 21:49:27 -04:00
parent e0ffe24eec
commit f1415a9840
25 changed files with 589 additions and 201 deletions
+77 -81
View File
@@ -1,24 +1,23 @@
const opts = require('./clientOpts.js');
const gateWayIntents = require('../gateway/intents.js');
const gateWayEvents = require('../gateway/dispatch.js');
var WebSocketClient = require('websocket').client;
const WebSocketConnection = require('websocket').connection;
const handleResponses = require('./handleEvents.js');
const { EventEmitter } = require('events');
const axios = require('axios');
const { exit } = require('process');
const Guild = require('../guilds/guild.js');
import gateWayEvents from '../gateway/dispatch.js';
//import WebSocketClient from 'websocket';
//import WebSocketConnection from 'websocket';
import WebSocket from 'ws';
import handleResponses from './handleEvents.js';
import { EventEmitter } from 'events';
import axios from 'axios';
import { exit } from 'process';
import Guild from '../guilds/Guild.js';
class Client extends EventEmitter {
/** @type {WebSocketClient} */
export class Client extends EventEmitter {
/** @type {WebSocket} */
ws;
/** @type {Number} */
heartBeatInterval;
/** @type {Array<opts.intents>} */
/** @type {gateWayEvents[]} */
gwintents;
/** @type {String} */
@@ -51,10 +50,10 @@ class Client extends EventEmitter {
async #heartbeat(hbInt, hbSequence) {
const toSend = JSON.stringify({ op: 1, d: 0 });
this.connection.send((toSend));
this.ws.send((toSend));
setInterval(() => {
this.connection.send(toSend);
this.ws.send(toSend);
}, hbInt);
}
@@ -65,27 +64,6 @@ class Client extends EventEmitter {
{
this.heartBeatInterval = hbint;
console.log("INTERVAL SET TO: " + this.heartBeatInterval);
//Get the user intents
let iCount = 0;
for (let i of this.gwintents) {
iCount += (i) ? i : 0;
}
var idObj = {
op: 2,
d: {
token: this.#token.replace("Bot ", ""),
intents: iCount, //61440,
properties: {
os: "linux",
browser: "ion_",
device: "my_library"
}
}
};
this.connection.send(JSON.stringify(idObj));
this.#heartbeat(hbint);
}
@@ -134,61 +112,79 @@ class Client extends EventEmitter {
if (!isUser) token = "Bot " + token;
return new Promise((resolve, reject) => {
this.ws = new WebSocketClient({maxReceivedFrameSize: Infinity});
this.ws = new WebSocket("wss://gateway.discord.gg/?v=10&encoding=json");
this.#token = token;
this.ws.on('connect', async (connection) => {
connection.on('message', async (msg) => {
const data = JSON.parse(msg.utf8Data);
const response = await handleResponses(data, token, this.id);
if (response.op == 10) { this.#startHeartBeat(response.heartBeat, token); }
else if (response.op == 0) {
if (response.t == gateWayEvents.Ready) {
this.user_profile = response.profile;
this.user_settings = response.config;
this.id = response.profile.id;
console.log(response.guilds);
this.ready();
this.ws.on('open', () => {
//Get the user intents
let iCount = 0;
for (let i of this.gwintents) {
iCount += (i) ? i : 0;
}
var idObj = {
op: 2,
d: {
token: token.replace("Bot ", ""),
intents: iCount, //61440,
properties: {
os: "linux",
browser: "ion_",
device: "my_library"
}
else if (response.t == gateWayEvents.MessageCreate) {
if (data["d"]["author"]["id"] != this.user_profile.id){
this.messageRecieved(response.message);
}
}
else if (response.t == gateWayEvents.InteractionCreate) {
if (data["d"]["user"]["id"] != this.user_profile.id) {
this.interactionRecieved(response.interaction);
}
}
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);
}
});
connection.on('close', (code, desc) => {
console.log(`CONNECTION CLOSED WITH CODE ${code}\nREASON:\n ${desc}`);
exit(1);
});
connection.on('error', (err) => {
reject(err);
});
this.connection = connection;
};
this.ws.send(JSON.stringify(idObj));
});
this.ws.on('connectFailed', (err) => { reject(err); });
this.ws.on('message', async (msg) => {
const data = JSON.parse(msg.toString());
const response = await handleResponses(data, token, this.id);
this.ws.connect("wss://gateway.discord.gg/?v=10&encoding=json");
if (response.op == 10) { this.#startHeartBeat(response.heartBeat, token); }
else if (response.op == 0) {
if (response.t == gateWayEvents.Ready) {
this.user_profile = response.profile;
this.user_settings = response.config;
this.id = response.profile.id;
console.log(response.guilds);
this.ready();
}
else if (response.t == gateWayEvents.MessageCreate) {
if (data["d"]["author"]["id"] != this.user_profile.id){
response.message.guild = this.guilds.get(response.message.guild_id);
this.messageRecieved(response.message);
}
}
else if (response.t == gateWayEvents.InteractionCreate) {
if (data["d"]["user"]["id"] != this.user_profile.id) {
this.interactionRecieved(response.interaction);
}
}
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);
}
});
this.ws.on('close', (code, desc) => {
console.log(`CONNECTION CLOSED WITH CODE ${code}\nREASON:\n ${desc}`);
exit(1);
});
this.ws.on('error', (err) => {
reject(err);
});
this.ws.on('error', (err) => { reject(err); });
});
}
}
//All client properties will be re-routed through this export
module.exports = { Client, gateWayIntents }
export {gateWayIntents} from '../gateway/intents.js';
-7
View File
@@ -1,7 +0,0 @@
const intents = require('../gateway/intents.js');
module.exports = {
/**@type {Array<intents>} */
intents: []
}
+5 -6
View File
@@ -1,8 +1,7 @@
const { exit } = require('process');
const gateWayEvents = require('../gateway/dispatch.js');
const { message } = require('../messages/message.js');
const Interaction = require('../interactions/interaction.js');
const Guild = require('../guilds/guild.js');
import gateWayEvents from '../gateway/dispatch.js'
import { message } from '../messages/message.js';
import {Interaction} from '../interactions/interaction.js';
import Guild from '../guilds/Guild.js';
/**
@@ -10,7 +9,7 @@ const Guild = require('../guilds/guild.js');
* @param {Object} msg
* @returns {Promise<Boolean>}
*/
module.exports = async function handleEvents(msgObj, token, id) {
export default async function handleEvents(msgObj, token, id) {
return new Promise((resolve, reject) => {
const op = msgObj["op"];
const t = msgObj["t"];
+1 -1
View File
@@ -1,4 +1,4 @@
module.exports = Object.freeze({
export default Object.freeze({
ApplicationCommandPermissionsUpdate: "APPLICATION_COMMAND_PERMISSIONS_UPDATE",
ChannelCreate: "CHANNEL_CREATE",
ChannelDelete: "CHANNEL_DELETE",
+1 -1
View File
@@ -1,4 +1,4 @@
module.exports = Object.freeze({
export const gateWayIntents = Object.freeze({
Guilds: 1 << 0,
GuildMembers: 1 << 1,
GuildModeration: 1 << 2,
+60 -23
View File
@@ -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;
+2 -5
View File
@@ -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;
}
+58
View File
@@ -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; }
}
}
}
+207 -2
View File
@@ -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));
}
}
+11 -8
View File
@@ -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;
}
+6 -9
View File
@@ -1,10 +1,10 @@
const axios = require('axios');
const author = require('../messages/author.js');
const { Channel, message } = require('../messages/message.js');
const Embed = require('../messages/embed.js');
import axios from 'axios';
import author from '../messages/author.js';
import { Channel, message } from '../messages/message.js';
import {Embed} from '../messages/embed.js';
class Interaction {
export class Interaction {
/** @type {author} */
user;
@@ -177,7 +177,4 @@ class Interaction {
}
}
}
}
module.exports = Interaction;
}
+1
View File
@@ -0,0 +1 @@
// see https://discord.com/developers/docs/reference#image-data
+2 -6
View File
@@ -1,4 +1,4 @@
class msgAuthor {
export default class msgAuthor {
/** @type {String} */
id;
@@ -30,8 +30,4 @@ class msgAuthor {
}
}
}
}
module.exports = msgAuthor;
}
+3 -5
View File
@@ -1,6 +1,6 @@
const colConvert = require('../../utils/color_functions.js');
import colConvert from '../../utils/color_functions.js';
class Embed {
export class Embed {
/** @type {Number} */
color;
@@ -122,6 +122,4 @@ class Embed {
return retObj;
}
//#endregion
}
module.exports = Embed;
}
+65 -13
View File
@@ -1,19 +1,68 @@
const messageChannelTypes = require('./messageChannelTypes.js');
const author = require('./author.js');
const axios = require('axios');
const Embed = require('./embed');
import author from './author.js';
import axios from 'axios';
class Channel {
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;
constructor(token, id) {
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(token, channel, guild) {
this.#token = token;
this.id = id;
for (const k in this) {
if (channel[k]) this[k] = channel[k];
}
this.guild = guild;
}
/**
@@ -49,11 +98,11 @@ class Channel {
}
class message {
export class message {
/** @type {author} */
author;
/** @type {Object} */
/** @type {String} */
channel_id;
/** @type {Object[]} */
@@ -89,6 +138,9 @@ class message {
/** @type {Object[]} */
embeds;
/** @type {Guild} */
guild;
/** @type {String} */
guild_id;
@@ -96,7 +148,7 @@ class message {
type;
/** @type {Channel} */
channel
channel;
/** @type {String} */
#token;
@@ -167,7 +219,7 @@ class message {
/**
* @param {Object} msgRaw
*/
constructor(msgRaw, token) {
constructor(msgRaw, token, guild) {
this.#token = token;
for (const k in this) {
@@ -180,7 +232,7 @@ class message {
}
else {
if (k == 'channel_id') {
this.channel = new Channel(this.#token, msgRaw[k]);
this.channel = new Channel(this.#token, {id: msgRaw[k]}, null);
}
this[k] = msgRaw[k];
@@ -191,4 +243,4 @@ class message {
}
module.exports = { message, messageChannelTypes, Channel };
export {messageChannelTypes} from './messageChannelTypes.js';
+1 -1
View File
@@ -1,7 +1,7 @@
//Blatantly stolen from https://github.com/discordjs/discord-api-types/blob/main/gateway/v10.ts
module.exports = Object.freeze({
export const messageChannelTypes = Object.freeze({
/**
* A text channel within a guild
*/
+5 -8
View File
@@ -1,8 +1,5 @@
const {message} = require('./messages/message');
const {Client, gateWayIntents} = require('./client/client.js');
const Embed = require('./messages/embed');
const messageChannelTypes = require('./messages/messageChannelTypes');
const Interaction = require('./interactions/interaction.js');
module.exports = { message, Client, gateWayIntents, Embed, messageChannelTypes, Interaction }
export {message} from './messages/message.js';
export {Client, gateWayIntents} from './client/client.js';
export {Embed} from './messages/embed.js';
export {messageChannelTypes} from './messages/messageChannelTypes.js';
export {Interaction} from './interactions/interaction.js';