繁体   English   中英

我想让我的 discord 机器人能够使用包含输入用户名的链接的用户名回复命令

[英]I want to make my discord bot be able to reply to a command with a username with a link that contains the inputted username

我希望用户输入!fn maxtl ,机器人会通过发布此链接https://fortnitetracker.com/profile/all/maxtl/event来回复

这就是我认为它的样子。 有人可以向我提供有关如何改进此 Discord 机器人 API 使用的任何建议吗?

bot.on("message", (msg) => {
  if (msg.content === `!fn ${Username}`) {
    msg.reply(`https://fortnitetracker.com/profile/all/${Username}/events`);
  }
});

假设您没有对消息进行任何其他处理,这就是您可能需要做的事情。

bot.on("message", (msg) => {
  if (!msg.content.startsWith('!')) return;

  const arguments = msg.content.split(' ');
  const command = arguments.shift();

  if (command === '!fn') {
    msg.reply(`https://fortnitetracker.com/profile/all/${arguments[0]}/events`);
  }
});

这将

  1. 检查并查看消息是否以您的命令标识符开头(假设它是“!”)
  2. 将消息拆分为 arguments 数组;
  3. 删除命令并将其存储在命令变量中
  4. 检查命令是否等于“!fn”
  5. 回复包含参数 [0] 的 url,如果提供了参数,此时该参数将等于用户名。

我认为这段代码是您正在寻找的东西,我不是 100% 因为我不知道您的机器人的 rest 在做什么。

这段代码将

  1. 收听消息
  2. 如果消息不是以 !fn 开头的代码执行的救助
  3. 尝试使用提到的用户名查找消息(您的代码段显示此变量可用,因此我假设它是事先已知的,如果不是,我们也可以弄清楚)
  4. Bot 尝试发布消息
 bot.on('message', async (msg) => {
  if (!msg.content.startsWith('!fn')) return;

  const commandFound = msg.content.find(mentioned => mentioned === `!fn ${Username}`)
  const arguments = commandFound.split(' ');
  const userName = arguments.shift();

   if (userName) {
       try {
           await msg.reply(`https://fortnitetracker.com/profile/all/${userName}/events`);
       } catch (err) {
           console.warn('Failed to respond to mention.');
           console.warn(err);
       }
   }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM