[英]Applying a role if users do not have other roles
Users are still getting the public role regardless of the role they have 用户仍然拥有公共角色,无论他们具有什么角色
I'm sorry if the answer here is fairly obvious but I'm learning javascript as I write this bot. 抱歉,如果这里的答案很明显,但我在编写此机器人时正在学习JavaScript。 I'm aiming for users to be able to do !name and gain a role called "public" as long as they don't have a role listed in the code (General, Captain, etc.). 我的目标是用户只要没有在代码中列出的角色(常规,上尉等),就可以使用!name并获得一个称为“ public”的角色。
client.on('message', async message => {
if (message.channel.id === '535226845654810624'); {
if(message.content.startsWith('!name')) {
if (message.member.roles.some(role => role.name === 'General', 'Captain', 'Lieutenant', 'Sergeant', 'Corporal', 'Recruit'));
const newname = message.content.split(' ').slice(1).join(' ');
message.member.setNickname(newname);
}
else {(message.content.startsWith('!name'));} {
if(message.channel.id === '535226845654810624') {
const newname = message.content.split(' ').slice(1).join(' ');
message.member.setNickname(newname);
const newrole = message.guild.roles.find(x => x.name === 'Public');
message.member.addRole(newrole);
message.delete();
}
}
I'm sure the code is completely ugly. 我确定代码完全丑陋。 I'm still learning. 我还在学习。 Right now regardless of if they have the Gen/Capt/Lieutenant/etc roles they still gain the public role. 现在,无论他们是否具有Gen / Capt / Lieutenant / etc角色,他们仍将获得公共角色。
client.on('message', async message => {
if(message.channel.id === '535226845654810624') {
if (message.content.startsWith('!name')) {
const newname = message.content.split(' ').slice(1).join(' ');
message.member.setNickname(newname);
const newrole = message.guild.roles.find(x => x.name === 'Public');
message.member.addRole(newrole);
message.delete();
}
}
This is the code I had before adding in the attempt to ignore the role add if they have the other roles. 这是我在添加之前尝试忽略角色添加(如果他们还有其他角色)时所拥有的代码。 I'm not sure how to change this to what I'm looking for. 我不确定如何将其更改为所需的内容。
Take a look at the below code, give it a try and let me know what the result is. 看一下下面的代码,尝试一下,让我知道结果是什么。 There were several errors with your code which I'm quite surprised your editor didn't pick up (or atleast didn't error out the code), such as having a semicolon right after defining an if
statement, the extra curly brackets after your else
statement, etc. 您的代码中有几个错误,令您的编辑器没有使用(或者至少没有错误地删除代码),我感到非常惊讶,例如,在定义if
语句后使用了分号,而在您的代码后加上了大括号else
语句等
Anyway, the code below checks if the command entered is !name
and if so, it assigns the new nickname to the user. 无论如何,下面的代码将检查输入的命令是否为!name
,如果是,它将为用户分配新的昵称。 After that it checks if the user has any of the specified roles, and if he does not, he gets a new role 'Public'. 此后,它将检查用户是否具有任何指定角色,如果没有,则将获得新角色“公共”。
client.on('message', async message => {
if (message.channel.id === '535226845654810624') {
if(message.content.startsWith('!name')) {
// Users are allowed to change their nicknames no matter their roles
const newname = message.content.split(' ').slice(1).join(' ');
message.member.setNickname(newname);
// Define the roles which need to be checked
const roleNames = ['General', 'Captain', 'Lieutenant', 'Sergeant', 'Corporal', 'Recruit'];
// If the user does not have any of the roles above, assign them the role 'Public'
if (!message.member.roles.some(role => roleNames.includes(role.name))) {
const newrole = message.guild.roles.find(x => x.name === 'Public');
message.member.addRole(newrole);
message.delete();
}
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.