【问题标题】:Take two variables from a message and put each into different parts从消息中获取两个变量并将每个变量放入不同的部分
【发布时间】:2022-01-25 08:54:06
【问题描述】:

我正在尝试在discord.js 中发出命令。

我想在输入 ?party (link or something here) requirements: be cool 的地方设置它,然后它会说

@user 已 ping 通派对链接:(已发布的链接)要求: 冷静点

我是discord.js 的新手,所以我正在努力学习。谢谢!

【问题讨论】:

  • 那么……您希望?party (link or something here) requirments: be cool 成为2 个不同的命令?
  • 我希望它获取链接部分和需求部分(在同一命令中),然后将它们放入返回消息的不同部分。示例:@user 发起了一个聚会 链接:(这里是命令的链接部分) 要求:(这里是命令的要求部分)
  • 到目前为止你有什么?
  • @skara9 | if(message.content.startsWith("*party")) { let link = message.content.split(" ").slice(1).join(" ") if(!link) link = "no link" let req = message.content.split(" ").slice(2).join(" ") if(!req) req = "no req" else { message.channel.send(has pinged for party\n \nLink: ${link}\n \nRequirements: ${req}\n \n@everyone) } } |这是一堆垃圾
  • @user17758804 不要在 cmets 中发布代码,只需更新您的问题

标签: javascript discord.js


【解决方案1】:

好吧,prolly 将你的命令更改为:

?party (link) (be cool)。保持简单:)

然后您可以通过命令的内容进行解析,并将链接与需求分开,如下所示:

text = message.content;

// check if the text is a command for your bot
if (text.startsWith("?party")) {
  // convert the contents of the command to an array, by splitting the text at spaces
  // Also get the rid of the command itself (?party)
  commandContents = text.split(" ").slice(1);
  link = commandContents[0];
  requirements = commandContents.slice(1).join(" "); // join the requirements back by spaces.

  if (!link) {
    message.channel.send(" no link");
  } else if (!requirements) {
    message.channel.send(" no requirements");
  } else {
    message.channel.send(
      `<@${message.author.id}> has pinged for party\n \n Link: ${link}]\n\nRequirements:${requirements}\n\n@everyone`
    );
  }
}

【讨论】:

  • 也是一堆垃圾,但希望对你有用:)
  • 做了一些调整,它运行顺利!只有一个问题,我怎样才能让它 ping 执行命令的人?
  • 您可以使用message.reply 代替message.channel.send,或者如果您希望有@mention,那么您可以通过message.author.id 获取用户的ID。更新了我的答案以提及作者:)
猜你喜欢
  • 1970-01-01
  • 2015-07-15
  • 2022-12-16
  • 1970-01-01
  • 1970-01-01
  • 2022-12-01
  • 2016-07-31
  • 2016-07-29
  • 2013-03-15
相关资源
最近更新 更多