// import { TOKEN } from "../configs.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: 770381961553510451n, events: createEventHandlers({ ready: () => { startedAt = Date.now(); }, debug: console.log, }), intents: [], cache: { isAsync: false, }, }); await startBot(bot); // Delay the execution to allow READY events to be processed 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" }); // Assertions assertExists(guild); assertExists(guild.id); // Delay the execution to allow GUILD_CREATE event to be processed await delayUntil(10000, () => bot.cache.guilds.has(guild.id)); // FINAL CHECK TO THROW IF MISSING STILL 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" }); // Assertions 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 to allow MESSAGE_CREATE event to be processed await delayUntil(10000, () => 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."); } }); }); await stopBot(bot); });