使用javascript .sendMessage的discord bot不是一个函数

时间:2017-06-13 16:01:43

标签: javascript npm bots npm-install discord

我正在研究机器人,我正试图让它使用.sendMessage发送消息

(当我输入内容时,我不希望它发送消息,因此我不想使用

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');

但是我得到.sendMessage不是一个函数

{{1}}

我已经完成了npm install discord.js,因为我认为它是discord.js软件包的一部分。

.setStreaming也会给出相同的错误。大多数(如果不是全部)函数都提供错误http://discordjs.readthedocs.io/en/latest/examples.html他们的教程说使用npm install --save --msvs_version = 2015 discord.js,我已经完成了。

我错过了什么?

3 个答案:

答案 0 :(得分:4)

您正尝试向服务器本身发送消息,但您只能发送到频道。此外,不推荐使用sendMessage,您应该使用send。

答案 1 :(得分:0)

我发现了如何做到这一点。我在这里包含了我的代码片段。我将使用我的代码按时间间隔发送消息,因此我的剪辑将包含其他一些不是您特别要求的内容,但希望您可以使用我的代码来获取很好地理解您的代码中需要什么。使这项工作按照你的方式工作的部分是第一块。

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)

修改:格式化

答案 2 :(得分:0)

要使用.setPresence和流媒体网址,必须将其添加到就绪功能中。

我将包含您可以复制和粘贴的非常简单的代码,如果这样可以帮助您理解问题中的功能:

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');