2017-12-16 2 views
-3

私は簡単なボットを使って特定のチャンネルにユーザーの入力を再投稿しようとしています。私は数時間それに取り組んできたし、ここで立ち往生した。 bot.loginで予期しないトークンエラーが発生しました。修正するときに、削除した部分を戻すように求められます。私はボットにも一般的な質問があります。どのようにユーザーメッセージを変数として保存しますか?それを再印刷し、どのように確実にする必要があります!bot.loginの予期しないトークン/変数としてチャットを保存

コード:

const Discord = require("discord.js"); // use discord.js 
var bot = new Discord.Client(); // sets Discord.Client to bot 
const BOT_TOKEN = "hidden_here" 
// bot's token 
const PREFIX = "~" // bot's prefix 

bot.on("ready", function() { // when the bot starts up, set its game to Use 
*help and tell the console "Booted up!" 
//bot.user.setGame("Use ~info") // sets the game the bot is playing 
console.log("Bot is now online") // messages the console Bot is now online! 
console.log("Use CTRL+C to shut down bot") // messages the console 

}); 


bot.on("message", function(message) { // when a message is sent 
if (message.author.equals(bot.user)) return; // if the message is sent by a 
bot, ignore 

if (!message.content.startsWith(PREFIX)) return; // if the message doesn't 
contain PREFIX (*), then ignore 

var args = message.content.substring(PREFIX.length).split(" "); // removes 
the prefix from the message 
var command = args[0].toLowerCase(); // sets the command to lowercase 
(making it incase sensitive) 

    if (command == "say") { // creates command say 
    if (!message.member.roles.some(r=>["Votebot.Start"].includes(r.name))) 
return message.reply("Sorry, you do not have the permission to do this!"); 
    var sayMessage = message.content.substring(4) 
    message.delete().catch(O_o=>{}); 
    message.channel.send(sayMessage); 
    } 

if (command == "vote") { //creates command vote 
if (!message.member.roles.some(r=>["Votebot.Start"].includes(r.name))) 
return message.reply("Sorry, you do not have the permission to do this!"); 
    message.channel.send("Type what you would like the vote to be about") 
    .then(() => { 
    var vote = message.channel.awaitMessages(response) 
    message.updateMessage("message", "Great! Starting a vote in #polls now") 
    message.channel.goto("#polls") 
    message.channel.send(vote) 
    message.react('✅'); 
    message.react('❎'); 
    }); 

    bot.login(BOT_TOKEN); // connects to the bot 

答えて

0

私は先に行って、あなたのコードを編集しました。私はそれをテストしていないので、それが動作するかどうかは分かりません。他の開発者が読むのが難しくないように、コードを適切にインデントすることを学ぶようにお願いします。 AtomやVisual Studio Codeなどの適切なIDEを使用すると、コードに構文エラーが表示されることがあります。変数の宣言の最後にセミコロンを置いてください。また、すべてのコード行にコメントする必要はありません。クラッタを作成するだけです。

{ 
 
const Discord = require("discord.js"); // use discord.js 
 
var bot = new Discord.Client(); // sets Discord.Client to bot 
 
const BOT_TOKEN = "hidden_here"; 
 
// bot's token 
 
const PREFIX = "~"; // bot's prefix 
 

 
bot.on("ready", function() { // when the bot starts up, set its game to Use 
 
    // *help and tell the console "Booted up!" 
 
    //bot.user.setGame("Use ~info") // sets the game the bot is playing 
 
    console.log("Bot is now online") // messages the console Bot is now online! 
 
    console.log("Use CTRL+C to shut down bot") // messages the console 
 
}); 
 

 

 
bot.on("message", message => { // when a message is sent 
 
    if (message.author.equals(bot.user)) return; 
 

 
    if (!message.content.startsWith(PREFIX)) return; 
 

 
    var args = message.content.substring(PREFIX.length).split(" "); 
 
    var command = args[0].toLowerCase(); 
 

 
    if (command == "say") { 
 

 
     if (!message.member.roles.some(r => ["Votebot.Start"].includes(r.name))) { 
 
      message.reply("Sorry, you do not have the permission to do this!"); 
 
      return 
 
     } 
 

 
     var sayMessage = message.content.substring(4) 
 
     message.delete().catch(O_o => {}); 
 
     message.channel.send(sayMessage); 
 

 
    } 
 

 
    if (command === "vote") { 
 

 
     if (!message.member.roles.some(r => ["Votebot.Start"].includes(r.name))) { 
 
      message.reply("Sorry, you do not have the permission to do this!"); 
 
      return 
 
     } 
 

 
     message.channel.send("Type what you would like the vote to be about") 
 
      .then(() => { 
 
       var vote = message.channel.awaitMessages(response); 
 
       message.updateMessage("message", "Great! Starting a vote in #polls now"); 
 
       message.channel.goto("#polls"); 
 
       message.channel.send(vote) 
 
       message.react('✅'); 
 
       message.react('❎'); 
 
      }); 
 
    } 
 

 
}); 
 

 
bot.login(BOT_TOKEN);

また、店舗のメッセージの内容に、これはあなたが@JeydinNewWonを実行した後に得たエラーに追加された何かのように、

var content = message.content;

+0

を、私はこのコードを実行しようとしましたが、それはそのメッセージが定義されていないというエラーを吐き出しています!私はdiscord.jsを再インストールしようとしましたが、動作しませんでした。 – Awsomebulderguy

0

を行います。 定義されていないため、「メッセージは定義されていません」というエラーが表示されます。これを修正するには、bot.on("message",() => {}を置き換えるべき正しいコード行を与えます。

bot.on('message', async message => {} 

イベントの後に受信したMessageオブジェクトを定義します。

あなたは完全なコードたい場合:

const Discord = require("discord.js"); // use discord.js 
 
var bot = new Discord.Client(); // sets Discord.Client to bot 
 
const BOT_TOKEN = "hidden_here"; 
 
// bot's token 
 
const PREFIX = "~"; // bot's prefix 
 

 
bot.on("ready", function() { // when the bot starts up, set its game to Use 
 
    // *help and tell the console "Booted up!" 
 
    //bot.user.setGame("Use ~info") // sets the game the bot is playing 
 
    console.log("Bot is now online") // messages the console Bot is now online! 
 
    console.log("Use CTRL+C to shut down bot") // messages the console 
 
}); 
 

 

 
bot.on('message', async message => { // when a message is sent 
 
    if (message.author.equals(bot.user)) return; 
 

 
    if (!message.content.startsWith(PREFIX)) return; 
 

 
    var args = message.content.substring(PREFIX.length).split(" "); 
 
    var command = args[0].toLowerCase(); 
 

 
    if (command == "say") { 
 

 
     if (!message.member.roles.some(r => ["Votebot.Start"].includes(r.name))) { 
 
      message.reply("Sorry, you do not have the permission to do this!"); 
 
      return 
 
     } 
 

 
     var sayMessage = message.content.substring(4) 
 
     message.delete().catch(O_o => {}); 
 
     message.channel.send(sayMessage); 
 

 
    } 
 

 
    if (command === "vote") { 
 

 
     if (!message.member.roles.some(r => ["Votebot.Start"].includes(r.name))) { 
 
      message.reply("Sorry, you do not have the permission to do this!"); 
 
      return 
 
     } 
 
    
 
     message.channel.send("Type what you would like the vote to be about") 
 
      .then(() => { 
 
       var vote = message.channel.awaitMessages(response); 
 
       message.updateMessage("message", "Great! Starting a vote in #polls now"); 
 
       message.channel.goto("#polls"); 
 
       message.channel.send(vote) 
 
       message.react('✅'); 
 
       message.react('❎'); 
 
      }); 
 
    } 
 

 
}); 
 

 
bot.login(BOT_TOKEN);

関連する問題