簡體   English   中英

如何只允許具有特定權限的用戶切換命令?

[英]How do I only allow a command to be toggled by users with certain permissions?

我編寫了一個可以格式化文本的機器人 - 我需要它以便不僅僅是隨機數可以訪問命令。 這是我的代碼 - 它不會引發任何錯誤

try {
    if(message.member.guild.me.hasPermission('ADMINISTRATOR')) {
      if (args[0] === 'off') {
        christian[message.guild.id] = false
      } else if (args[0] === 'on') {
        christian[message.guild.id] = true
      }
      message.channel.send(`Christian Mode ${christian[message.guild.id] ? 'Active' : 'Inactive'}`)
      return christian
    } else {
      message.channel.send('You do not have permission to use that command!')
    }
  } catch (error) {
    console.log('Permission Error')
  }

注意:通過說 =Christian on/off 這個函數有效,命令被切換回 index.js (main)。

message.member.guild.me.hasPermission('ADMINISTRATOR')正在檢查機器人的權限,而不是命令執行者的權限。 message.member上調用GuildMember#hasPermission() (參見Message#member )。

例如...

if (!message.member.hasPermission('ADMINISTRATOR')) {
  // return an error to the user (and actually return)
}

// continue with command code

注意: Discord.js 文檔超鏈接適用於最近發布的 v12。 如果您的 Discord.js 不是最新的,請切換到頁面頂部的正確版本以獲取准確信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM