【问题标题】:Check if command is run in a server?检查命令是否在服务器中运行?
【发布时间】:2021-08-24 15:01:51
【问题描述】:

我正在使用 discord js 制作一个机器人,但我想检查该命令是否在特定服务器中运行。我怎么能这样做?

我已经尝试在互联网上搜索,但除了检查成员是否具有权限或角色之外,我找不到任何东西。

【问题讨论】:

    标签: node.js discord.js


    【解决方案1】:

    如果您使用交互(斜杠命令):

    // Where `interaction` is your `Interaction` object
    
    if (interaction.guildId === "your guild id here") {
      // Your guild-specific logic here
    }
    

    或者,如果您使用基于消息的命令:

    // Where `message` is your `Message` object
    
    if (message.guildId === "your guild id here") {
      // Your guild-specific logic here
    }
    

    【讨论】:

      【解决方案2】:

      基于是否要使用斜杠命令。我认为,您是 discord.js 的新手,所以我会推荐普通命令。
      当有人发送消息时,您可以使用message event 运行函数。在函数中,您将消息作为第一个参数。首先,您必须通过.content 属性获取消息内容,然后使用 .startsWith 函数检查它是否是您需要的命令,然后您可以比较从.guildId 属性获得的公会ID .
      代码(未经测试):

      client.on("messageCreate", message => {
          if (message.content.startsWith("your command") && message.guildId === "your guild id") {
              //your code
          }
      })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-04-30
        • 1970-01-01
        • 1970-01-01
        • 2020-01-21
        • 1970-01-01
        • 2014-08-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多