我设置了一个机器人,因此每当有人输入t!accept @user [昵称]时,它就会向用户添加 Angular 色并更改用户的昵称。这是我的代码:
mention = message.mentions.members.first();
if (msg.startsWith (prefix + "accept")){
if(!message.member.hasPermission("MANAGE_ROLES")) return message.reply("You have no permission!");
if (mention == null) { return; }
let args = message.content.split(" ").slice(2);
mention.roles.add('123456789')
mention.setNickname(mention.displayName+" "+args.join(' '))
mention.send("Congratulations");
try{
await message.author.send("Sent.")
}catch(e){
message.channel.send("An error has occured.")
}
}
但是,只有大多数时候它才能完全正常工作,有时它不会同时完成这两件事,有时它只会执行其中一项任务(添加 Angular 色,但不添加昵称,反之亦然)
我的代码有什么问题吗? 提前致谢!
最佳答案
您有时提供的昵称可能太长,超出了 32 个字符的不和谐昵称长度限制。您应该添加以下检查:
if ((mention.displayName + ""+ args.join("")).length > 32) return message.channel.send("昵称超过32个字符")
然而,这当然不可能,我会事先发表评论,但不幸的是我没有足够的声誉。
关于javascript - 有时机器人不会添加角色并设置昵称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61042612/