0

DiscordJS example bot

Free lines JavaScript 1 revisions 167 a year ago a year ago
const Discord = require('discord.js');

const bot = new Discord.Client();
const token = '<token>';
const prefix = '!';

bot.on('ready', () => {
	console.log(`Logged in as ${bot.user.username}!`);
});

bot.on('message', msg => {
	// You probably here want a more sophisticated command parser
	// As well as command handler
	if (msg.content.startsWith(prefix)) {
		if (msg.content.startsWith(prefix + 'ping')) {
			msg.channel.sendMessage('Pong!');
		}
	}
})

bot.login(token);

This is the most basic example of a JavaScript Discord bot using the DiscordJS library.

You need to go to your Discord apps page, create an application, make it a bot one, and then replace the token in the code above. The code should be directly runnable.