如何使用channelid获取特定的Discor Bot频道?

时间:2020-01-15 02:37:01

标签: discord.js

我的机器人在使用undefined时返回bot.channels.get(channelid)

这是我的代码示例:

//this is executed in a guild with id 416809320513011713
const Discordjs = require("discord.js");
const bot = new Discordjs.Client();
const auth = require('./auth.json');
const FileSys = require("fs");

bot.on('messageDelete', async function (message) {
  console.log('message deleted')
  let currentguildsettings = JSON.parse(FileSys.readFileSync('./DatStore/GuildDat/' + message.guild.id + '.dat', 'UTF8'));
  if (currentguildsettings[0] == 1) {
    if (currentguildsettings[1] != 0) {
      let channelid = currentguildsettings[1].toString()
      let channel = bot.channels.get(channelid);
      console.log('settings on true, channelid ' + channelid)
      if (channel) {
        console.log('channel found')
      }
    }
  }
}

bot.login(auth.token)

文件./DatStore/GuildDat/416809320513011713.dat包含:

[1,424085503361417200,0]

这是输出:

message deleted
settings on true, channelid 424085503361417200

如果找到了频道,则应该在输出中记录“找到频道”

我应该做些什么才能使其返回频道?

2 个答案:

答案 0 :(得分:2)

channel id键是一个字符串,必须将它作为字符串括在数组中。

let c1 = bot.channels.get(424085503361417200); // Will produce undefined
let c2 = bot.channels.get('424085503361417200'); // Will produce a channel object (if available)

答案 1 :(得分:0)

该频道不可用,因为我使用了错误的ID:

我在服务器设置命令中以int格式(而不是字符串)保存了ID,parseInt()打破了确切的数字。

if (logchannelaction == 'set') {
  if (currentguildsettings[1] == message.channel.id) return message.reply("logchannel was already set to this channel");
  currentguildsettings[1] = parseInt(message.channel.id);
  message.reply("logchannel has set to #" + message.channel.name);
  if (currentguildsettings[0] == 0) {
    currentguildsettings[0] = 1
    message.reply("logchannel has automatically enabled");
  }
  FileSys.writeFileSync(guilddatpath, JSON.stringify(currentguildsettings));
  return
}

感谢您尝试帮助我。