当前位置: 首页 > 知识库问答 >
问题:

为什么不应用权限(静音)?

丁雅惠
2023-03-14

我正试图制造一个基本的不和。jsbot,我想添加一个mute命令。

我已经试过了(下面的代码)。这就分配了一个“foca mutted you oof”的角色(我知道名字很奇怪。)这不允许你做任何事。但在我的测试帐户上,即使它分配了那个角色,我仍然可以非常精细地键入消息。

主要的js:

    } else if(cmd === `${prefix}mute`) {
        if(!msg.member.hasPermission(["MANAGE_MESSAGES"])) return msg.channel.send("no no no, you can't manage messages. ok? alright?");
        let toMute = msg.guild.member(msg.mentions.users.first());
        if(toMute.user.username === msg.author.username) return msg.channel.send("oof. u can't mute urself.");
        if(!toMute) return msg.channel.send("idk who to mute. does that person exist????");

        let role = msg.guild.roles.find(r => r.name === "foca muted you oof");
        if(!role) {
            try {
                role = await msg.guild.createRole({
                    name: "foca muted you oof",
                    color: "#000000",
                    permissions: []
                });
                msg.guild.channels.forEach(async (channel, id) => {
                    await channel.overwritePermissions(role, {
                        "SEND_MESSAGES": false,
                        "ADD_REACTIONS": false
                    });
                })
            } catch(e) {
                console.log(e.stack);
            }
        }
        if(toMute.roles.has(role.id)) return msg.channel.send("oof. this user is already muted. tried to do a bravery, huh?");
        await toMute.addRole(role);
        msg.channel.send(`${toMute.user.username} has been muted.`);
    }

你能告诉我怎么了吗?

共有1个答案

谯振国
2023-03-14
}else if(cmd === `${prefix}mute`){
  if(!msg.member.hasPermission(["MANAGE_MESSAGES"])) return msg.channel.send("You don't have permission to use this command."); //Check for permission.
  const toMute = msg.mentions.members.first(); //Get first mention of member.
  if(!toMute) return msg.reply("Member doesen't exist."); //If there's no member.
  if(toMute.user.id == msg.author.id) return msg.reply("You can't mute yourself."); //If member is author.
  const role = msg.guild.roles.get('Muted'); //Fetch the role.
  if(!role){ //If role doesen't exist create one.
    try{ //Try block which stops code in case of error.
      await msg.guild.creatRole({ //Create the role.
        name: 'Muted',
        color: '#F87666',
        permissions: ['SEND_MESSAGES': false, 'ADD_REACTIONS': false]
      });
      await msg.guild.channels.forEach(async (channel) => { //Overwrite every permission in every channel.
        'SEND_MESSAGES': false,
        'ADD_REACTIONS': false
      });
      await toMute.removeRoles(toMute.roles); //Remove every role member has.
      await toMute.addRole(role.id); //If everything went good add muted role to member.
      return msg.reply('Member muted successfully!')
    }catch(e){ //In case of error execute the code inside.
      msg.reply(`Error: ${e.message}`); //Tell author about error.
    }
  }else{ //If role exists
    if(toMute.roles.has(role.id)) return msg.reply('Member is already muted.'); //If member is muted reply.
    await toMute.removeRoles(toMute.roles);
    await toMute.addRole(role.id);
    return msg.reply('Member muted successfully!')
  }
}

我还没测试过,应该可以。享受吧

许可证:https://discord.js.org/#/docs/main/stable/class/GuildMember?scrollTo=hasPermission

获取函数而不是查找,因为它已弃用(很快将被删除):https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get

createRole:https://discord.js.org/#/docs/main/stable/class/Guild?scrollTo=createRole

清除孔:https://discord.js.org/#/docs/main/stable/class/GuildMemberscrollTo=removeRoles

添加角色:https://discord.js.org/#/docs/main/stable/class/GuildMember?scrollTo=addRole

有:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/has

forEach:https://www.w3schools.com/jsref/jsref_foreach.asp

角色的随机红色:https://coolors.co/

 类似资料:
  • 是否还需要权限?我需要粗略位置的用例是什么?

  • 我已经在我的应用程序清单文件中列出了RECEIVE_SMS和READ_SMS的权限,它们也有不同的权限字符串。 授予相应的权限。然而,我注意到,在授予(用户)任何一个权限(或)时,我们都可以执行这两项任务。我的问题是,如果他们两人执行不同的任务: 1):它允许应用程序读取用户手机上的所有短信(当前存在)。 2) :它允许应用程序在用户使用应用程序时收听用户手机上接收到的所有短信。 在请求权限和拒绝

  • 11.6.3.为Receiver应用权限机制 我们还需要检查Receiver得到的广播是否合法。为此,在注册Receiver时为它添加上相关的权限信息。 例 11.13. TimelineReceiver in TimelineActivity.java ... public class TimelineActivity extends BaseActivity { static final St

  • 11.6.2.为UpdaterService应用权限机制 UpdaterService会在每次收到新数据时广播一条Intent,但我们不希望任何人都可以收到这条Intent,因此限制只有拥有授权的Receiver才可以收到这条Intent。 例 11.12. UpdaterService中的内部类Updater ... private class Updater extends Thread {

  • 使用指南 - 数据报告 - 概述 - 为什么有些报告没有权限 在百度统计中,无权限报告所属的站点应是“权限站点”,权限站点在右上角站点选择下拉框中有“权限站点”的字样标注,如图所示: 权限站点是其他账户将其自有站点授权给您查看数据的站点,在授权时定义了您的查看和设置权限。如果此报告无权限,说明授权发起者未授权给您。 您可以在“管理->网站列表”的“权限网站”标签下,查看该站点的权限来源账户,如需开

  • 我已经根据新材料设计集成了Marshmallow权限对话框,但权限对话框正在模拟器上工作,而不是在真实设备上工作。而且我也没有得到任何错误。任何人都可以帮我吗? 我正在使用以下代码:- ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.pe