This branch is a special fork to automate discord bot testing
Is this a good idea to test a discord bot?
No. But it works. I'm working on a flimsy mock library that allows you to test certain actions without actually sending it to the discord server/apiWhy another package? Why not a wrapper?
The whole point of this fork is to allow easy testing via replacing all `discord.js` with `discord.js.test` (this will not break anything unless you are not using the latest `discord.js` see `DOCS.md` for versioning info.)I originally thought about creating a wrapper, but here are reasons why that didn't work
- User#bot behavior is not changable by adding a wrapper. Unless you want to rewrite your whole code base, this is the better option
- Various methods are injected to make testing easier. This is also not possible with a wrapper
- Other packages either do not implement point 1, or are outdated.
The example uses 3 bots, but its really up to you
- a bot to be tested
- a user bot to be used as test
- (optional) a ci bot (has admin perms)
a lot of the options don't even have a framework. The reason is that everyone has a different bot structure, and implementing a setup for mocha isn't something for a framework
The env file and guild settings used for `setup.js`
# the bot that is subject to testing
DISCORD_TOKEN=
DISCORD_BOT_ID=
# the manager bot that has admin perms
DISCORD_CI_TOKEN=
# the user bot that will execute commands
DISCORD_USER_TOKEN=
# optional: the guild to test this
DISCORD_GUILD=
The guild has a channel named general
and another named system
(marked as a systems channel)
how to run the tests
check tests/mocha/README.md
for why this fork exists and
check example.spec.js
for examples
check test\mocha\DOCS.md
for update log and docs
note: if you want to test the bot, you need
mocha
anddotenv
, which is not included in package.json since i don't want to touch it
do npm run mocha
with 3 tokens and try the bot
Example output
Logging in...
user client logged in as Discord CI#4505
user client logged in as discord-db tester#4422
user client logged in as Emma P#3648
All clients are logged in. Testing for guild
guild set to 814550070790979645
fetching members...
channels: general, system-channel, afk channel
config==================================
CI Bot: Discord CI#4505
Bot: discord-db tester#4422
User Bot: Emma P#3648
========================================
Basic Test
ping command
√ should return pong
react this
√ should react to the message with joy
Disconnecting clients...
2 passing (11s)
How to integrate this into your project
-
rename all
["'`]discord\.js["'`]
topath.join(process.cwd(), 'discord.js')
-
create a folder called
discord.js
at root -
create an
index.js
file with content// Your condition here if (process.env.UNIT_TESTING) { // console.log(`using test file`); module.exports = require(`discord.js.test`); } else { module.exports = require(`discord.js`); }
this will create a flexible environment
Table of contents
About
discord.js is a powerful Node.js module that allows you to easily interact with the Discord API.
- Object-oriented
- Predictable abstractions
- Performant
- 100% coverage of the Discord API
Installation
Node.js 12.0.0 or newer is required.
Ignore any warnings about unmet peer dependencies, as they're all optional.
Without voice support: npm install discord.js
With voice support (@discordjs/opus): npm install discord.js @discordjs/opus
With voice support (opusscript): npm install discord.js opusscript
Audio engines
The preferred audio engine is @discordjs/opus, as it performs significantly better than opusscript. When both are available, discord.js will automatically choose @discordjs/opus. Using opusscript is only recommended for development environments where @discordjs/opus is tough to get working. For production bots, using @discordjs/opus should be considered a necessity, especially if they're going to be running on multiple servers.
Optional packages
-
zlib-sync for WebSocket data compression and inflation (
npm install zlib-sync
) -
erlpack for significantly faster WebSocket data (de)serialisation (
npm install discord/erlpack
) - One of the following packages can be installed for faster voice packet encryption and decryption:
-
sodium (
npm install sodium
) -
libsodium.js (
npm install libsodium-wrappers
)
-
sodium (
-
bufferutil for a much faster WebSocket connection (
npm install bufferutil
) -
utf-8-validate in combination with
bufferutil
for much faster WebSocket processing (npm install utf-8-validate
)
Example usage
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', msg => {
if (msg.content === 'ping') {
msg.reply('pong');
}
});
client.login('token');
Links
- Website (source)
- Documentation
-
Guide (source) - this is still for stable
See also the Update Guide, including updated and removed items in the library. - Discord.js Discord server
- Discord API Discord server
- GitHub
- NPM
- Related libraries
Extensions
Contributing
Before creating an issue, please ensure that it hasn't already been reported/suggested, and double-check the
documentation.
See the contribution guide if you'd like to submit a PR.
Help
If you don't understand something in the documentation, you are experiencing problems, or you just need a gentle nudge in the right direction, please don't hesitate to join our official Discord.js Server.