如何使用频道ID获取特定的不和谐机器人频道?



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

这是我的代码示例:

//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

如果找到通道,它应该在输出中记录"找到通道"。

我应该更改什么才能使其返回频道?

通道 id 键是一个字符串,您必须将其作为字符串包含在数组中。

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

由于我使用了错误的 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
}

谢谢你试图帮助我。

最新更新