Created
December 4, 2017 13:41
-
-
Save zekroTJA/8f9fe93992556dd920e692d6dca46ee1 to your computer and use it in GitHub Desktop.
discord.js Tutorial - Part 1
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
{ | |
"token": "YOUR TOKEN HERE", | |
"owner": "YOUR DISCORD ID HERE", | |
"prefix": "::" | |
} |
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
const Discord = require('discord.js') | |
const fs = require('fs') | |
// Read out content of 'config.json' synchronous and | |
// parse it to a javascript object | |
const config = JSON.parse(fs.readFileSync('config.json', 'utf8')) | |
// Creating instance of discord bot client | |
var client = new Discord.Client() | |
// Ready Event: | |
// Fires when bot is logged in and ready to react on other events | |
client.on('ready', () => { | |
console.log(`Logged in as ${client.user.username}...`) | |
}) | |
// Refer command invokes to functions | |
var chdmap = { | |
say: cmd_say, | |
test: cmd_test | |
} | |
/** | |
* Command function for say command | |
* @param {Discord.Message} msg | |
* @param {string[]} args | |
*/ | |
function cmd_say(msg, args) { | |
msg.channel.send(args.join(' ')) | |
} | |
/** | |
* Command function for test command | |
* @param {Discord.Message} msg | |
* @param {string[]} args | |
*/ | |
function cmd_test(msg, args) { | |
console.log("test") | |
} | |
// Message Event: | |
// Fires when any message was send into a channel, | |
// the bot is able to read in | |
client.on('message', (msg) => { | |
// Getting content, sender, channel and guild out of message object | |
var cont = msg.content, | |
author = msg.member, | |
chan = msg.channel, | |
guild = msg.guild | |
// Only interpret when message was not send by bot self and if the | |
// message content starts with the prefix string | |
if (author.id != client.user.id && cont.startsWith(config.prefix)) { | |
// ::say hello world! | |
var invoke = cont.split(' ')[0].substr(config.prefix.length), | |
args = cont.split(' ').slice(1) | |
if (invoke in cmdmap) { | |
cmdmap[invoke](msg, args) | |
} | |
} | |
}) | |
// Login client | |
client.login(config.token) |
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
{ | |
"name": "tutorialjsbot", | |
"version": "1.0.0", | |
"description": "", | |
"main": "src/main.js", | |
"scripts": { | |
"test": "node main.js", | |
"start": "node main.js debug" | |
}, | |
"author": "", | |
"licence": "ISC", | |
"dependencies": { | |
"discord.js": "^11.2.1", | |
"colors": "^1.1.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment