Skip to content

Commit

Permalink
chore: add simple example
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasgmagalhaes committed Mar 21, 2021
1 parent bad8c94 commit c671c73
Show file tree
Hide file tree
Showing 6 changed files with 501 additions and 0 deletions.
1 change: 1 addition & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
86 changes: 86 additions & 0 deletions example/bot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// TODO: In some tests, when a function from another module is called,
// Corde reader fail in import the test file because Node.js can not
// import the submodule. To avoid the problem, this file is here is root of the project,
// but it should be in ./e2e
// Bug to fix: https://github.com/lucasgmagalhaes/corde/issues/490
//
// For some reason unknown by god, ts files get error, but js files works ok (???)
// (it's why this is the only file that is js file in this folder)

const { Client, Message } = require("discord.js");
const config = require("./corde.config");

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");
}

login();
5 changes: 5 additions & 0 deletions example/bot.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { test, expect } = require("corde");

test("ping should return pong", () => {
expect("ping").toReturn("pong");
});
34 changes: 34 additions & 0 deletions example/corde.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// This file must be .js. If it's .ts
// for some reason, tests will throw
// Error: Cannot find module './corde.config'
// A task to fix this problem is informed on
// https://github.com/lucasgmagalhaes/corde/issues/490

const env = require("dotenv");

var result = env.config();

// Do not throw any error if the project in running inside CI.
if (!process.env.CI && result.error) {
throw result.error;
}

const botPrefix = process.env.BOT_PREFIX;
const botTestId = process.env.BOT_TEST_ID;
const channelId = process.env.CHANNEL_ID;
const cordeTestToken = process.env.CORDE_TEST_TOKEN;
const guildId = process.env.GUILD_ID;
const testFiles = [process.env.TEST_FILES_DIR];
const botTestToken = process.env.BOT_TEST_TOKEN;
const timeOut = process.env.TIME_OUT;

module.exports = {
botPrefix,
botTestId,
channelId,
cordeTestToken,
guildId,
botTestToken,
testFiles,
timeOut,
};
14 changes: 14 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "example",
"version": "1.0.0",
"description": "",
"main": "bot.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Lucas Gomes",
"license": "ISC",
"devDependencies": {
"corde": "^2.0.1"
}
}
Loading

0 comments on commit c671c73

Please sign in to comment.