javascript - 为什么 .get() 在我的不和谐机器人代码中不起作用?

标签 javascript node.js discord.js

我正在尝试更改 channel 名称以显示公会中的用户数量。但是,当我启动机器人时,它在控制台中显示以下内容:

Cannot read property 'edit' of undefined

我将问题范围缩小到这一行:bot.channels.get("533334637163053077");

以防万一您需要它,这是我的代码:

const Discord=require('discord.js');
const fs = require("fs");
const bot = new Discord.Client();
const PREFIX = "-";
const guildID = "418505146876559371";
let ref = require("./ref.json");
let request = require(`request`);
const memberCountChannel = bot.channels.get("533334637163053077"); //This one is the one
//that is not working I think
bot.commands = new Discord.Collection();

var servers = {};

bot.on('warn', console.warn);
bot.on('error', console.error);

bot.on('ready', function(guild)
{
    console.log(`${bot.user.username} is online with ${bot.users.size} 
    members.\n\n\n\n\n\n\n\n\n`);
    bot.user.setActivity('with the server. Type -help for info.');
    memberCountChannel.edit(`Members: ${guild.memberCount}`, 8000); //Change name of channel
});
bot.on("guildMemberAdd", function(member){
    memberCountChannel.edit(`Members: ${guild.memberCount}`, 8000);//Change name of channel
});

bot.on("guildMemberRemove", async function(member){
    memberCountChannel.edit(`Members: ${guild.memberCount}`, 8000);//Change name of channel
});
bot.login('TOKEN HERE');

编辑:该 channel 确实存在,并且机器人确实有权使用该 channel 执行所有操作。

最佳答案

本质上问题是,正如您正确推测的那样

const memberCountChannel = bot.channels.get("533334637163053077");

原因是您试图在 .on('ready') 事件触发之前不存在的对象( channel )上调用方法(.get)。

关于javascript - 为什么 .get() 在我的不和谐机器人代码中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54151917/

相关文章:

javascript - 如何在 D3.js 中应用旋转变换而不改变坐标系?

javascript - 在 AngularJS 中,如何在 POST 后获得响应?

javascript - Node.JS HTTPS 问题 TypeError : Cannot read property 'writeQueueSize' of null

node.js - postgres SELECT 查询返回不可用的结果

javascript - 获取用户的公会昵称

javascript - 在 javascript 中获取一个数组中的内容而不是另一个数组中的内容

javascript - 无法读取未定义的不一致嵌入的属性 'includes' 来检测表情符号

javascript - 将图像设置为页面在 React 中呈现的第一张图像

javascript - 如何通过窗口 :scroll target using @HostListener to trigger an Observable. fromEvent() 订阅?

node.js - Redis 在哈希列表中按喜欢排序?