nonode-discord
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -11,5 +11,13 @@ "use strict"; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Client = exports.ChannelTypes = exports.AppCommandOptionType = exports.AppCommandType = exports.PresenceTypes = void 0; | ||
const verify_1 = require("./verify"); | ||
exports.Client = exports.Intents = exports.EventTypes = exports.ChannelTypes = exports.AppCommandOptionType = exports.AppCommandType = exports.PresenceTypes = void 0; | ||
const verify_1 = require("./util/verify"); | ||
const error_1 = require("./util/error"); | ||
const endpoints_1 = require("./util/endpoints"); | ||
const request_1 = require("util/request"); | ||
const ws_1 = __importDefault(require("ws")); | ||
const heartbeat_1 = require("util/heartbeat"); | ||
var PresenceTypes; | ||
@@ -56,14 +64,113 @@ (function (PresenceTypes) { | ||
})(ChannelTypes || (exports.ChannelTypes = ChannelTypes = {})); | ||
var EventTypes; | ||
(function (EventTypes) { | ||
EventTypes["Ready"] = "READY"; | ||
EventTypes["ChannelCreate"] = "CHANNEL_CREATE"; | ||
EventTypes["ChannelUpdate"] = "CHANNEL_UPDATE"; | ||
EventTypes["ChannelDelete"] = "CHANNEL_DELETE"; | ||
EventTypes["MessageCreate"] = "MESSAGE_CREATE"; | ||
EventTypes["MessageUpdate"] = "MESSAGE_UPDATE"; | ||
EventTypes["MessageDelete"] = "MESSAGE_DELETE"; | ||
})(EventTypes || (exports.EventTypes = EventTypes = {})); | ||
var Intents; | ||
(function (Intents) { | ||
Intents[Intents["guilds"] = 1] = "guilds"; | ||
Intents[Intents["guildMembers"] = 2] = "guildMembers"; | ||
Intents[Intents["guildBans"] = 4] = "guildBans"; | ||
Intents[Intents["guildEmojisAndStickers"] = 8] = "guildEmojisAndStickers"; | ||
Intents[Intents["guildIntegrations"] = 16] = "guildIntegrations"; | ||
Intents[Intents["guildWebhooks"] = 32] = "guildWebhooks"; | ||
Intents[Intents["guildInvites"] = 64] = "guildInvites"; | ||
Intents[Intents["guildVoiceStates"] = 128] = "guildVoiceStates"; | ||
Intents[Intents["guildPresences"] = 256] = "guildPresences"; | ||
Intents[Intents["guildMessages"] = 512] = "guildMessages"; | ||
Intents[Intents["guildMessageReactions"] = 1024] = "guildMessageReactions"; | ||
Intents[Intents["guildMessageTypings"] = 2048] = "guildMessageTypings"; | ||
Intents[Intents["directMessages"] = 4096] = "directMessages"; | ||
Intents[Intents["directMessageReactions"] = 8192] = "directMessageReactions"; | ||
Intents[Intents["directMessageTypings"] = 16384] = "directMessageTypings"; | ||
Intents[Intents["messageContent"] = 32768] = "messageContent"; | ||
Intents[Intents["guildScheduledEvents"] = 65536] = "guildScheduledEvents"; | ||
})(Intents || (exports.Intents = Intents = {})); | ||
class Client { | ||
constructor(options) { | ||
this._events = {}; | ||
if (!options.token) | ||
throw new Error("No token in options."); | ||
(0, error_1.error)("No token in options."); | ||
if (!options.clientId) | ||
throw new Error("No clientId in options."); | ||
(0, error_1.error)("No clientId in options."); | ||
if (!options.intents[0]) | ||
(0, error_1.error)("No intents in options."); | ||
options.presence = options.presence || { | ||
type: PresenceTypes.online, | ||
status: "This bot is now ready to rock and roll.", | ||
afk: false, | ||
}; | ||
let intents = 0; | ||
options.intents.forEach((i) => (intents += i)); | ||
options.intents = [intents]; | ||
this.options = options; | ||
const ws = new ws_1.default("wss://gateway.discord.gg/?v=10&encoding=json"); | ||
ws.on("open", (data) => { | ||
ws.send(JSON.stringify({ | ||
op: 2, | ||
d: { | ||
token: this.options.token, | ||
intents: this.options.intents[0], | ||
properties: { | ||
os: "linux", | ||
browser: "chrome", | ||
device: "chrome", | ||
}, | ||
presence: { | ||
activities: [ | ||
{ | ||
name: this.options.presence.status, | ||
type: 0, | ||
}, | ||
], | ||
status: this.options.presence.type, | ||
afk: this.options.presence.afk, | ||
}, | ||
}, | ||
})); | ||
}); | ||
ws.on("message", (dataStr) => { | ||
const { d, t, s, op } = JSON.parse(dataStr); | ||
switch (op) { | ||
case 10: | ||
(0, heartbeat_1.doHeartbeat)(d.heartbeat_interval, ws, s ? s : null); | ||
break; | ||
case 1: | ||
(0, heartbeat_1.doHeartbeat)(null, ws, s); | ||
break; | ||
} | ||
this.emit(toCamelCase(t), d); | ||
}); | ||
} | ||
on(name, listener) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!this._events[name]) { | ||
this._events[name] = []; | ||
} | ||
if (typeof listener != "function") | ||
(0, error_1.error)("Event listener cannot be anything but a function!"); | ||
this._events[name].push(listener); | ||
}); | ||
} | ||
emit(name, data) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
this._events[name].forEach((funcs) => funcs()); | ||
}); | ||
} | ||
removeEvent(name) { | ||
if (!this._events[name]) | ||
(0, error_1.error)(`No event found with the name ${name}`); | ||
this._events[name] = null; | ||
} | ||
removeListener(name, listener) { | ||
if (!this._events[name]) | ||
(0, error_1.error)(`No event found with the name ${name}`); | ||
this._events[name].filter((l) => l !== listener); | ||
} | ||
verifyRequest(rawBody, signature, timestamp) { | ||
@@ -74,12 +181,22 @@ return __awaiter(this, void 0, void 0, function* () { | ||
} | ||
addInteraction(global, execute, options) { | ||
addCommand(global, execute, options) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!global && !options.guildId) { | ||
throw new Error("Cant add a guild command without guildId!"); | ||
} | ||
if (options.name.search(/^[-_\p{L}\p{N}\p{sc=Deva}\p{sc=Thai}]{132}$/u) == -1) | ||
(0, error_1.error)(`Invalid name for app command, "${options.name}"`); | ||
for (const option of options.options) { | ||
if (option.name.search(/^[-_\p{L}\p{N}\p{sc=Deva}\p{sc=Thai}]{1,32}$/u)) | ||
continue; | ||
(0, error_1.error)("Invalid name for app command options!"); | ||
} | ||
const cmd = { | ||
name: options.name, | ||
description: options.description, | ||
name: options.name || (0, error_1.error)("No name provided for command!"), | ||
description: options.description || (0, error_1.error)("No description provided for command!"), | ||
required: options.required == false ? false : true, | ||
max_length: options.max_length || 6000, | ||
min_length: options.min_length || 0, | ||
max_length: options.maxLength || 6000, | ||
min_length: options.minLength || 0, | ||
nsfw: options.nsfw == false ? false : true, | ||
guild_id: options.guild_id || null, | ||
guild_id: options.guildId || null, | ||
options: options.options || null, | ||
@@ -105,12 +222,10 @@ type: options.type || AppCommandType.chat, | ||
continue; | ||
if (!cmd.global && !cmd.cmd.guild_id) | ||
throw new Error("No guild_id specified for local command!"); | ||
const url = cmd.global | ||
? `https://discord.com/api/v10/applications/${this.options.clientId}/commands` | ||
: `https://discord.com/api/v10/applications/${this.options.clientId}/guilds/${cmd.cmd.guild_id}/commands`; | ||
fetch(url, { | ||
body: JSON.stringify(cmd.cmd), | ||
headers: { | ||
if (cmd.global) { | ||
yield (0, request_1.request)(endpoints_1.endpoints.interaction.register.global.method, endpoints_1.endpoints.interaction.register.global.url(this.options.clientId), cmd, { | ||
Authorization: `Bot ${this.options.token}`, | ||
}, | ||
}); | ||
continue; | ||
} | ||
yield (0, request_1.request)(endpoints_1.endpoints.interaction.register.guild.method, endpoints_1.endpoints.interaction.register.guild.url(this.options.clientId, cmd.cmd.guild_id), cmd, { | ||
Authorization: `Bot ${this.options.token}`, | ||
}); | ||
@@ -117,0 +232,0 @@ } |
@@ -1,2 +0,7 @@ | ||
import { verifyKey } from "./verify"; | ||
import { verifyKey } from "./util/verify"; | ||
import { error } from "./util/error"; | ||
import { endpoints } from "./util/endpoints"; | ||
import { request } from "util/request"; | ||
import WebSocket from "ws"; | ||
import { doHeartbeat } from "util/heartbeat"; | ||
export var PresenceTypes; | ||
@@ -43,28 +48,133 @@ (function (PresenceTypes) { | ||
})(ChannelTypes || (ChannelTypes = {})); | ||
export var EventTypes; | ||
(function (EventTypes) { | ||
EventTypes["Ready"] = "READY"; | ||
EventTypes["ChannelCreate"] = "CHANNEL_CREATE"; | ||
EventTypes["ChannelUpdate"] = "CHANNEL_UPDATE"; | ||
EventTypes["ChannelDelete"] = "CHANNEL_DELETE"; | ||
EventTypes["MessageCreate"] = "MESSAGE_CREATE"; | ||
EventTypes["MessageUpdate"] = "MESSAGE_UPDATE"; | ||
EventTypes["MessageDelete"] = "MESSAGE_DELETE"; | ||
})(EventTypes || (EventTypes = {})); | ||
export var Intents; | ||
(function (Intents) { | ||
Intents[Intents["guilds"] = 1] = "guilds"; | ||
Intents[Intents["guildMembers"] = 2] = "guildMembers"; | ||
Intents[Intents["guildBans"] = 4] = "guildBans"; | ||
Intents[Intents["guildEmojisAndStickers"] = 8] = "guildEmojisAndStickers"; | ||
Intents[Intents["guildIntegrations"] = 16] = "guildIntegrations"; | ||
Intents[Intents["guildWebhooks"] = 32] = "guildWebhooks"; | ||
Intents[Intents["guildInvites"] = 64] = "guildInvites"; | ||
Intents[Intents["guildVoiceStates"] = 128] = "guildVoiceStates"; | ||
Intents[Intents["guildPresences"] = 256] = "guildPresences"; | ||
Intents[Intents["guildMessages"] = 512] = "guildMessages"; | ||
Intents[Intents["guildMessageReactions"] = 1024] = "guildMessageReactions"; | ||
Intents[Intents["guildMessageTypings"] = 2048] = "guildMessageTypings"; | ||
Intents[Intents["directMessages"] = 4096] = "directMessages"; | ||
Intents[Intents["directMessageReactions"] = 8192] = "directMessageReactions"; | ||
Intents[Intents["directMessageTypings"] = 16384] = "directMessageTypings"; | ||
Intents[Intents["messageContent"] = 32768] = "messageContent"; | ||
Intents[Intents["guildScheduledEvents"] = 65536] = "guildScheduledEvents"; | ||
})(Intents || (Intents = {})); | ||
export class Client { | ||
options; | ||
cmds; | ||
_events = {}; | ||
constructor(options) { | ||
if (!options.token) | ||
throw new Error("No token in options."); | ||
error("No token in options."); | ||
if (!options.clientId) | ||
throw new Error("No clientId in options."); | ||
error("No clientId in options."); | ||
if (!options.intents[0]) | ||
error("No intents in options."); | ||
options.presence = options.presence || { | ||
type: PresenceTypes.online, | ||
status: "This bot is now ready to rock and roll.", | ||
afk: false, | ||
}; | ||
let intents = 0; | ||
options.intents.forEach((i) => (intents += i)); | ||
options.intents = [intents]; | ||
this.options = options; | ||
const ws = new WebSocket("wss://gateway.discord.gg/?v=10&encoding=json"); | ||
ws.on("open", (data) => { | ||
ws.send(JSON.stringify({ | ||
op: 2, | ||
d: { | ||
token: this.options.token, | ||
intents: this.options.intents[0], | ||
properties: { | ||
os: "linux", | ||
browser: "chrome", | ||
device: "chrome", | ||
}, | ||
presence: { | ||
activities: [ | ||
{ | ||
name: this.options.presence.status, | ||
type: 0, | ||
}, | ||
], | ||
status: this.options.presence.type, | ||
afk: this.options.presence.afk, | ||
}, | ||
}, | ||
})); | ||
}); | ||
ws.on("message", (dataStr) => { | ||
const { d, t, s, op } = JSON.parse(dataStr); | ||
switch (op) { | ||
case 10: | ||
doHeartbeat(d.heartbeat_interval, ws, s ? s : null); | ||
break; | ||
case 1: | ||
doHeartbeat(null, ws, s); | ||
break; | ||
} | ||
this.emit(toCamelCase(t), d); | ||
}); | ||
} | ||
async on(name, listener) { | ||
if (!this._events[name]) { | ||
this._events[name] = []; | ||
} | ||
if (typeof listener != "function") | ||
error("Event listener cannot be anything but a function!"); | ||
this._events[name].push(listener); | ||
} | ||
async emit(name, data) { | ||
this._events[name].forEach((funcs) => funcs()); | ||
} | ||
removeEvent(name) { | ||
if (!this._events[name]) | ||
error(`No event found with the name ${name}`); | ||
this._events[name] = null; | ||
} | ||
removeListener(name, listener) { | ||
if (!this._events[name]) | ||
error(`No event found with the name ${name}`); | ||
this._events[name].filter((l) => l !== listener); | ||
} | ||
async verifyRequest(rawBody, signature, timestamp) { | ||
return verifyKey(rawBody, signature, timestamp, this.options.clientId); | ||
} | ||
async addInteraction(global, execute, options) { | ||
async addCommand(global, execute, options) { | ||
if (!global && !options.guildId) { | ||
throw new Error("Cant add a guild command without guildId!"); | ||
} | ||
if (options.name.search(/^[-_\p{L}\p{N}\p{sc=Deva}\p{sc=Thai}]{132}$/u) == -1) | ||
error(`Invalid name for app command, "${options.name}"`); | ||
for (const option of options.options) { | ||
if (option.name.search(/^[-_\p{L}\p{N}\p{sc=Deva}\p{sc=Thai}]{1,32}$/u)) | ||
continue; | ||
error("Invalid name for app command options!"); | ||
} | ||
const cmd = { | ||
name: options.name, | ||
description: options.description, | ||
name: options.name || error("No name provided for command!"), | ||
description: options.description || error("No description provided for command!"), | ||
required: options.required == false ? false : true, | ||
max_length: options.max_length || 6000, | ||
min_length: options.min_length || 0, | ||
max_length: options.maxLength || 6000, | ||
min_length: options.minLength || 0, | ||
nsfw: options.nsfw == false ? false : true, | ||
guild_id: options.guild_id || null, | ||
guild_id: options.guildId || null, | ||
options: options.options || null, | ||
@@ -88,12 +198,10 @@ type: options.type || AppCommandType.chat, | ||
continue; | ||
if (!cmd.global && !cmd.cmd.guild_id) | ||
throw new Error("No guild_id specified for local command!"); | ||
const url = cmd.global | ||
? `https://discord.com/api/v10/applications/${this.options.clientId}/commands` | ||
: `https://discord.com/api/v10/applications/${this.options.clientId}/guilds/${cmd.cmd.guild_id}/commands`; | ||
fetch(url, { | ||
body: JSON.stringify(cmd.cmd), | ||
headers: { | ||
if (cmd.global) { | ||
await request(endpoints.interaction.register.global.method, endpoints.interaction.register.global.url(this.options.clientId), cmd, { | ||
Authorization: `Bot ${this.options.token}`, | ||
}, | ||
}); | ||
continue; | ||
} | ||
await request(endpoints.interaction.register.guild.method, endpoints.interaction.register.guild.url(this.options.clientId, cmd.cmd.guild_id), cmd, { | ||
Authorization: `Bot ${this.options.token}`, | ||
}); | ||
@@ -100,0 +208,0 @@ } |
@@ -38,5 +38,34 @@ export declare enum PresenceTypes { | ||
} | ||
export declare enum EventTypes { | ||
"Ready" = "READY", | ||
"ChannelCreate" = "CHANNEL_CREATE", | ||
"ChannelUpdate" = "CHANNEL_UPDATE", | ||
"ChannelDelete" = "CHANNEL_DELETE", | ||
"MessageCreate" = "MESSAGE_CREATE", | ||
"MessageUpdate" = "MESSAGE_UPDATE", | ||
"MessageDelete" = "MESSAGE_DELETE" | ||
} | ||
export declare enum Intents { | ||
"guilds" = 1, | ||
"guildMembers" = 2, | ||
"guildBans" = 4, | ||
"guildEmojisAndStickers" = 8, | ||
"guildIntegrations" = 16, | ||
"guildWebhooks" = 32, | ||
"guildInvites" = 64, | ||
"guildVoiceStates" = 128, | ||
"guildPresences" = 256, | ||
"guildMessages" = 512, | ||
"guildMessageReactions" = 1024, | ||
"guildMessageTypings" = 2048, | ||
"directMessages" = 4096, | ||
"directMessageReactions" = 8192, | ||
"directMessageTypings" = 16384, | ||
"messageContent" = 32768, | ||
"guildScheduledEvents" = 65536 | ||
} | ||
export interface PresenceOptions { | ||
type: PresenceTypes; | ||
status: string; | ||
afk: boolean; | ||
} | ||
@@ -46,2 +75,3 @@ export interface ClientOptions { | ||
clientId: string; | ||
intents: Intents[]; | ||
presence?: PresenceOptions; | ||
@@ -61,9 +91,9 @@ } | ||
options?: AppCommandOptions[]; | ||
channel_types?: ChannelTypes[]; | ||
min_length?: number; | ||
max_length?: number; | ||
channelTypes?: ChannelTypes[]; | ||
minLength?: number; | ||
maxLength?: number; | ||
} | ||
export interface AppCommand { | ||
type?: AppCommandType; | ||
guild_id?: string; | ||
guildId?: string; | ||
options?: AppCommandOptions[]; | ||
@@ -73,4 +103,4 @@ name: string; | ||
required?: boolean; | ||
min_length?: number; | ||
max_length?: number; | ||
minLength?: number; | ||
maxLength?: number; | ||
nsfw?: boolean; | ||
@@ -81,6 +111,11 @@ } | ||
private cmds; | ||
private _events; | ||
constructor(options: ClientOptions); | ||
on(name: EventTypes, listener: () => any): Promise<void>; | ||
private emit; | ||
removeEvent(name: string): void; | ||
removeListener(name: string, listener: () => any): void; | ||
verifyRequest(rawBody: string | Uint8Array | ArrayBuffer, signature: string, timestamp: string): Promise<boolean>; | ||
addInteraction(global: boolean, execute: (cmd: AppCommand) => void, options: AppCommand): Promise<boolean>; | ||
addCommand(global: boolean, execute: (cmd: AppCommand) => void, options: AppCommand): Promise<boolean>; | ||
register(): Promise<void>; | ||
} |
{ | ||
"name": "nonode-discord", | ||
"description": "Building bots without using node made 100x easier!", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"repository": "https://github.com/MDxWARRIORxOP/nonode-discord.git", | ||
@@ -6,0 +6,0 @@ "homepage": "https://github.com/MDxWARRIORxOP/nonode-discord.git#readme", |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
33504
26
801