Send Message to Discord using Discordie NPM Package
I mainly use Discordie NPM Package for developing Discord Bot mainly because its easy to use and it has a good documentation which is better for any newbie like me to understand the basics.
To Start using it, at first you need to install it using the command shown below :
npm install discordie --save-dev
Then you need to require it in your file as shown below, where client is the new object created and Events are all the events which will be fire from the Discord.
const Discordie = require('discordie');
const client = new Discordie();
const Events = Discordie.Events;
Next is to start the connection for which you need to first get the Discord Bot Token
client.connect({
token: 'Your_Token'
});
client.Dispatcher.on(Events.GATEWAY_READY, e => {
console.log('Connected as: ' + client.User.username);
});
Once you are ready with the connection you need to listen to all the incoming messages and then if any message matches you need to send back the response as shown below
client.Dispatcher.on("MESSAGE_CREATE", e => {
console.log("new message arrived ");
if (e.message.content === "ping") e.message.channel.sendMessage("pong");
});
If you want to send the Typing before sending a message then use below command
event.message.channel.sendTyping();
The main challenge I faced when I tried to upload any document which can be done using the below commands, where first I get the source of the Image and then upload that using uploadFile function.
const source = fs.createReadStream('abc.jpeg');
event.message.channel.uploadFile(source, coin + 'abc.jpeg');
Please let me know if you need any more information about the same.
Posted on Utopian.io - Rewarding Open Source Contributors