bot.login中的意外标记/将聊天存储为变量

时间:2017-12-16 21:26:46

标签: discord discord.js

我正在尝试制作一个简单的机器人来重新发布用户在特定频道中的输入。我已经工作了几个小时而且已经卡在这里了。我在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

2 个答案:

答案 0 :(得分:0)

我继续编辑你的代码。我没有测试过,所以我不确定它是否会起作用。请问您是否学会正确缩进代码,以免其他开发人员阅读;并且可能使用适当的IDE,如Atom或Visual Studio Code,因此您可以在代码中看到语法错误。记得在变量声明的末尾放置一个分号。此外,您不需要对每一行代码进行评论,只会造成混乱。

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

此外,要存储邮件内容,请执行以下操作:

var content = message.content;

答案 1 :(得分:0)

这会增加运行@JeydinNewWon后出现的错误。 您收到错误“消息未定义”,因为它从未定义过。要解决此问题,我将为您提供应替换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);