Discord.js connecting api to discord bot in an embed link(Discord.js 在嵌入链接中将 api 连接到不和谐机器人)
问题描述
我一直在尝试编写一个机器人来发布和嵌入链接,以根据用户输入的数字显示漫画的标题、标签等.我正在查看的 api 是
I've been trying to code a bot that will post and embed link to show the title of manga, tags, etc. based on what digits user input. The api that I'm looking at is This and I want to know what's the best way of connecting it to my discord bot?
client.on("message", (message) => {
if (!message.content.startsWith(prefix)) return;
if (message.content.startsWith(prefix)) {
let chosenNum = message.content.match(/d+/)?.[0].slice(0);
if (chosenNum.length > 6) {
message.channel.send("**1-6 digits only**")
} else{
message.channel.send(`${chosenNum}`)
}
}
});
The package you chose has a really simple API. All you need is the nhentai.exists()
and nhentai.getDoujin()
methods. With the first one you can check if the user submitted ID exists, and with the second one you can fetch a JSON object that you can use to populate your embed.
Check out the example below:
const { Client, MessageEmbed } = require('discord.js');
const nhentai = require('nhentai-js');
const client = new Client();
const prefix = '!';
client.on('message', async (message) => {
if (!message.content.startsWith(prefix)) return;
// create an args variable that slices off the prefix and splits it into an array
const args = message.content.slice(prefix.length).split(/ +/);
// create a command variable by taking the first element in the array
// and removing it from args
const command = args.shift().toLowerCase();
if (command === 'hen') {
const chosenNum = args[0]?.match(/d+/)?.[0];
if (!chosenNum)
return message.channel.send('You need to provide a number');
if (chosenNum.length > 6)
return message.channel.send('**1-6 digits only**');
try {
if (!(await nhentai.exists(chosenNum)))
return message.channel.send(`No results found for "${chosenNum}".`);
// res is your JSON object, feel free to console.log its content
const res = await nhentai.getDoujin(chosenNum);
const embed = new MessageEmbed()
.setColor('#ed2553')
.setTitle(res.title)
.setImage(res.pages[0])
.setURL(res.link)
.addField('Pages', res.details.pages[0], true)
.addField('Uploaded', res.details.uploaded[0], true);
if (res.details.languages)
embed.addField('Languages', res.details.languages.join(', '), true);
if (res.details.characters)
embed.addField('Top characters', res.details.characters.slice(0, 3).join(', '), true);
if (res.details.tags)
embed.addField('Top tags', res.details.tags.slice(0, 3).join(', '), true);
return message.channel.send(embed);
} catch (err) {
console.log(err);
return message.channel.send('Oops, there was an error. Maybe try again?!');
}
}
});
这篇关于Discord.js 在嵌入链接中将 api 连接到不和谐机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Discord.js 在嵌入链接中将 api 连接到不和谐机器人


- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- 400或500级别的HTTP响应 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07