Corde example: corde bot sent message but the testing bot doesn't response #674
-
I try to get working the example of corde but when the corde bot sent the message, the testing bot doesn't response. But it responds with a real user. What would be the solution ? The corde test: const { test, expect,beforeStart } = require("corde");
const { login } = require("../index");
beforeStart(async() => {
login();
});
test("ping should return pong", () => {
expect("ping").toReturn("pong");
}); The main script: const { Client, Message } = require("discord.js");
const config = require("./corde.config.json");
const bot = new Client();
/**
* @param {string} message
*/
async function sendMessage(message) {
const channel = bot.channels.cache.get(config.channelId);
if (channel === undefined) {
throw new Error("Channelds not loaded");
}
if (channel.isText()) {
return await channel.send(message);
}
return null;
}
/**
* @param {string} name
*/
function getRole(name) {
return bot.guilds.cache.get(config.guildId).roles.cache.find((r) => r.name === name);
}
function getRoleManager() {
return bot.guilds.cache.get(config.guildId).roles;
}
/**
* Use this functions before use sendMessage (add it to **corde.beforeStart**)
*/
async function login() {
const readyPromise = new Promise((resolve) => {
bot.once("ready", () => {
resolve();
});
});
await bot.login(config.botTestToken);
await readyPromise;
}
bot.on("message", async (message) => {
try {
const args = message.content.slice(config.botPrefix.length).trim().split(" ");
const command = args.shift();
await handleCommands(message, command, args);
} catch (error) {
console.error(error);
throw new Error("Could not execute operation");
}
});
/**
* @param {Message} message
* @param {string} command
* @param {string[]} args
*/
async function handleCommands(message, command, args) {
// TODO: Refact this. '-'
if (command === "ping") {
await ping(message);
}
}
/**
* @param {Message} msg
* @param {string} msgId
*/
async function ping(msg) {
await msg.channel.send("pong");
}
exports.login = login; |
Beta Was this translation helpful? Give feedback.
Answered by
lucasgmagalhaes
May 13, 2021
Replies: 1 comment 3 replies
-
Hi @A2va, maybe you're using the wrong value for Another thing that I noticed was in the test file: beforeStart(async() => {
await login();
}); |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
lucasgmagalhaes
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @A2va, maybe you're using the wrong value for
botTestToken
. Did you check it?Another thing that I noticed was in the test file:
login
is an async function, so putawait
before calling it. It's possible that the test is being executed before the bot made login so it is executed unexpectedly