I am new to Javascript and Discord Bots. I am trying to make a simple bot that just copies what a user says and then sends that exact message back. I am having difficulties though because for some reason the bot sends multiple messages at once instead of a singular message.
Here is my code:
var Discord = require('discord.io'); var logger = require('winston'); var auth = require('./auth.json'); // Configure logger settings logger.remove(logger.transports.Console); logger.add(new logger.transports.Console, { colorize: true }); logger.level = 'debug'; // Initialize Discord Bot var bot = new Discord.Client({ token: auth.token, autorun: true }); bot.on('ready', function (evt) { logger.info('Connected'); logger.info('Logged in as: '); logger.info(bot.username + ' - (' + bot.id + ')'); }); bot.on('message', function (user, userID, channelID, message, evt) { if (message) { var messageArray = message.substring(0).split(' '); var recentMessage = messageArray[0]; messageArray = messageArray.splice(1); switch(recentMessage) { default: bot.sendMessage({ to:channelID, message: recentMessage }) break; } } });
Thank you
Advertisement
Answer
If you look at the discord ping example, you can access the contents of the message with message.content
. I’m not sure, but you code looks like it is breaking the message
object apart which might be why you’re getting multiple messages back.
You might try changing your code to be:
bot.on('message', function (message) { // Send message back on the same channel message.channel.send(message.content); });
or to reply
bot.on('message', function (message) { // Reply to message with the same message message.reply(message.content); });