2017-05-17 78 views
1

你好,我想發出的自動消息不和諧,但我不斷收到以下錯誤發送消息:Discord.js以1個分鐘的間隔

bot.sendMessage is not a function 

我不能確定,爲什麼我」米得到這個錯誤,下面是我的代碼;

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

bot.on('ready', function() { 
    console.log(bot.user.username); 
}); 

bot.on('message', function() { 
    if (message.content === "$loop") { 
     var interval = setInterval (function() { 
     bot.sendMessage(message.channel, "123") 
     }, 1 * 1000); 
    } 
}); 
+0

我剛剛意識到 - 你在看'discord.io'文檔嗎? –

回答

0

您的代碼返回錯誤,因爲Discord.Client()沒有一個叫sendMessage()如可以在docs可以看出方法。

如果您想發送消息,您應該按照以下方式進行操作;

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

bot.on('ready', function() { 
    console.log(bot.user.username); 
}); 

bot.on('message', function() { 
    if (message.content === "$loop") { 
     var interval = setInterval (function() { 
     message.channel.send("123") 
     }, 1 * 1000); 
    } 
}); 

我推薦用它可以發現here爲discord.js文檔熟悉自己。

0

Lennart是正確的,您不能使用bot.sendMessage,因爲botClient類,並且沒有sendMessage函數。這是冰山一角。你要找的是send(或舊版本,sendMessage)。

這些功能不能從Client類(這是bot是什麼,他們是在一個TextChannel類使用直接使用。那麼,你如何得到這個TextChannel?你把它從的Message。在示例代碼,你是不是真正從bot.on('message'...聽衆得到一個Message對象,但你應該

回調函數bot.on('...應該是這個樣子:

// add message as a parameter to your callback function 
bot.on('message', function(message) { 
    // Now, you can use the message variable inside 
    if (message.content === "$loop") { 
     var interval = setInterval (function() { 
      // use the message's channel (TextChannel) to send a new message 
      message.channel.send("123") 
      .catch(console.error); // add error handling here 
     }, 1 * 1000); 
    } 
}); 

您還會注意到在使用message.channel.send("123")之後,我添加了.catch(console.error);,因爲Discord期望它們的Promise返回函數來處理錯誤。

我希望這有助於!