参考错误:不和谐机器人中未定义机器人

时间:2021-01-11 10:11:37

标签: node.js discord discord.js

我正在尝试在 repl.it 上制作一个不和谐的机器人,但我无法打开它,这是它所说的:

Promise { <pending> }
Hint: hit control+c anytime to enter REPL.
(node:125) UnhandledPromiseRejectionWarning: Error [TOKEN_INVALID]: An invalid token was provided.
at WebSocketManager.connect (/home/runner/discord-bot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:133:26)
at Client.login (/home/runner/discord-bot/node_modules/discord.js/src/client/Client.js:223:21)
Promise { <pending> }
Hint: hit control+c anytime to enter REPL.
Your Bot is now Online.
/home/runner/discord-bot/index.js:16
setInterval(() => bot.user.setActivity(`${activities[i++ %  activities.length]}`,  {type:"STREAMING",url:"https://www.youtube.com/watch?v=DWcJFNfaw9c"  }), 5000)
                                 ^
    
ReferenceError: bot is not defined
    at Timeout._onTimeout (/home/runner/discord-bot/index.js:16:30)
    at listOnTimeout (internal/timers.js:549:17)
    at processTimers (internal/timers.js:492:7)

这是我的代码:

const Discord = require('discord.js');
const client = new Discord.Client();
const prefix = 'xxx ';
    
var http = require('http');  
http.createServer(function (req, res) {   
    res.write("I'm alive");   
    res.end(); 
}).listen(8080);
    
client.on('ready', () => {
    console.log('Your Bot is now Online.')
    let activities = [`gang shit`, `with the gang`, `with the gang`   ],i = 0;
    setInterval(() => bot.user.setActivity(`${activities[i++ %  activities.length]}`,  {type:"STREAMING",url:"https://www.youtube.com/watch?v=DWcJFNfaw9c"  }), 5000)
});
    
client.on('message', message =>{
    if (!message.content.startsWith(prefix) || message.author.bot) return;
    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if (command === 'ping'){
        message.channel.send('pong!')
    }
}); 
    
client.login('my-token');

别担心,我重新生成了令牌。 请帮助我,这是我的第一个机器人,我不知道我这样做是否正确,如果您有什么建议,请说出来。

2 个答案:

答案 0 :(得分:1)

您的客户被定义为 client。 (const client = new Discord.Client();)

您只需将其从 bot.user.setActivity() 更改为 client.user.setActivity()

答案 1 :(得分:0)

您必须将 bot 更改为 client,正如您定义的 client 而不是 bot

setInterval(() => client.user.setActivity(`${activities[Math.floor(Math.random() * activities.length)]}`,  {type:"STREAMING",url:"https://www.youtube.com/watch?v=DWcJFNfaw9c"  }), 5000)

您还想获得 activities 数组的随机索引。这是通过使用 activities[Math.floor(Math.random() * activities.length)] 来完成的。

编辑:
您的代码现在应如下所示:

const Discord = require('discord.js');

const client = new Discord.Client();

const prefix = 'xxx ';

var http = require('http');  
http.createServer(function (req, res) {   
  res.write("I'm alive");   
  res.end(); 
}).listen(8080);

client.on('ready', () => {
  console.log('Your Bot is now Online.')
  let activities = [`gang shit`, `with the gang`, `with the gang`];
  setInterval(() => client.user.setActivity(`${activities[Math.floor(Math.random() * activities.length)]}`,  {type:"STREAMING",url:"https://www.youtube.com/watch?v=DWcJFNfaw9c"  }), 5000)
});

client.on('message', message =>{
    if(!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if (command === 'ping'){
        message.channel.send('pong!')
 }
}); 

client.login('Your Token');

还要确保使用有效令牌!