javascript - 不和谐.js | 从成员中删除角色并将该角色赋予另一个成员
问题描述
我正在尝试从一个人中删除一个角色,并将该角色赋予发起该命令的人。
当 A 人这样做时,它第一次起作用。但是当其他人第二次尝试调用该命令时,代码找不到人 A。任何帮助都会很酷。
//Just some startup stuff
const { error, timeStamp } = require('console'),
client = new Discord.Client(),
client.login('How About No?')
var command, role, person
//Check if it's alive
client.once('ready', () => {
console.log(`${client.user.tag} is online`)
})
//When a message is sent
client.on('message', message => {
command = message.content.toLowerCase()
if(command === 'test' && message.guild.id === 'the server ID'){
role = message.guild.roles.cache.find(r => r.id === 'the role ID') //Find the correct role
person = role.members.first()
/* I can use ".first()" here, because there's never supposed to be more than one person with this role.
The problem is here. On the second run, it outputs "undefined" even though a person has the role. */
//If a person has the role, remove it from them.
if(typeof person !== 'undefined') person.roles.remove(role).catch(error => console.log(error))
//Give the role
person = message.member
person.roles.add(role).catch(error => console.log(error))
}
})
解决方案
推荐阅读
- kubernetes - Prometheus 警报管理器未发送警报
- android-studio - 在 android studio 中使用以前版本的 gradle 创建项目
- oracle - Can we change the interval for a partition?
- ios - Admob w/Spritekit
- facebook - Facebook Bot 附件不可用问题
- php - 如果特定列中没有重复项,则在 codeigniter 中插入批处理
- c# - 将子项添加到 EntryDirectory 错误
- angular - 过渡动画在 Angular 5 中不起作用
- forms - 在动态生成的控件上使用事件变量
- ios - 如何在 iOS 中将 base64 字符串发送到服务器?