简体   繁体   English

如果用户没有其他角色,则应用角色

[英]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.

相关问题 仅为discord.js中具有1+角色的用户分配角色? - Only assign role to users with 1+ roles in discord.js? 如果其他用户具有管理员角色,我如何才能使我的管理页面(用于从数据库中删除用户)不在列表中显示其他用户? - How can I make my admin page (for removing users from DB) not display other users in the list if they have an admin role? Discord.JS | 显示具有特定角色的用户 - Discord.JS | Display Users who have a specific roles 角色寻求认为,当他们没有说角色时,所述成员已经说角色 - Role seeking believes that said members have said role when they do not 使用用户角色重用应用程序 - React application with users roles 列出具有特定角色的用户(其中一些未出现在列表中) - Listing users with specific role (some of them do not appear on list) jQuery:在模态上构建一个表单,其中角色字段为select且用户字段为其他。 如果在其他选择上选中该角色,则必须禁用该角色 - jQuery: Building a form on modal with role field as select and user field as other. Have to disable role if it is selected on other select 如果用户的积分不足,则提醒用户 - Alert users if user do not have enough points 如果我的用户有ie6怎么办 - what to do if my users have ie6 列出角色中的所有用户 - List all users in a role
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM