如何解决我想让我的Discord机器人能够使用用户名包含包含输入的用户名的链接回复命令
我希望用户输入!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`);
}
});
这将
- 检查并查看消息是否以命令标识符开头(假设它是“!”)
- 将消息拆分为一个参数数组;
- 删除命令并将其存储在命令变量中
- 检查命令是否等于“!fn”
- 使用包含自变量[0]的URL进行答复,如果提供的话,此时它等于用户名。
我认为您需要的是这段代码,我不是100%,因为我不知道您的机器人其余部分在做什么。
此代码将
- 听一条消息
- 如果消息不是以!fn开头,则对代码执行进行救助
- 尝试查找带有提到的用户名的消息(您的代码段显示了此变量 可用,因此我认为它是事先已知的,如果没有,我们也可以弄清楚)
- 机器人试图发布消息
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);
}
}
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。