2017-06-13 72 views
0

我對機器人的工作,我試圖把它發送使用.sendMessage不和諧的機器人不是一個函數

消息(我不希望它的消息時,我輸入的時候,所以我不想使用

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

但是我得到.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'); 

我已經做了故宮,因爲我認爲這是discord.js包的一部分安裝discord.js 。

.setStreaming也給出同樣的錯誤。大多數(如果不是全部)來自這裏的函數給出錯誤http://discordjs.readthedocs.io/en/latest/examples.html他們的教程說使用npm install --save --msvs_version = 2015 discord.js我已經完成了。

我錯過了什麼?

+0

能否請您提供的代碼,這樣其他用戶就可以看到**爲什麼**'.sendMessage不是函數' –

+0

編輯我的原始文章 – JustOneTime

+0

['bot.loginWithToken(BotToken,successCallback)'](http://discordjs.readthedocs.io/en/latest/examples.html#logging -in上帶有一個令牌)。您不是在等待響應,而是將令牌傳遞給錯誤的登錄函數 –

回答

4

您正嘗試向服務器本身發送消息,但只能發送到通道。另外,sendMessage已被棄用,您應該使用send來代替。

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) 

編輯:格式化

0

要使用.setPresence和流媒體URL,必須將它添加到就緒功能。

我會包括非常簡單的代碼,你可以複製和粘貼,如果這將幫助你瞭解你的問題的功能:

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'); 
相關問題