diff --git a/src/cache.ts b/src/cache.ts index c4ca32f51..e39ca6904 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -36,6 +36,7 @@ export function createCache( return { guilds: tableCreator("guilds"), users: tableCreator("users"), + members: tableCreator("members"), channels: tableCreator("channels"), messages: tableCreator("messages"), presences: tableCreator("presences"), @@ -49,6 +50,7 @@ export function createCache( return { guilds: tableCreator("guilds"), users: tableCreator("users"), + members: tableCreator("members"), channels: tableCreator("channels"), messages: tableCreator("messages"), presences: tableCreator("presences"), @@ -178,7 +180,7 @@ export function createExecute(cache: Cache | AsyncCache): CacheExecutor { }; } -export type TableNames = "channels" | "users" | "guilds" | "messages" | "presences" | "threads" | "unavailableGuilds"; +export type TableNames = "channels" | "users" | "guilds" | "messages" | "presences" | "threads" | "unavailableGuilds" | "members"; // function messageSweeper(bot: Bot, message: DiscordenoMessage) { // // DM messages aren't needed diff --git a/src/helpers/voice/connect_to_voice_channel.ts b/src/helpers/voice/connect_to_voice_channel.ts index 98678196c..d5d1d9310 100644 --- a/src/helpers/voice/connect_to_voice_channel.ts +++ b/src/helpers/voice/connect_to_voice_channel.ts @@ -15,8 +15,8 @@ export async function connectToVoiceChannel( bot.gateway.sendShardMessage(bot.gateway, bot.utils.calculateShardId(bot.gateway, guildId), { op: DiscordGatewayOpcodes.VoiceStateUpdate, d: { - guild_id: guildId, - channel_id: channelId, + guild_id: guildId.toString(), + channel_id: channelId.toString(), self_mute: Boolean(options?.selfMute), self_deaf: options?.selfDeaf ?? true, }, diff --git a/src/transformers/channel.ts b/src/transformers/channel.ts index d9b376a50..d5af4c446 100644 --- a/src/transformers/channel.ts +++ b/src/transformers/channel.ts @@ -85,3 +85,4 @@ export interface DiscordenoChannel /** The voice states that are in this channel assuming it is a voice channel. */ voiceStates?: Collection; } + diff --git a/src/transformers/guild.ts b/src/transformers/guild.ts index e4ae01b47..1b4b4a803 100644 --- a/src/transformers/guild.ts +++ b/src/transformers/guild.ts @@ -11,6 +11,14 @@ export function transformGuild( bot: Bot, payload: { guild: SnakeCasedPropertiesDeep } & { shardId: number } ): DiscordenoGuild { + const guildId = bot.transformers.snowflake(payload.guild.id); + const channels = payload.guild.channels || []; + + channels.forEach(async (channel) => { + const chnl = bot.transformers.channel(bot, { channel, guildId }); + await bot.cache.channels.set(chnl.id, chnl); + }); + return { afkTimeout: payload.guild.afk_timeout, approximateMemberCount: payload.guild.approximate_member_count, diff --git a/tests/mod.ts b/tests/mod.ts index cebae98ce..d51f3fb77 100644 --- a/tests/mod.ts +++ b/tests/mod.ts @@ -1,24 +1,41 @@ // import { TOKEN } from "../configs.ts"; -import { Bot, createBot, createEventHandlers, DiscordChannelTypes, startBot, stopBot } from "../mod.ts"; +import { createBot, createEventHandlers, DiscordChannelTypes, startBot, stopBot } from "../mod.ts"; import { assertEquals, assertExists } from "./deps.ts"; import { delayUntil } from "./utils.ts"; Deno.test("[Bot] - Starting Tests", async (t) => { + let startedAt = 0; const bot = createBot({ // token: TOKEN || Deno.env.get("DISCORD_TOKEN"), token: Deno.env.get("DISCORD_TOKEN")!, - botId: 675412054529540107n, + botId: 770381961553510451n, events: createEventHandlers({ - // debug: console.log, + ready: () => { + startedAt = Date.now(); + }, + debug: console.log, }), intents: [], - }) as Bot; + cache: { + isAsync: false, + }, + }); await startBot(bot); // Delay the execution to allow READY events to be processed - await delayUntil(3000, () => true); + await delayUntil(10000, () => Boolean(startedAt)); console.log("Bot online"); + // DELETE GUILDS IF LESS THAN 10 SERVERS AS SAFETY MEASURE + if (bot.cache.guilds.size() <= 10) { + bot.cache.guilds.forEach(async (guild) => { + if (guild.ownerId === bot.id) await bot.helpers.deleteGuild(bot, guild.id); + }) + } + + // Delay the execution to allow delete guilds to be processed + await delayUntil(10000, () => Boolean(startedAt)); + // CREATE ONE GUILD SO WE CAN REUSE LATER TO SAVE RATE LIMITS const guild = await bot.helpers.createGuild(bot, { name: "Discordeno Test" }); @@ -27,13 +44,14 @@ Deno.test("[Bot] - Starting Tests", async (t) => { assertExists(guild.id); // Delay the execution to allow GUILD_CREATE event to be processed - await delayUntil(10000, async () => await bot.cache.guilds.has(guild.id)); + await delayUntil(10000, () => bot.cache.guilds.has(guild.id)); // FINAL CHECK TO THROW IF MISSING STILL - if (!(await bot.cache.guilds.has(guild.id))) { + if (!bot.cache.guilds.has(guild.id)) { throw new Error(`The guild seemed to be created but it was not cached. ${guild.id.toString()}`); } + // CHANNEL TESTS GROUPED await t.step("Channel related tests", async (t) => { const channel = await bot.helpers.createChannel(bot, guild.id, { name: "Discordeno-test" }); @@ -41,16 +59,17 @@ Deno.test("[Bot] - Starting Tests", async (t) => { assertExists(channel); assertEquals(channel.type, DiscordChannelTypes.GuildText); + // ALL MESSAGE RELATED TESTS THAT DEPEND ON AN EXISTING CHANNEL await t.step("Message related tests", async (t) => { const message = await bot.helpers.sendMessage(bot, channel.id, "Testing"); // Assertions assertExists(message); - // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed - await delayUntil(10000, async () => await bot.cache.messages.has(message.id)); + // Delay the execution to allow MESSAGE_CREATE event to be processed + await delayUntil(10000, () => bot.cache.messages.has(message.id)); - if (!(await bot.cache.messages.has(message.id))) { + if (!bot.cache.messages.has(message.id)) { throw new Error("The message seemed to be sent but it was not cached."); } });