mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-15 19:13:31 +01:00
Updated README and tests
This commit is contained in:
112
README.md
112
README.md
@@ -1,69 +1,71 @@
|
|||||||
# discord.js
|
# discord.js
|
||||||
Discord.js is a node module that allows you to interface with the [Discord](https://discordapp.com/) API for creation of things such as bots or loggers.
|
|
||||||
|
|
||||||
The aim of this API is to make it *really* simple to start developing your bots. This API has server, channel and user tracking, as well as tools to make identification really simple.
|
[](https://travis-ci.org/discord-js/discord.js)
|
||||||
|
|
||||||
The new rewrite of the API (version 3+) is written in ECMAScript 6 and compiled down to EC5 using Babel. It allows the code to be written faster and more consistently, and take use of new features.
|
discord.js is a node module used as a way of interfacing with
|
||||||
|
[Discord](https://discordapp.com/). It is a very useful module for creating
|
||||||
## New update break your code? Read why [here](https://github.com/discord-js/discord.js/wiki#why-did-my-code-break-with-the-new-update).
|
bots.
|
||||||
|
|
||||||
**[Find the website here.](http://discord-js.github.io)**
|
|
||||||
|
|
||||||
**[For more information, click here.](https://github.com/hydrabolt/discord.js/wiki)**
|
|
||||||
|
|
||||||
### This module is still in alpha - especially the newer versions!
|
|
||||||
|
|
||||||
This node module is still in alpha, and some methods and functions may change or completely disappear!
|
|
||||||
|
|
||||||
### Installation
|
### Installation
|
||||||
``npm install --save discord.js``
|
`npm install --save discord.js`
|
||||||
|
|
||||||
### Features
|
---
|
||||||
|
|
||||||
* Send, Receive Delete and **Edit** messages from channels _and_ DMs! Auto-initiates DMs for you!
|
### Example
|
||||||
* Create, Delete and Leave servers and channels
|
|
||||||
* Create invites for Servers
|
|
||||||
* Silent Mention - trigger mention notification without actually @mentioning a user!
|
|
||||||
* Get complete metadata on users, channels and servers - including avatars.
|
|
||||||
* Get limitless logs from channels.
|
|
||||||
* Fast and efficient caching
|
|
||||||
* Auto-cache messages
|
|
||||||
|
|
||||||
### Example usage
|
|
||||||
```js
|
```js
|
||||||
/*
|
var Discord = require("discord.js");
|
||||||
* A basic bot that shows how to connect to a Discord account,
|
|
||||||
* how to listen to messages and how to send messages.
|
|
||||||
*
|
|
||||||
* This bot responds to every "ping" message with a "pong".
|
|
||||||
*/
|
|
||||||
|
|
||||||
var Discord = require( "discord.js" );
|
var mybot = new Discord.Client();
|
||||||
|
|
||||||
// Create the bot
|
mybot.on("message", function(message){
|
||||||
var myBot = new Discord.Client();
|
|
||||||
|
|
||||||
// Login with an example email and password
|
if(message.content === "ping")
|
||||||
myBot.login( "hello@example.com", "password1" );
|
mybot.reply(message, "pong");
|
||||||
|
|
||||||
// The "ready" event is triggered after the bot successfully connected to
|
});
|
||||||
// Discord and is ready to send messages.
|
|
||||||
myBot.on( "ready", function() {
|
|
||||||
console.log( "Bot connected successfully." );
|
|
||||||
} );
|
|
||||||
|
|
||||||
// Add a listener to the "message" event, which triggers upon receiving
|
mybot.login("email", "password");
|
||||||
// any message
|
|
||||||
myBot.on( "message", function( message ) {
|
|
||||||
// message.content accesses the content of the message as a string.
|
|
||||||
// If it is equal to "ping", then the bot should respond with "pong".
|
|
||||||
if ( message.content === "ping" ) {
|
|
||||||
// Send a message ("pong") to the channel the message was sent in,
|
|
||||||
// which is accessed by message.channel.
|
|
||||||
this.sendMessage( message.channel, "pong" );
|
|
||||||
}
|
|
||||||
} );
|
|
||||||
```
|
```
|
||||||
### TODO
|
---
|
||||||
* Joining servers from an invite
|
|
||||||
* Stealthy Ninja support
|
### Related Projects
|
||||||
|
|
||||||
|
Here is a list of other Discord APIs:
|
||||||
|
|
||||||
|
#### Java:
|
||||||
|
[Discord4J](https://github.com/nerd/Discord4J)
|
||||||
|
#### .NET:
|
||||||
|
[Discord.Net](https://github.com/RogueException/Discord.Net)
|
||||||
|
|
||||||
|
[DiscordSharp](https://github.com/Luigifan/DiscordSharp)
|
||||||
|
#### NodeJS
|
||||||
|
[node-discord](https://github.com/izy521/node-discord) (similar to discord.js but lower level)
|
||||||
|
|
||||||
|
#### PHP
|
||||||
|
[DiscordPHP](https://github.com/teamreflex/DiscordPHP)
|
||||||
|
|
||||||
|
#### Python
|
||||||
|
[discord.py](https://github.com/Rapptz/discord.py)
|
||||||
|
|
||||||
|
#### Ruby
|
||||||
|
[discordrb](https://github.com/meew0/discordrb)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Links
|
||||||
|
**[Documentation](https://github.com/discord-js/discord.js/wiki/Documentation)**
|
||||||
|
|
||||||
|
**[GitHub](https://github.com/discord-js/discord.js)**
|
||||||
|
|
||||||
|
**[Wiki](https://github.com/discord-js/discord.js/wiki)**
|
||||||
|
|
||||||
|
**[Website](http://discord-js.github.io/)**
|
||||||
|
|
||||||
|
**[NPM](npmjs.com/package/discord.js)**
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Contact
|
||||||
|
|
||||||
|
If you would like to contact me, you can create an issue on the GitHub repo
|
||||||
|
or send a DM to **hydrabolt** in [Discord API](https://discord.gg/0SBTUU1wZTY66OLO).
|
||||||
@@ -73,7 +73,7 @@ var Client = (function () {
|
|||||||
}, {
|
}, {
|
||||||
key: "debug",
|
key: "debug",
|
||||||
value: function debug(message) {
|
value: function debug(message) {
|
||||||
console.log(message);
|
this.trigger("debug", message);
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
key: "on",
|
key: "on",
|
||||||
@@ -167,6 +167,7 @@ var Client = (function () {
|
|||||||
callback(err);
|
callback(err);
|
||||||
reject(err);
|
reject(err);
|
||||||
} else {
|
} else {
|
||||||
|
self.websocket.close();
|
||||||
self.state = 4;
|
self.state = 4;
|
||||||
callback();
|
callback();
|
||||||
resolve();
|
resolve();
|
||||||
|
|||||||
@@ -95,7 +95,7 @@ class Client {
|
|||||||
|
|
||||||
//def debug
|
//def debug
|
||||||
debug(message) {
|
debug(message) {
|
||||||
console.log(message);
|
this.trigger("debug", message);
|
||||||
}
|
}
|
||||||
|
|
||||||
on(event, fn) {
|
on(event, fn) {
|
||||||
@@ -185,6 +185,7 @@ class Client {
|
|||||||
callback(err);
|
callback(err);
|
||||||
reject(err);
|
reject(err);
|
||||||
} else {
|
} else {
|
||||||
|
self.websocket.close();
|
||||||
self.state = 4;
|
self.state = 4;
|
||||||
callback();
|
callback();
|
||||||
resolve();
|
resolve();
|
||||||
|
|||||||
115
test/bot.js
115
test/bot.js
@@ -1,18 +1,119 @@
|
|||||||
/*
|
/*
|
||||||
|
this file should be used for travis builds only
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var Discord = require("../");
|
var Discord = require("../");
|
||||||
var mybot = new Discord.Client();
|
var mybot = new Discord.Client();
|
||||||
|
|
||||||
mybot.login("email", "password").then(success).catch(error);
|
var server, channel, message, sentMessage = false;
|
||||||
|
|
||||||
function success(){
|
function success1(){ //make server
|
||||||
console.log("login successful");
|
console.log("preparing...");
|
||||||
|
mybot.createServer("test-server", "london").then(success2).catch(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
function success2(_server){ //make channel
|
||||||
|
console.log("test 1 successful");
|
||||||
|
server = _server;
|
||||||
|
mybot.createChannel(server, "test-channel", "text").then(success3).catch(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
function success3(_channel){ //send message
|
||||||
|
console.log("test 2 successful");
|
||||||
|
channel = _channel;
|
||||||
|
mybot.sendMessage(channel, [mybot.user.avatarURL, "an", "array", "of", "messages"]).then(success4).catch(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
function success4(_message){ //delete message
|
||||||
|
console.log("test 3 successful");
|
||||||
|
message = _message;
|
||||||
|
mybot.deleteMessage(message).then(success5).catch(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
function success5(){ //send ping
|
||||||
|
console.log("test 4 successful");
|
||||||
|
mybot.sendMessage(channel, "ping").then(function(msg){
|
||||||
|
message = msg;
|
||||||
|
}).catch(error);
|
||||||
|
setTimeout(checkError, 30 * 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
function success7(){
|
||||||
|
console.log("test 6 successful");
|
||||||
|
mybot.deleteChannel(channel).then(success8).catch(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
function success8(){
|
||||||
|
console.log("test 7 successful");
|
||||||
|
mybot.createInvite(server).then(success9).catch(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
function success9(invite){
|
||||||
|
console.log("test 8 successful");
|
||||||
|
if(invite.code){
|
||||||
|
success10();
|
||||||
|
}else{
|
||||||
|
error("reference error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function success10(){
|
||||||
|
console.log("test 9 succesful");
|
||||||
|
mybot.leaveServer(server).then(success11).catch(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
function success11(){
|
||||||
|
console.log("test 10 succesful");
|
||||||
|
mybot.joinServer(process.env["ds-invite"]).then(success12).catch(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
function success12(_server){
|
||||||
|
console.log("test 11 successful");
|
||||||
|
server = mybot.getServer("id", _server.id);
|
||||||
|
if(server){
|
||||||
|
success13();
|
||||||
|
}else{
|
||||||
|
error("reference error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function success13(){
|
||||||
|
console.log("test 12 successful");
|
||||||
|
mybot.leaveServer(server).then(success14).catch(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
function success14(){
|
||||||
|
console.log("test 13 successful");
|
||||||
|
mybot.logout().then(done).catch(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
function done(){
|
||||||
|
console.log("All tests completed succesfully.");
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
function error(){
|
function checkError(){
|
||||||
console.log("login error, but the API works");
|
if(!sentMessage){
|
||||||
process.exit(0);
|
error("failure receiving messages");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function error(err){
|
||||||
|
console.log("error", err);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
mybot.on("message", function(message){
|
||||||
|
|
||||||
|
if(message.channel.equals(channel)){
|
||||||
|
if(message.content === "ping"){
|
||||||
|
console.log("test 5 successful");
|
||||||
|
sentMessage = true;
|
||||||
|
|
||||||
|
mybot.updateMessage(message, "pong").then(success7).catch(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
mybot.login(process.env["ds-email"], process.env["ds-password"]).then(success1).catch(error);
|
||||||
Reference in New Issue
Block a user