【问题标题】:discord.js Javascript string manipulationdiscord.js Javascript字符串操作
【发布时间】:2021-08-27 06:05:36
【问题描述】:

所以我正在创建一个带有 kick 命令的机器人,并希望能够为所述操作添加一个原因,我从某个地方听说我可能需要进行字符串操作。目前我有一个独立的原因,如下面的代码所示:

client.on("message", (message) => {
  // Ignore messages that aren't from a guild
  if (!message.guild) return;

  // If the message starts with ".kick"
  if (message.content.startsWith(".kick")) {
    // Assuming we mention someone in the message, this will return the user
    const user = message.mentions.users.first();
    // If we have a user mentioned
    if (user) {
      // Now we get the member from the user
      const member = message.guild.member(user);
      // If the member is in the server
      if (member) {
        member
          .kick("Optional reason that will display in the audit logs")
          .then(() => {
            // lets the message author know we were able to kick the person
            message.reply(`Successfully kicked ${user.tag}`);
          })
          .catch((err) => {
            // An error happened
            // This is generally due to the bot not being able to kick the member,
            // either due to missing permissions or role hierarchy
            message.reply(
              "I was unable to kick the member (this could be due to missing permissions or role hierarchy"
            );
            // Log the error
            console.error(err);
          });
      } else {
        // The mentioned user isn't in this server
        message.reply("That user isn't in this server!");
      }
      // Otherwise, if no user was mentioned
    } else {
      message.reply("You didn't mention the user to kick!");
    }
  }
});

【问题讨论】:

    标签: javascript discord.js


    【解决方案1】:

    我假设你希望你的完整命令看起来像

    .kick @user Being hostile to other members
    

    如果您想假设命令中未提及或“.kick”命令的所有内容都是原因,那么要从该字符串中获取原因,您可以执行一些简单的字符串操作来提取命令并从字符串中提及,并保留其他所有内容。

    从未使用过 Discord API,但根据我从文档中拼凑出来的内容,这应该可以工作。

    let reason = message.content.replaceAll(".kick", "")
    message.mentions.forEach((mentionedUser) => reason.replaceAll("@" + mentionedUser.username, "")
    // assume everything else left in `reason` is the sentence given by the user as a reason
    
    if (member) {
        member
          .kick(reason)
          .then(() => {
            // lets the message author know we were able to kick the person
            message.reply(`Successfully kicked ${user.tag}`);
          })
    }
    

    【讨论】:

    • 快速提问我将在哪里实现它
    【解决方案2】:

    拆分 message.content 并切片前 2 个数组元素,这将为您留下构成原因的元素。将剩余的元素连接回一个字符串。

    const user = message.mentions.users.first();
    const reason = message.content.split(' ').slice(2).join(' ');
    

    【讨论】:

      【解决方案3】:

      这里有一些帮助:

      const args = message.content.slice(1).split(" "); //1 is the prefix length
      const command = args.shift();
      //that works as a pretty good command structure
      if(command === 'kick') {
      const user = message.mentions.users.first();
      args.shift();
      const reason = args.join(" ");
      user.kick(reason);
      //really close to Elitezen's answer but you might have a very terrible problem
      //if you mention a user inside the reason, depending on the users' id, the bot could kick 
      //the user in the reason instead!
      }
      

      以下是解决该问题的方法(使用正则表达式)

      const userMention = message.content.match(/<@!?[0-9]+>/);
      //you may have to do some more "escapes"
      //this works since regex stops at the first one, unless you make it global
      var userId = userMention.slice(2, userMention.length-1);
      if(userId.startsWith("!")) userId = userId.slice(1);
      const user = message.guild.members.cache.get(userId);
      args.shift();
      args.shift();
      user.kick(args.join(" "))
      .then(user => message.reply(user.username + " was kicked successfully"))
      .catch(err => message.reply("An error occured: " + err.message))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-26
        • 2019-04-16
        • 2022-08-03
        • 2019-11-01
        相关资源
        最近更新 更多