Skip to content

Commit

Permalink
backup push before migration to BaseStruct
Browse files Browse the repository at this point in the history
  • Loading branch information
ION606 committed Apr 7, 2023
1 parent 1a048a0 commit e6b32cc
Show file tree
Hide file tree
Showing 15 changed files with 248 additions and 68 deletions.
6 changes: 6 additions & 0 deletions structures/baseStruct.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class BaseStruct {
/** @type {import('./client/client.js').Client} */
client;

constructor(c) { this.client = c; }
}
113 changes: 80 additions & 33 deletions structures/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import axios from 'axios';
import { exit } from 'process';
import Guild from '../guilds/Guild.js';
import user from '../messages/User.js';
import { Thread } from '../guilds/ThreadManager.js';



Expand Down Expand Up @@ -39,6 +40,8 @@ export class Client extends EventEmitter {
/** @type {Map<String, Guild>} */
guilds;

/** @type {import('axios').AxiosInstance} */
axiosCustom;

/**
* @param {opts} input
Expand Down Expand Up @@ -74,16 +77,24 @@ export class Client extends EventEmitter {
this.emit("messageRecieved", msg);
}

ready() {
ready(response) {
this.user_profile = response.profile;
this.user_settings = response.config;
this.id = response.profile.id;
this.emit('ready');
}

customError(err) {
this.emit('error', err);
}
customError(err) { this.emit('error', err); }

interactionRecieved(interaction) {
this.emit('interactionRecieved', interaction);
interactionRecieved(data, response) {
if (data["d"]["member"] && data["d"]["member"]["id"] != this.user_profile.id) {
response.interaction.guild = this.guilds.get(response.interaction.guild_id);
response.interaction.user = new user(data["d"]["member"]["user"]);
this.emit('interactionRecieved', response.interaction);
}
else if (data["d"]["user"]["id"] != this.user_profile.id) {
this.emit('interactionRecieved', response.interaction);
}
}

/**
Expand All @@ -103,6 +114,30 @@ export class Client extends EventEmitter {
guildMemberAdd(member) {
this.emit('guildMemberAdd', member);
}


threadCreate(threadRaw) {
const guild = this.guilds.get(threadRaw.guild_id);
const newThread = new Thread(threadRaw, guild, this.#token);
guild.threads.cache.set(newThread.id, newThread);
}

threadEdit(threadRaw) {
if (!this.guilds.has(threadRaw.guild_id)) return;
const guild = this.guilds.get(threadRaw.guild_id);

if (!guild.threads.cache.has(threadRaw.id)) return;
console.log(threadRaw);
}

ThreadDelete(threadRaw) {
if (!this.guilds.has(threadRaw.guild_id)) return;
const guild = this.guilds.get(threadRaw.guild_id);

if (!guild.threads.cache.has(threadRaw.id)) return;
guild.threads.cache.delete()
}

//#endregion


Expand All @@ -111,6 +146,10 @@ export class Client extends EventEmitter {
*/
async login(token, isUser = false) {
if (!isUser) token = "Bot " + token;
this.axiosCustom = axios.create({
baseURL: "https://discord.com/api/",
headers: { Authorization: this.#token }
});

return new Promise((resolve, reject) => {
this.ws = new WebSocket("wss://gateway.discord.gg/?v=10&encoding=json");
Expand Down Expand Up @@ -143,35 +182,43 @@ export class Client extends EventEmitter {
const data = JSON.parse(msg.toString());
const response = await handleResponses(data, token, this.id);

if (response.op == 10) { this.#startHeartBeat(response.heartBeat, token); }
if (response.op == 10) { return 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"]["member"] && data["d"]["member"]["id"] != this.user_profile.id) {
response.interaction.guild = this.guilds.get(response.interaction.guild_id);
response.interaction.user = new user(data["d"]["member"]["user"]);
this.interactionRecieved(response.interaction);
}
else if (data["d"]["user"]["id"] != this.user_profile.id) {
this.interactionRecieved(response.interaction);
}
switch(response.t) {
case gateWayEvents.Ready: this.ready(response);
break;

case 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);
}
break;

case gateWayEvents.InteractionCreate: this.interactionRecieved(data, response);
break;

case gateWayEvents.GuildCreate: this.guildCreate(response.guild);
break;

case gateWayEvents.GuildDelete: this.guildDelete(response.guild);
break;

case gateWayEvents.GuildMemberAdd: this.guildMemberAdd(response.member);
break;

case gateWayEvents.ThreadCreate: this.threadCreate(response.threadRaw);
break;

case gateWayEvents.ThreadUpdate: this.threadEdit(response.threadRaw);
break;

case gateWayEvents.ThreadDelete: this.ThreadDelete(response.threadRaw);
break;

default: // console.log(response.t);
}
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 {
// commmented to avoid heartbeats
// console.log(response.t);
Expand Down
48 changes: 31 additions & 17 deletions structures/client/handleEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,39 @@ export default async function handleEvents(msgObj, token, id) {
const t = msgObj["t"];

if (op == 10) return resolve({op: op, heartBeat: msgObj["d"]["heartbeat_interval"]});
else if (op != 0) { return resolve(false); } // https://discord.com/developers/docs/topics/opcodes-and-status-codes#gateway-gateway-opcodes

else if (op != 0) { resolve(false); }
switch(t) {
case gateWayEvents.Ready:
resolve({op: op, t: t, config: msgObj["d"]["user_settings"], profile: msgObj["d"]["user"]}); //, guilds: msgObj["d"]["guilds"]
break;

else if (t == gateWayEvents.Ready) {
resolve({op: op, t: t, config: msgObj["d"]["user_settings"], profile: msgObj["d"]["user"]}); //, guilds: msgObj["d"]["guilds"]
}
else if (t == gateWayEvents.MessageCreate) {
const msg = new message(msgObj["d"], token);
resolve({op: op, t: t, message: msg});
}
else if (t == gateWayEvents.InteractionCreate) {
resolve({op: op, t: t, interaction: new Interaction(msgObj["d"], token, id)});
}
else if (t == gateWayEvents.GuildCreate) {
resolve({op: op, t: t, guild: new Guild(msgObj["d"], token)});
}

else {
// console.log(t);
case gateWayEvents.MessageCreate:
const msg = new message(msgObj["d"], token);
resolve({op: op, t: t, message: msg});
break;

case gateWayEvents.InteractionCreate:
resolve({op: op, t: t, interaction: new Interaction(msgObj["d"], token, id)});
break;

case gateWayEvents.GuildCreate:
resolve({op: op, t: t, guild: new Guild(msgObj["d"], token)});
break;

case gateWayEvents.ThreadCreate:
resolve({op: op, t: t, threadRaw: msgObj["d"]});
break;

case gateWayEvents.ThreadDelete:
resolve({op: op, t: t, threadRaw: msgObj["d"]});
break;

case gateWayEvents.ThreadUpdate:
resolve({op: op, t: t, threadRaw: msgObj["d"]});
break;

default: console.log(t);
}
});
}
5 changes: 4 additions & 1 deletion structures/guilds/Channel.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import axios from 'axios';
import {message} from '../messages/message.js';
import { BaseStruct } from '../baseStruct.js';

export class Channel {
export class Channel extends BaseStruct {
/** @type {String} */
id;

Expand Down Expand Up @@ -56,6 +57,8 @@ export class Channel {


constructor(channel, guild, token = null) {
super();

this.#token = token;
for (const k in this) {
if (channel[k]) this[k] = channel[k];
Expand Down
7 changes: 5 additions & 2 deletions structures/guilds/GuildChannelManager.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import axios from 'axios';
import { Channel } from './Channel.js';
import Guild from './Guild.js';
import { BaseStruct } from '../baseStruct.js';

export class GuildChannelManager {

export class GuildChannelManager extends BaseStruct {
#token;

/** @type {Guild} */
Expand Down Expand Up @@ -91,8 +93,9 @@ export class GuildChannelManager {
});
}


constructor(token, guild, newCache) {
super();

this.#token = token;
this.guild = guild;
this.cache = newCache;
Expand Down
4 changes: 3 additions & 1 deletion structures/guilds/GuildStickers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import axios from "axios";
import user from "../messages/User.js";
import Guild from "./Guild.js";
import { BaseStruct } from "../baseStruct.js";

export class guildSticker {

export class guildSticker extends BaseStruct {
#token;

/** @type {String} */
Expand Down
80 changes: 80 additions & 0 deletions structures/guilds/ThreadManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import axios from "axios";
import { Channel } from "./Channel.js";

export class Thread extends Channel {
/** @type {Number} */
total_message_sent;

/** @type {{locked: Boolean, create_timestamp: String, auto_archive_duration: Number, archived: Boolean, archive_timestamp: String}} */
thread_metadata;

/** @type {Number} */
message_count;


constructor(o, guild, token) {
super(o, guild, token);
this.last_message_sent = o['total_message_sent'];
this.thread_metadata = o['thread_metadata'];
this.message_count = o['thread_metadata'];
}
}


export class ThreadManager {
#token;
/** @type {Map<String, Thread>} */
cache;

/**
* @returns {Thread | Boolean} The deleted thread or false if the operation failed
* @param {Thread | String} threadId
*/
has(thread) {
const tid = (typeof thread == 'string') ? thread : thread.id;
if (!this.cache.has(tid)) return false;
const threadToDel = this.cache.get(tid);
this.cache.delete(tid);
return threadToDel;
}


/**
* @description returns the deleted thread if successful
* @param {Thread} thread
* @param {String} reason
* @returns {promise<Channel>}
*/
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);
const newChannel = this.cache.get(thread.id);
this.cache.delete(thread.id);
resolve(newChannel);
} catch (err) {
throw err;
}
});
}



constructor(o, guild, token) {
this.cache = new Map();
this.#token = token;

for (const k of o) {
const newThread = new Thread(k, guild, token);
this.cache.set(newThread.id, newThread);
}
}
}
Loading

0 comments on commit e6b32cc

Please sign in to comment.