diff --git a/src/bot.ts b/src/bot.ts index a90dafccb..d46b41c76 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -64,7 +64,6 @@ import { resume, resharder, log, - startGateway, spawnShards, createShard, identify, @@ -668,7 +667,6 @@ export function createGatewayManager( buckets: new Collection(), utf8decoder: new TextDecoder(), - startGateway, spawnShards, createShard, identify, @@ -685,9 +683,15 @@ export function createGatewayManager( }; } -export function stopBot(bot: Bot) { +export async function stopBot(bot: Bot) { // STOP REST // STOP WS + bot.gateway.shards.forEach((shard) => { + clearInterval(shard.heartbeat.intervalId); + bot.gateway.closeWS(shard.ws, 3061, "Discordeno Testing Finished! Do Not RESUME!"); + }); + + await delay(3000); } export interface CreateBotOptions { @@ -1149,8 +1153,6 @@ export interface GatewayManager { // METHODS - /** The handler function that starts the gateway. */ - startGateway: typeof startGateway; /** The handler for spawning ALL the shards. */ spawnShards: typeof spawnShards; /** Create the websocket and adds the proper handlers to the websocket. */ diff --git a/src/helpers/channels/edit_channel.ts b/src/helpers/channels/edit_channel.ts index b2daa448f..146999a93 100644 --- a/src/helpers/channels/edit_channel.ts +++ b/src/helpers/channels/edit_channel.ts @@ -1,8 +1,8 @@ -import type { DiscordenoChannel } from "../../structures/channel.ts"; import type { Channel } from "../../types/channels/channel.ts"; import type { ModifyChannel } from "../../types/channels/modify_channel.ts"; import type { Bot } from "../../bot.ts"; import { SnakeCasedPropertiesDeep } from "../../types/util.ts"; +import { DiscordenoChannel } from "../../transformers/channel.ts"; /** Update a channel's settings. Requires the `MANAGE_CHANNELS` permission for the guild. */ export async function editChannel(bot: Bot, channelId: bigint, options: ModifyChannel, reason?: string) { diff --git a/src/types/interactions/interaction.ts b/src/types/interactions/interaction.ts index 8e408d2ef..a6e308ac3 100644 --- a/src/types/interactions/interaction.ts +++ b/src/types/interactions/interaction.ts @@ -5,7 +5,7 @@ import { InteractionGuildMember } from "./interaction_guild_member.ts"; import { DiscordInteractionTypes } from "./interaction_types.ts"; import { SelectMenuData } from "../messages/components/select_data.ts"; import { ButtonData } from "../messages/components/button_data.ts"; -import { DiscordenoMessage } from "../../structures/message.ts"; +import { DiscordenoMessage } from "../../transformers/message.ts"; /** https://discord.com/developers/docs/interactions/slash-commands#interaction */ export type Interaction = PingInteraction | SlashCommandInteraction | ComponentInteraction; diff --git a/src/ws/heartbeat.ts b/src/ws/heartbeat.ts index 1968a2220..d271c0cea 100644 --- a/src/ws/heartbeat.ts +++ b/src/ws/heartbeat.ts @@ -10,7 +10,7 @@ export async function heartbeat(gateway: GatewayManager, shardId: number, interv gateway.log("HEARTBEATING_DETAILS", { shardId, interval, shard }); - // The first heartbeat is special so we send it without setInterval: https://discord.com/developers/docs/topics/gateway#heartbeating + // The first heartbeat is special so we send it without set Interval: https://discord.com/developers/docs/topics/gateway#heartbeating await delay(Math.floor(shard.heartbeat.interval * Math.random())); if (shard.ws.readyState !== WebSocket.OPEN) return; diff --git a/src/ws/identify.ts b/src/ws/identify.ts index 080bab013..e5010fc23 100644 --- a/src/ws/identify.ts +++ b/src/ws/identify.ts @@ -39,20 +39,6 @@ export function identify(gateway: GatewayManager, shardId: number, maxShards: nu }); socket.onopen = () => { - console.log({ - op: DiscordGatewayOpcodes.Identify, - d: { - token: gateway.token, - compress: gateway.compress, - properties: { - $os: gateway.$os, - $browser: gateway.$browser, - $device: gateway.$device, - }, - intents: gateway.intents, - shard: [shardId, maxShards], - }, - },) gateway.sendShardMessage( gateway, shardId, diff --git a/src/ws/mod.ts b/src/ws/mod.ts index 146bdc424..b4cd71c47 100644 --- a/src/ws/mod.ts +++ b/src/ws/mod.ts @@ -10,6 +10,5 @@ export * from "./resume.ts"; export * from "./spawn_shards.ts"; export * from "./send_shard_message.ts"; export * from "./start_gateway_options.ts"; -export * from "./start_gateway.ts"; export * from "./tell_cluster_to_identify.ts"; export * from "./ws.ts"; diff --git a/src/ws/start_gateway.ts b/src/ws/start_gateway.ts deleted file mode 100644 index d0f0f4ba9..000000000 --- a/src/ws/start_gateway.ts +++ /dev/null @@ -1,44 +0,0 @@ -import { DiscordGatewayIntents } from "../types/gateway/gateway_intents.ts"; -import type { GetGatewayBot } from "../types/gateway/get_gateway_bot.ts"; -import { StartGatewayOptions } from "./start_gateway_options.ts"; -import { GatewayManager } from "../bot.ts"; -import { SnakeCasedPropertiesDeep } from "../types/util.ts"; - -/** ADVANCED DEVS ONLY!!!!!! - * Starts the standalone gateway. - * This will require starting the bot separately. - */ -export async function startGateway(gateway: GatewayManager, options: StartGatewayOptions) { - gateway.token = `Bot ${options.token}`; - gateway.secretKey = options.secretKey; - gateway.firstShardId = options.firstShardId; - gateway.url = options.url; - if (options.shardsPerCluster) gateway.shardsPerCluster = options.shardsPerCluster; - if (options.maxClusters) gateway.maxClusters = options.maxClusters; - - if (options.compress) { - gateway.compress = options.compress; - } - if (options.reshard) gateway.reshard = options.reshard; - // Once an hour check if resharding is necessary - setInterval(() => gateway.resharder(gateway), 1000 * 60 * 60); - - gateway.intents = options.intents.reduce( - (bits, next) => (bits |= typeof next === "string" ? DiscordGatewayIntents[next] : next), - 0 - ); - - const result = (await fetch(`https://discord.com/api/gateway/bot`, { - headers: { Authorization: gateway.token }, - }).then((res) => res.json())) as SnakeCasedPropertiesDeep; - - gateway.urlWSS = result.url; - gateway.sessionStartLimitTotal = result.session_start_limit.total; - gateway.sessionStartLimitRemaining = result.session_start_limit.remaining; - gateway.sessionStartLimitResetAfter = result.session_start_limit.reset_after; - gateway.maxConcurrency = result.session_start_limit.max_concurrency; - gateway.maxShards = options.maxShards || result.shards; - gateway.lastShardId = options.lastShardId || result.shards - 1; - - gateway.spawnShards(gateway, gateway.firstShardId); -} diff --git a/tests/mod.ts b/tests/mod.ts index deaf056a0..cce31bc4d 100644 --- a/tests/mod.ts +++ b/tests/mod.ts @@ -1,19 +1,20 @@ import { TOKEN } from "../configs.ts"; -import { Bot, createBot, createEventHandlers, startBot } from "../mod.ts"; +import { Bot, createBot, createEventHandlers, startBot, stopBot } from "../mod.ts"; Deno.test("[Bot] - Starting Tests", async (t) => { const bot = createBot({ token: TOKEN || Deno.env.get("DISCORD_TOKEN"), botId: 675412054529540107n, events: createEventHandlers({ - raw: console.log, }), intents: [], - }); + }) as Bot; - await startBot(bot as Bot); + await startBot(bot); console.log("Bot online"); + + await stopBot(bot); }); // // THE ORDER OF THE IMPORTS IN THIS FILE MATTER!