尝试获取频道时未定义客户端

时间:2020-05-29 19:29:51

标签: javascript discord.js

我试图在两次之间锁定频道,但是我得到的只是一个错误。我是制作不和谐机器人的新手,所以这是我的第一个。我的错误在哪里,如何解决它们?为什么我的代码不起作用?

Error:

    ReferenceError: client is not defined
    at getChannel (C:\Users\<User>\DISCORDBOT\main.js:42:2)
    at checkTime (C:\Users\<User>\DISCORDBOT\main.js:48:3)
    at Object.<anonymous> (C:\Users\<user>\DISCORDBOT\main.js:53:1)
    at Module._compile (internal/modules/cjs/loader.js:1138:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
    at Module.load (internal/modules/cjs/loader.js:986:32)
    at Function.Module._load (internal/modules/cjs/loader.js:879:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

代码:

var Discord = require('discord.io');
var auth = require('./auth.json');
// Initialize Discord Bot
var bot = new Discord.Client({
  token: auth.token,
  autorun: true
});
bot.on('ready', function (evt) {
  bot.sendMessage({
    to: '<ID>',
    message: 'Bot Running!'
  });
})
bot.on('message', function (user, userID, channelID, message, evt) {
  // Our bot needs to know if it will execute a command
  // It will listen for messages that will start with `!`
  if (message.substring(0, 1) == '?') {
    var args = message.substring(1).split(' ');
    var cmd = args[0];

    args = args.splice(1);
    switch (cmd) {
      // !ping
      case 'ping':
        bot.sendMessage({
          to: channelID,
          message: 'Pong!'
        });
        break;
      case 'getinfo':
        bot.sendMessage({
          to: channelID,
          message: 'User Requested Channel data. User: ' + user + ', UserID: ' + userID + ', in ChannelID: ' + channelID
        });
        break;
      // Just add any case commands if you want to..
    }
  }
});

function getChannel(ID) {
  client.channels.get(ID)
}
function checkTime() {
  var today = new Date().getHours();
  console.log(today)
  if (today >= 7 && today <= 22) {
    getChannel("<ID>").updateOverwrite(getChannel("<ID>").guild.roles.everyone, { VIEW_CHANNEL: false });
  } else {

  }
}
checkTime()

修改: 我收到一个新错误:

TypeError: Cannot read property 'get' of undefined
    at getChannel (C:\Users\<User>\DISCORDBOT\main.js:42:15)
    at checkTime (C:\Users\<user>\DISCORDBOT\main.js:48:3)
    at Object.<anonymous> (C:\Users\<User>\DISCORDBOT\main.js:53:1)
    at Module._compile (internal/modules/cjs/loader.js:1138:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
    at Module.load (internal/modules/cjs/loader.js:986:32)
    at Function.Module._load (internal/modules/cjs/loader.js:879:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

似乎无法获得频道...有人知道解决方法吗? 我不知道API的数量,我真的一点也不了解。抱歉,如果您看到我忽略的非常明显的错误

1 个答案:

答案 0 :(得分:0)

那是因为您将其定义为bot而不是client

function getChannel(ID) {
    bot.channels.get(ID)
}