【问题标题】:How to send a message if user has the permission如果用户有权限如何发送消息
【发布时间】:2020-10-14 20:24:15
【问题描述】:

好的,我正在尝试做一个宣布命令,让机器人说出用户要求它写的内容, 该命令工作正常,但主要问题是我不希望不是版主/管理员的用户使用此命令我尝试使用 if (user.hasPermission("KICK_MEMBERS") 但我根本不知道如何在我的代码中实现它,(在这里是)

const Discord = require('discord.js');
module.exports = {
    name: 'announce',
    description: "announce",
    execute(message, args){
        const developerEmbed = new Discord.MessageEmbed()
        .setColor('#0099ff')
        .setTitle(`failed to send message`)
        .setAuthor('♥Aiko♥', 'https://i.imgur.com/1q9zMpX.png')
        .setDescription('please mention the channel first(if this promblem persists contact the developer)')
        .setTimestamp()
        .setFooter(`©Revenge#7005 | Requested by ${message.author.tag}.`);
        if(message.mentions.channels.size === 0) {
            message.channel.send(developerEmbed);
        }
        else {
            let targetChannel = message.mentions.channels.first();
    
            const args = message.content.split(" ").slice(2);
            let userMessage = args.join(" ");
            targetChannel.send(userMessage);
            message.delete();
            
        }
    }
}

是的,有什么想法可以让机器人检查权限,然后在用户有权限的情况下发送消息吗? 我对编码很陌生,这个机器人是我的第一个更大的项目,如果这个问题看起来很愚蠢,很抱歉

【问题讨论】:

    标签: node.js discord.js


    【解决方案1】:

    确保使用message.member 代替user,并且应该在代码的开头实现它。

    module.exports = {
     name: 'announce',
     description: "announce",
     execute(message, args) {
      if (!message.member.hasPermisson('KICK_MEMBERS')) // if member doesn't have permissions
       return message.channel.send('Insufficient Permissions');
     
      // rest of your code...
    

    【讨论】:

    • 我已经尝试过了,我收到了这个错误消息TypeError: message.member.hasPermisson is not a function
    猜你喜欢
    • 2021-09-30
    • 2021-08-23
    • 2020-09-28
    • 1970-01-01
    • 2018-07-25
    • 1970-01-01
    • 2021-05-20
    • 2020-11-07
    • 1970-01-01
    相关资源
    最近更新 更多