【问题标题】:Finding a discord role and returning a message if they don't have the role找到不和谐的角色并在他们没有角色时返回消息
【发布时间】:2019-12-05 08:18:34
【问题描述】:

我相信在此之前我有一条工作线会检查名为 Motion Design 的用户角色,然后如果他们继续,但如果他们没有,则向频道发送消息。即使他们有角色,它似乎总是将消息发送到频道。 ****** 是应该检查角色的行。

我尝试重命名角色并更改代码,但没有任何效果。

if (messageReaction.emoji.name === reactions.success) {




        let client1 = messageReaction.users

        const notbot = messageReaction.users.filter(clientuser => clientuser !== client.user).first();
        await messageReaction.remove(notbot);  
            //check if they have role id
********        if(!message.member.roles.find(r => r.name === "Motion Design")) return client.channels.get(`${ticketchat}`).send(`${notbot} You do not have the requested role for that ticket`);

        const defbot = messageReaction.users.filter(clientuser => clientuser == client.user).first();
        await messageReaction.remove(defbot); 


        let web1 = new Discord.RichEmbed()
        .setDescription(`This commission has been claimed by ${notbot} .`)
        .setColor("#15f153")
        .addField("Client", `${message.author} with ID: ${message.author.id}`)
        .addField("Budget", `${budget}`)
        .addField("Time", message.createdAt)
        .addField("Budget", `${budget}`)
        .addField("Requested Freelancer",`<@&${motionDesign}>`)

        let tickets = message.guild.channels.find('name', "tickets")
        if(!tickets) return message.channel.send(`${message.author} Can't find tickets channel.`)
        message.delete().catch(O_o=>{});

        message.author.send(`Freelancer ${notbot} has taken your ticket. ${message.author}`);

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    你可以在*********使用这个:

    if(!message.member.roles.has("ROLE_ID"))

    但如果你仍然想通过名字来获取角色:

    if(!message.member.roles.has(message.guild.roles.find(r =&gt; r.name === "ROLE_NAME"))

    【讨论】:

    • 使用message.member.roles.find(...) 代替按名称搜索。
    【解决方案2】:

    您应该使用!message.member.roles.get(ROLE_ID),这样即使您决定更改角色名称也不会中断。这也更可靠。如果还想用角色名,就不需要那个箭头功能了,直接!message.member.roles.find('name', ROLE_NAME)就行了

    【讨论】:

    • 你不应该使用 roles.find("name", ROLE_NAME) 因为它已被弃用。他现在使用 .find 的方式是正确的方式。
    • @Xge 你在哪里发现它被弃用了?在 discord.js documentation 上,没有任何说明它已被弃用。如果是,我希望 find 示例被删除。
    • 如果您在代码中使用此 (.find('name', ROLE_NAME)),它会显示以下内容:DeprecationWarning: Collection#find: pass a function instead since api-version 11。
    • 澄清一下,Collection.find() 作为一个整体 没有被弃用,只是旧的Collection.find('property', 'value') 用法。使用这样的谓词函数:Collection.find(element =&gt; element.property === value).
    猜你喜欢
    • 2020-12-25
    • 2017-05-18
    • 2022-01-23
    • 2021-03-05
    • 2020-10-24
    • 1970-01-01
    • 2021-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多