【问题标题】:How to define a particular prefix that can only be used by the owner of the bot?如何定义只能由机器人所有者使用的特定前缀?
【发布时间】:2022-01-27 07:53:08
【问题描述】:

我正在尝试设置另一个前缀,但只能由我使用,同时为其他用户保留常用前缀。

client.on('message', async message => {
  const prefix = '.' 
  if(message.content.startsWith(prefix)) {
    const messageArray = message.content.split(' ');
    const cmd = messageArray[0];
    const args = messageArray.slice(1);
    const command = client.commands.get(cmd.slice(prefix.length))
                 || client.commands.get(client.aliases.get(cmd.slice(prefix.length)));
    // Command execution
});

【问题讨论】:

  • 您只需要添加另一个条件,例如:if(message.content.startsWith(prefix) || (owner && message.content.startsWith(ownerPrefix)) {}

标签: javascript discord.js prefix owner


【解决方案1】:

您可以使用ternary operator 轻松分配前缀,具体取决于用户是否为所有者,ternary operator 经常用作if...else 语句的替代。

如果消息作者不是所有者,则前缀将是默认的,否则将是所有者前缀:

const prefix = message.author.id !== OWNER_ID ? '.' : OWNER_PREFIX; 

所有者可以使用两个前缀,但用户只能使用默认的一个:

const prefix = message.content.startsWith('.') ? '.'
             : message.author.id === OWNER_ID ? OWNER_PREFIX
             : '.';

【讨论】:

    猜你喜欢
    • 2018-02-11
    • 2021-04-25
    • 2021-06-19
    • 1970-01-01
    • 2018-11-03
    • 1970-01-01
    • 2023-03-22
    • 2013-02-07
    • 1970-01-01
    相关资源
    最近更新 更多