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

这是在运行@JeyDinNewwon后遇到的错误。您正在收到错误"未定义",因为它从未定义。为了解决此问题,我将为您提供正确的代码行,您应该将其替换为bot.on("message", () => {}

bot.on('message', async 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);

我继续编辑您的代码。我没有测试过,所以我不确定它是否可以使用。我可以请您学会正确地缩进您的代码,以免其他开发人员阅读的努力;也许使用适当的IDE(例如Atom或Visual Studio代码),因此您可以在代码中看到语法错误。切记在变量声明的末尾放置一个半龙。另外,您无需对每条代码行的每一行评论,它只是创建混乱。

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

最新更新