【问题标题】:ReferenceError: bot is not defined in discord botReferenceError: bot 未在 discord bot 中定义
【发布时间】:2021-04-16 06:40:40
【问题描述】:

我正在尝试在 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');

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

【问题讨论】:

    标签: node.js discord discord.js


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      您必须将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');
      

      还要确保使用有效令牌!

      【讨论】:

      • 谢谢你,很抱歉回答我自己的问题
      猜你喜欢
      • 2021-10-28
      • 1970-01-01
      • 2019-03-25
      • 2022-01-13
      • 2021-12-20
      • 2021-04-02
      • 2020-10-13
      • 2021-03-26
      • 2021-08-07
      相关资源
      最近更新 更多