Initial code commit

This commit is contained in:
ION606
2023-03-18 18:02:17 -04:00
parent 24316888ec
commit 35b993b3fe
12 changed files with 718 additions and 0 deletions
+130
View File
@@ -0,0 +1,130 @@
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');
class Client extends EventEmitter {
/** @type {WebSocketClient} */
ws;
/** @type {Number} */
heartBeatInterval;
/** @type {Array<opts.intents>} */
gwintents;
/** @type {String} */
#token;
/** @type {WebSocketConnection}*/
connection;
/** @type {Object} */
user_settings;
/** @type {Object} */
user_profile;
/**
* @param {opts} input
*/
constructor(input) {
super();
this.gwintents = input.intents;
}
/**
* @param {Number} hbint
*/
async #startHeartBeat(hbint)
{
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,
intents: this.gwintents.value, //61440,
properties: {
os: "linux",
browser: "ion_",
device: "my_library"
}
}
};
this.connection.send(JSON.stringify(idObj));
}
messageRecieved(msg) {
this.emit("messageRecieved", msg);
}
ready() {
this.emit('ready');
}
customError(err) {
this.emit('error', err);
}
/**
* @param {String} token
*/
async login(token) {
this.ws = new WebSocketClient({maxReceivedFrameSize: Infinity});
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);
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.ready();
}
else if (response.t == gateWayEvents.MessageCreate) {
this.messageRecieved(response.message);
}
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}`);
});
this.connection = connection;
});
this.ws.on('connectFailed', (err) => { console.error(err); })
this.ws.connect("wss://gateway.discord.gg/?v=10&encoding=json");
}
}
//All client properties will be re-routed through this export
module.exports = { Client, gateWayIntents }
+7
View File
@@ -0,0 +1,7 @@
const intents = require('../gateway/intents.js');
module.exports = {
/**@type {Array<intents>} */
intents: []
}
+29
View File
@@ -0,0 +1,29 @@
const { exit } = require('process');
const gateWayEvents = require('../gateway/dispatch.js');
const { message } = require('../messages/message.js');
/**
* @description Returns true if this is a READY event and false otherwise
* @param {Object} msg
* @returns {Promise<Boolean>}
*/
module.exports = async function handleEvents(msgObj) {
return new Promise((resolve, reject) => {
const op = msgObj["op"];
const t = msgObj["t"];
if (op == 10) return resolve({op: op, heartBeat: msgObj["d"]["heartbeat_interval"]});
if (op == 0 && t == gateWayEvents.Ready) {
resolve({op: op, t: t, config: msgObj["d"]["user_settings"], profile: msgObj["d"]["user"] });
} else if (t == gateWayEvents.MessageCreate) {
const msg = new message(msgObj["d"]);
resolve({op: op, t: t, message: msg});
}
else {
// console.log(t);
}
});
}
+62
View File
@@ -0,0 +1,62 @@
module.exports = Object.freeze({
ApplicationCommandPermissionsUpdate: "APPLICATION_COMMAND_PERMISSIONS_UPDATE",
ChannelCreate: "CHANNEL_CREATE",
ChannelDelete: "CHANNEL_DELETE",
ChannelPinsUpdate: "CHANNEL_PINS_UPDATE",
ChannelUpdate: "CHANNEL_UPDATE",
GuildBanAdd: "GUILD_BAN_ADD",
GuildBanRemove: "GUILD_BAN_REMOVE",
GuildCreate: "GUILD_CREATE",
GuildDelete: "GUILD_DELETE",
GuildEmojisUpdate: "GUILD_EMOJIS_UPDATE",
GuildIntegrationsUpdate: "GUILD_INTEGRATIONS_UPDATE",
GuildMemberAdd: "GUILD_MEMBER_ADD",
GuildMemberRemove: "GUILD_MEMBER_REMOVE",
GuildMembersChunk: "GUILD_MEMBERS_CHUNK",
GuildMemberUpdate: "GUILD_MEMBER_UPDATE",
GuildRoleCreate: "GUILD_ROLE_CREATE",
GuildRoleDelete: "GUILD_ROLE_DELETE",
GuildRoleUpdate: "GUILD_ROLE_UPDATE",
GuildStickersUpdate: "GUILD_STICKERS_UPDATE",
GuildUpdate: "GUILD_UPDATE",
IntegrationCreate: "INTEGRATION_CREATE",
IntegrationDelete: "INTEGRATION_DELETE",
IntegrationUpdate: "INTEGRATION_UPDATE",
InteractionCreate: "INTERACTION_CREATE",
InviteCreate: "INVITE_CREATE",
InviteDelete: "INVITE_DELETE",
MessageCreate: "MESSAGE_CREATE",
MessageDelete: "MESSAGE_DELETE",
MessageDeleteBulk: "MESSAGE_DELETE_BULK",
MessageReactionAdd: "MESSAGE_REACTION_ADD",
MessageReactionRemove: "MESSAGE_REACTION_REMOVE",
MessageReactionRemoveAll: "MESSAGE_REACTION_REMOVE_ALL",
MessageReactionRemoveEmoji: "MESSAGE_REACTION_REMOVE_EMOJI",
MessageUpdate: "MESSAGE_UPDATE",
PresenceUpdate: "PRESENCE_UPDATE",
StageInstanceCreate: "STAGE_INSTANCE_CREATE",
StageInstanceDelete: "STAGE_INSTANCE_DELETE",
StageInstanceUpdate: "STAGE_INSTANCE_UPDATE",
Ready: "READY",
Resumed: "RESUMED",
ThreadCreate: "THREAD_CREATE",
ThreadDelete: "THREAD_DELETE",
ThreadListSync: "THREAD_LIST_SYNC",
ThreadMembersUpdate: "THREAD_MEMBERS_UPDATE",
ThreadMemberUpdate: "THREAD_MEMBER_UPDATE",
ThreadUpdate: "THREAD_UPDATE",
TypingStart: "TYPING_START",
UserUpdate: "USER_UPDATE",
VoiceServerUpdate: "VOICE_SERVER_UPDATE",
VoiceStateUpdate: "VOICE_STATE_UPDATE",
WebhooksUpdate: "WEBHOOKS_UPDATE",
GuildScheduledEventCreate: "GUILD_SCHEDULED_EVENT_CREATE",
GuildScheduledEventUpdate: "GUILD_SCHEDULED_EVENT_UPDATE",
GuildScheduledEventDelete: "GUILD_SCHEDULED_EVENT_DELETE",
GuildScheduledEventUserAdd: "GUILD_SCHEDULED_EVENT_USER_ADD",
GuildScheduledEventUserRemove: "GUILD_SCHEDULED_EVENT_USER_REMOVE",
AutoModerationRuleCreate: "AUTO_MODERATION_RULE_CREATE",
AutoModerationRuleUpdate: "AUTO_MODERATION_RULE_UPDATE",
AutoModerationRuleDelete: "AUTO_MODERATION_RULE_DELETE",
AutoModerationActionExecution: "AUTO_MODERATION_ACTION_EXECUTION"
});
+46
View File
@@ -0,0 +1,46 @@
module.exports = Object.freeze({
Guilds: 1 << 0,
GuildMembers: 1 << 1,
GuildModeration: 1 << 2,
GuildEmojisStickers: 1 << 3,
GuildIntegrations: 1 << 4,
GuildWebhooks: 1 << 5,
GuildInvites: 1 << 6,
GuildVoiceStates: 1 << 7,
GuildPresences: 1 << 8,
GuildMessages: 1 << 9,
GuildMessageReactions: 1 << 10,
GuildMessageTyping: 1 << 11,
DirectMessages: 1 << 12,
DirectMessageReactions: 1 << 13,
DirectMessageTyping: 1 << 14,
MessageContent: 1 << 15,
GuildScheduledEvents: 1 << 16,
AutoModerationConfiguration: 1 << 20,
AutoModerationExecution: 1 << 21,
});
/*
module.exports = Object.freeze(class intents {
constructor() {
Guilds = 1 << 0,
GuildMembers = 1 << 1,
GuildModeration = 1 << 2,
GuildEmojisStickers = 1 << 3,
GuildIntegrations = 1 << 4,
GuildWebhooks = 1 << 5,
GuildInvites = 1 << 6,
GuildVoiceStates = 1 << 7,
GuildPresences = 1 << 8,
GuildMessages = 1 << 9,
GuildMessageReactions = 1 << 10,
GuildMessageTyping = 1 << 11,
DirectMessages = 1 << 12,
DirectMessageReactions = 1 << 13,
DirectMessageTyping = 1 << 14,
MessageContent = 1 << 15,
GuildScheduledEvents = 1 << 16,
AutoModerationConfiguration = 1 << 20,
AutoModerationExecution = 1 << 21
}
});*/
+17
View File
@@ -0,0 +1,17 @@
const axios = require("axios").create({baseUrl: "https://jsonplaceholder.typicode.com/"});
class Channel {
/** @type {String} */
id;
/**
* @param {Object} inp
*/
async send(inp) {
const toSend = (typeof inp == 'string') ? {content: inp} : inp;
const response = axios.post(`${id}/messages`, {
// Authorization:
});
}
}
+52
View File
@@ -0,0 +1,52 @@
const messageChannelTypes = require('./messageChannelTypes.js');
class message {
/** @type {Object} */
author;
/** @type {Object} */
channel_id;
/** @type {Object[]} */
mentions;
/** @type {Object} */
member;
/** @type {String} */
id;
/** @type {Object} */
content;
/** @type {Object[]} */
attachments;
/** @type {String} */
guild_id;
/** @type {Object} */
type;
/**
* @param {Object} msgRaw
*/
constructor(msgRaw) {
for (const k in this) {
if (msgRaw[k] != undefined) {
if (k == 'type') {
this.type = (msgRaw['guild_id']) ? msgRaw[k] : 1;
} else {
this[k] = msgRaw[k];
}
}
}
}
}
module.exports = { message, messageChannelTypes };
@@ -0,0 +1,81 @@
//Blatantly stolen from https://github.com/discordjs/discord-api-types/blob/main/gateway/v10.ts
module.exports = Object.freeze({
/**
* A text channel within a guild
*/
GuildText: 0,
/**
* A direct message between users
*/
DM: 1,
/**
* A voice channel within a guild
*/
GuildVoice: 2,
/**
* A direct message between multiple users
*/
GroupDM: 3,
/**
* An organizational category that contains up to 50 channels
*
* See https://support.discord.com/hc/articles/115001580171
*/
GuildCategory: 4,
/**
* A channel that users can follow and crosspost into their own guild
*
* See https://support.discord.com/hc/articles/360032008192
*/
GuildAnnouncement: 5,
/**
* A temporary sub-channel within a Guild Announcement channel
*/
AnnouncementThread: 10,
/**
* A temporary sub-channel within a Guild Text or Guild Forum channel
*/
PublicThread: 11,
/**
* A temporary sub-channel within a Guild Text channel that is only viewable by those invited and those with the Manage Threads permission
*/
PrivateThread: 12,
/**
* A voice channel for hosting events with an audience
*
* See https://support.discord.com/hc/articles/1500005513722
*/
GuildStageVoice: 13,
/**
* The channel in a Student Hub containing the listed servers
*
* See https://support.discord.com/hc/articles/4406046651927
*/
GuildDirectory: 14,
/**
* A channel that can only contain threads
*/
GuildForum: 15,
/**
* A channel that users can follow and crosspost into their own guild
*
* @deprecated This is the old name for {@apilink ChannelType#GuildAnnouncement}
*
* See https://support.discord.com/hc/articles/360032008192
*/
GuildNews: 5,
/**
* A temporary sub-channel within a Guild Announcement channel
*
* @deprecated This is the old name for {@apilink ChannelType#AnnouncementThread}
*/
GuildNewsThread: 10,
/**
* A temporary sub-channel within a Guild Text channel
*
* @deprecated This is the old name for {@apilink ChannelType#PublicThread}
*/
GuildPublicThread: 11,
});