-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bad8c94
commit c671c73
Showing
6 changed files
with
501 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
Oops, something went wrong.