npm - Discord bot using javascript .sendMessage is not a function - Stack Overflow

I am working on a bot and I am trying to get it to send a message using .sendMessage (I do not want it

I am working on a bot and I am trying to get it to send a message using .sendMessage

(I do not want it to message when I type something, so I dont want to use

bot.on("message", function(message) {});

However I am getting .sendMessage is not a function

const Discord = require('discord.js');
const getJSON = require('get-json');
const BotToken = "token";
const bot = new Discord.Client();

bot.login(BotToken); 

bot.sendMessage('serverid', 'test');

I have done npm install discord.js as I thought it was part of the discord.js package.

.setStreaming gives the same error too. Most if not all of the functions from here give the error .html their tutorial says to use npm install --save --msvs_version=2015 discord.js which I have done.

What am I missing?

I am working on a bot and I am trying to get it to send a message using .sendMessage

(I do not want it to message when I type something, so I dont want to use

bot.on("message", function(message) {});

However I am getting .sendMessage is not a function

const Discord = require('discord.js');
const getJSON = require('get-json');
const BotToken = "token";
const bot = new Discord.Client();

bot.login(BotToken); 

bot.sendMessage('serverid', 'test');

I have done npm install discord.js as I thought it was part of the discord.js package.

.setStreaming gives the same error too. Most if not all of the functions from here give the error http://discordjs.readthedocs.io/en/latest/examples.html their tutorial says to use npm install --save --msvs_version=2015 discord.js which I have done.

What am I missing?

Share Improve this question edited Jun 13, 2017 at 17:46 JustOneTime asked Jun 13, 2017 at 16:01 JustOneTimeJustOneTime 351 gold badge2 silver badges12 bronze badges 3
  • Can you please provide the code so that other users can see why .sendMessage is not a function – Peter Reid Commented Jun 13, 2017 at 16:03
  • edited my original post with it – JustOneTime Commented Jun 13, 2017 at 17:17
  • bot.loginWithToken(BotToken, successCallback). You're not waiting for a response and you're passing a token to the wrong login function – Patrick Roberts Commented Jun 13, 2017 at 17:53
Add a ment  | 

3 Answers 3

Reset to default 4

You are trying to send a message to the server itself, but you can only send to channels. Also, sendMessage is deprecated and you should use send instead.

I've found out how to do this. I'll include a clip of my code here. I'm going to use my code to send messages at timed intervals, so my clip will include some other stuff that's not specifically what you're asking, but hopefully you can use my code to get a good understanding of what you need in your code. The part that makes this work the way you're is the first chunk.

var NotifyChannel;
bot.on('ready', () => {
  NotifyChannel = bot.channels.find("id", "347400244461568011");
});

var sched = later.parse.text('every 2 mins on the 30th sec');

function logTest() {
  NotifyChannel.send("This is a two minute test.");
  console.log(`Please only do this once, please. ${retrieveDate()}` + 
  `${retrieveTimestamp()}`);
  return;
}

var timer = later.setInterval(function(){ logTest(); }, sched)

Edit: formatting

To use .setPresence and a streaming url, it must be added to the ready function.

I'll include very simple code you can copy and paste, if that will help you understand the functions in your question:

var Discord = require('discord.js');
var bot = new Discord.Client();

bot.on('message', message => {
    var prefix = '!';
    if (msg === prefix + 'ping') {
      message.channel.send('pong!')
    }
});

bot.on('ready', () => {
    console.log('Connecting...');
    bot.user.setStatus('available') // Can be 'available', 'idle', 'dnd', or 'invisible'
    bot.user.setPresence({
        game: {
            name: 'MESSAGE', // What 'Now Playing:' reads
            type: 0,
            url: 'URL' // Replace with twitch.tv/channel url
        }
    });
});

bot.login('TOKEN');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745339057a4623239.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信