简体   繁体   中英

Using node-fetch to embed DnD spells from api

I'm wanting to be able to embed specific DnD spells taken from the 5e Api using a Discord bot. I can log all of the spells to the console using node-fetch but completely unsure of the next step in grabbing user input, mapping it to the correct spell and then embeding it.

What the console log looks like after running the command:

{
  count: 319,
  results: [
    {
      index: 'acid-arrow',
      name: 'Acid Arrow',
      url: '/api/spells/acid-arrow'
    },
(*Continuing on for all of the spells*)

I essentially would like the command to be:

!s acid arrow (for example)

Which then returns:

Discord embed image

Here is the code from the command I have so far:

const fetch = require('node-fetch');
const Discord = require('discord.js');

module.exports = {
    name: 'spells',
    aliases: ['s'],
    category: 'dnd',
    description: 'Returns spell info',
    usage: '!s <spell name>',
    run: async (client, message, args) => {

        fetch('https://www.dnd5eapi.co/api/spells/')
            .then(res => res.json())
            .then(json => console.log(json));

        **?????????????;**

        const embed = new Discord.MessageEmbed()
            .setTitle()
            .addField('Description:')
            .addField('At higher levels:')
            .addField('Range:')
            .addField('Components:')
            .addField('Materials needed:')
            .addField('Is it a ritual:')
            .addField('Needs concentration:')
            .addField('Level:')
            .addField('Casting time:');

        message.channel.send({ embed });
    },
};

To start off, you don't need to use .then() twice after fetching the spells .

According to what I can see when I visit https://www.dnd5eapi.co/api/spells/ every spell has an index which can be used to look up a spell and is usually the spell name join using a - so if you grab the user's input, let's say !s acid arrow you can do

fetch(`https://www.dnd5eapi.co/api/spells/${args.join('-')}`)

Which will fetch https://www.dnd5eapi.co/api/spells/acid-arrow . This will help avoid having you look for the spell .

Then you can use .then() to use the spell info and pass it to the embed .

So your solution would be:

fetch('https://www.dnd5eapi.co/api/spells/')
    .then(res => {
        const embed = new Discord.MessageEmbed()
            .setTitle(res.name)
            .addField('Description:', res.desc.join('\n'))
            .addField('At higher levels:', res.higher_level.join('\n'))
            .addField('Range:', res.range)
            .addField('Components:', res.components.join('\n'))
            .addField('Materials needed:', res.material)
            .addField('Is it a ritual:', res.ritual ? 'Yes' : 'No')
            .addField('Needs concentration:', res.concentration ? 'Yes' : 'No')
            .addField('Level:', res.level)
            .addField('Casting time:', res.casting_time);

        message.channel.send(embed);
    })
    .catch(err => message.reply('that spell does not exist!'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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