javascript - 我在 discord.js-commando 中发出了一个命令来宣布我的机器人所在的所有频道。但我收到了一个错误
问题描述
const Commando = require('discord.js-commando');
module.exports = class AnnounceCommand extends Commando.Command {
constructor(client) {
super(client, {
name: 'announce',
aliases: ['an'],
group: 'util',
memberName: 'announce',
description: 'Announce something to the topmost channel in all servers the bot is in.',
details: 'Only the bot owner may use this command.',
examples: ['announce Hello, world!'],
args: [
{
key: 'msg',
label: 'message',
prompt: 'What should be announced?',
type: 'string'
}
],
guarded: true,
});
}
hasPermission(msg) {
return this.client.isOwner(msg.author);
}
async run(msg, args) {
this.client.guilds.array().forEach((guild) => {
let channels = guild.channels.filter((channel) => {
return channel.type === 'text' && channel.permissionsFor(guild.me).has(['VIEW_CHANNEL', 'SEND_MESSAGES']);
});
if (channels.array().length > 0) {
channels = channels.sort((a, b) => {
return a.calculatedPosition - b.calculatedPosition;
}).array();
channels[0].send(args.msg).catch(e => console.error(e));
}
});
}
};
但在控制台我得到:
TypeError: this.client.guilds.array 不是 AnnounceCommand.run (/home/runner/emperor-2/commands/util/announce.js:30:22) 的函数
解决方案
GuildManager 类的属性 guilds 没有 array() 方法。正确的行代码应该是this.client.guilds.cache.forEach()
.
我建议你阅读文档:Class GuildManager
推荐阅读
- ruby-on-rails - 如何根据activerecord中一行的值排除符合条件的行?
- html - 如何在一行上有 3 个图像,而在另一行有 2 个图像?
- c++ - 每次运行“return main();”时如何使变量增加
- python - Python 3.7 中的缓存整数、`is` 运算符和 `id()`
- c - 排序函数在循环中无法正常工作
- c# - InstantiateTemplateRequest 可以返回多个值吗?
- javascript - 下拉菜单按钮保持打开状态,不会关闭
- python - Pycharm 不加载找到我的静态文件
- javascript - Vue.js 从数组中弹出一个嵌套对象
- asp.net - IdentityServer4 与 Active Directory 集成?