代码无法编辑现有角色的权限
Code unable to edit perms of an existing role
我正在尝试编写一个机器人来编辑角色 "LFT" 的权限,并在提及角色时使其具有可提及的权限 false
。但是,代码什么也不做。我刚得到一个 DeprecationWarning
。
如果有人能帮助我,那就太好了。
let lftrole = message.guild.roles.find("name", "LFT");
if (message.content.includes('@LFT')) {
lftrole.edit({
mentionable: false
});
console.log(`Role mentionable: False`);
.then(
setTimeout(() => {
let lftrole = message.guild.roles.find("name", "LFT");
lftrole.edit({
mentionable: true
});
console.log(`Role mentionable: True`);
}, 30000)
);
}
您的代码不起作用,因为您正在检查 message.content
是否包含 '@LFT'
:提及项呈现为 @RoleName
,但对于机器人来说它们看起来像 <@&RoleID>
.
您可以尝试解析该模式,但使用 Message.mentions
会更容易。我会这样做:
let lftrole = message.guild.roles.find("name", "LFT");
function changeRole(value = true) {
lftrole.edit({
mentionable: value
});
console.log(`LFT role mentionable: ${value}`);
}
if (message.mentions.roles.has(lftrole.id)) {
changeRole(false);
setTimeout(changeRole, 30000);
}
注意:您获得的 DeprecationWarning
是您使用 Collection.find()
查找角色时获得的。未弃用的方法使您可以使用 returns 元素是否可接受的函数:通过这种方式,您可以组合更多条件。方法如下:
// Instead of using:
let role = guild.roles.find("name", "myRole");
// You can use:
let role = guild.roles.find(r => r.name == "myRole");
// This is useful when you want to check for multiple things,
// or if you want to use other variables from your code
let role = guild.roles.find(r => {
if (otherVariable) return r.name == "myRole";
else return r.name == "myRole" && r.mentionable;
});
我正在尝试编写一个机器人来编辑角色 "LFT" 的权限,并在提及角色时使其具有可提及的权限 false
。但是,代码什么也不做。我刚得到一个 DeprecationWarning
。
如果有人能帮助我,那就太好了。
let lftrole = message.guild.roles.find("name", "LFT");
if (message.content.includes('@LFT')) {
lftrole.edit({
mentionable: false
});
console.log(`Role mentionable: False`);
.then(
setTimeout(() => {
let lftrole = message.guild.roles.find("name", "LFT");
lftrole.edit({
mentionable: true
});
console.log(`Role mentionable: True`);
}, 30000)
);
}
您的代码不起作用,因为您正在检查 message.content
是否包含 '@LFT'
:提及项呈现为 @RoleName
,但对于机器人来说它们看起来像 <@&RoleID>
.
您可以尝试解析该模式,但使用 Message.mentions
会更容易。我会这样做:
let lftrole = message.guild.roles.find("name", "LFT");
function changeRole(value = true) {
lftrole.edit({
mentionable: value
});
console.log(`LFT role mentionable: ${value}`);
}
if (message.mentions.roles.has(lftrole.id)) {
changeRole(false);
setTimeout(changeRole, 30000);
}
注意:您获得的 DeprecationWarning
是您使用 Collection.find()
查找角色时获得的。未弃用的方法使您可以使用 returns 元素是否可接受的函数:通过这种方式,您可以组合更多条件。方法如下:
// Instead of using:
let role = guild.roles.find("name", "myRole");
// You can use:
let role = guild.roles.find(r => r.name == "myRole");
// This is useful when you want to check for multiple things,
// or if you want to use other variables from your code
let role = guild.roles.find(r => {
if (otherVariable) return r.name == "myRole";
else return r.name == "myRole" && r.mentionable;
});