【发布时间】:2020-08-22 19:24:27
【问题描述】:
我正在尝试在 discord.js 机器人中创建一个命令,当使用 d!noattention member 时,它会获取服务器中在其用户名或昵称开头具有特殊字符的所有用户并将他们的昵称更改为member 我的代码是:
const options = [
"!",
"?",
'/',
'\'',
'~',
'#',
'@',
'+',
'=',
'(',
'*',
'&',
'^',
'%',
'$',
'[',
'`',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'_',
'-',
'.',
'¿'
];
module.exports = {
name: "noattention",
aliases: ["attentionnick", "anick"],
description: "Change nickname of all the attentions seekers to something you want.",
category: "moderation",
usage: "<new nickname>",
run: async(client, message, args) => {
if(!message.member.hasPermission(["MANAGE_NICKNAMES"])) return message.reply("You do not have permission to use this command.").then(m => m.delete(15000));
const name = args.join(" ");
if(!name) return message.reply("Also provide the new nickname you want for attention seekers.").then(m => m.delete(15000));
for (i = 0; i < options.length; i++) {
const user = message.guild.members.filter(m => m.displayName.startsWith(options[i]));
user.forEach(u => u.setNickname(name));
}
message.channel.send(`Successfully changed nicknames of all attention seeking members`);
}
}
它运行良好,但我也希望它发送给我并告诉我有多少用户的昵称已更改。当我尝试将message.channel.send 代码放在for (i = 0; i < options.length; i++) 下以便它可以获得user 的值时,机器人开始发送垃圾邮件并且没有说明用户数量。
user.length 在这里不工作。
【问题讨论】:
-
欢迎@Ronak。尝试确保您的代码易于阅读,并且仅包含与问题相关的部分。这样你更有可能得到好的答案。当我打开这个问题时,选项数组占据了大部分。它甚至似乎与问题无关。运行函数的几行需要水平滚动才能阅读。如果你让回答变得容易,你更有可能得到好的答案。
标签: javascript arrays node.js discord discord.js