npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dofus-api-wrapper

v1.0.1

Published

A powerful Node.JS module that allows you to interact with dofus api.

Downloads

139

Readme

Dofus API Wrapper

A powerful Node.js module to interact with the Dofus API in TypeScript. It provides methods to fetch data like items, monsters, sets, and spells from the Dofus database, with robust error handling and logging.

Features

  • Fetch data about items, monsters, sets, and spells.
  • Support for multiple languages (e.g., English, French).
  • Robust error handling with detailed logging.
  • Extendable design for integration with other applications like Discord bots.
  • TypeScript support for better type safety and maintainability.

Installation

To install the module, use npm.

npm install dofus-api-wrapper

Configuration

Create a .env file to configure the base URL of the API.

API_BASE_URL=https://api.dofusdb.fr

Usage

Setup

To start using the wrapper, import the DofusService class and instantiate it:

import { DofusService } from 'dofus-api-wrapper';

const dofusService = new DofusService(); // Defaults to the API_BASE_URL from the .env file

Fetching items

const items = await dofusService.fetchItems('en'); // Fetches items in English
console.log(items.slice(0, 5)); // Prints the first 5 items

Fetching monsters

const monsters = await dofusService.fetchMonsters('fr'); // Fetches monsters in French
console.log(monsters.slice(0, 5)); // Prints the first 5 monsters

Fetching sets

const sets = await dofusService.fetchSets('en'); // Fetches sets in English
console.log(sets.slice(0, 5)); // Prints the first 5 sets

Fetching spells

const spells = await dofusService.fetchSpells('fr'); // Fetches spells in French
console.log(spells.slice(0, 5)); // Prints the first 5 spells

Javascript integration

const { DofusService } = require('dofus-api-wrapper');

const dofusService = new DofusService();

async function main() {
  try {
    console.log('Fetching items...');
    const items = await dofusService.fetchItems('fr');
    console.log('First 5 items:', items.slice(0, 5));

    console.log('\nFetching monsters...');
    const monsters = await dofusService.fetchMonsters('fr');
    console.log('First 5 monsters:', monsters.slice(0, 5));

    console.log('\nFetching sets...');
    const sets = await dofusService.fetchSets('fr');
    console.log('First 5 sets:', sets.slice(0, 5));

    console.log('\nFetching spells...');
    const spells = await dofusService.fetchSpells('fr');
    console.log('First 5 spells:', spells.slice(0, 5));
  } catch (error) {
    console.error('An error occurred:', error);
  }
}

main();

Error logging

In case of errors, detailed logs will be saved to a file named dofusdb.log in the logs directory. You can inspect this file for debugging purposes.


Discord Bot Integration

The wrapper can be easily integrated into a Discord bot. Below is a sample implementation using discord.js.

Bot Setup

Install the necessary packages:

npm install discord.js dotenv dofusdb-ts-wrapper

Create a .env file for your Discord bot token and the Dofus API base URL:

DISCORD_TOKEN=your-discord-bot-token
API_BASE_URL=https://api.dofusdb.fr

Implement the bot

import { Client, GatewayIntentBits } from 'discord.js';
import { DofusService } from 'dofus-api-wrapper';
import dotenv from 'dotenv';

dotenv.config();

const dofusService = new DofusService();
const client = new Client({
  intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent],
});

client.on('ready', () => {
  console.log(`Bot logged in as ${client.user?.tag}`);
});

client.on('messageCreate', async (message) => {
  if (message.author.bot) return;

  if (message.content.startsWith('!items')) {
    const items = await dofusService.fetchItems('en');
    if (items.length > 0) {
      const itemNames = items.slice(0, 5).map((item) => item.name?.en || 'Unknown').join('\n');
      message.channel.send(`Here are the first 5 items:\n${itemNames}`);
    } else {
      message.channel.send('Could not fetch items. Please try again later.');
    }
  }

  if (message.content.startsWith('!monsters')) {
    const monsters = await dofusService.fetchMonsters('en');
    if (monsters.length > 0) {
      const monsterNames = monsters.slice(0, 5).map((monster) => monster.name?.en || 'Unknown').join('\n');
      message.channel.send(`Here are the first 5 monsters:\n${monsterNames}`);
    } else {
      message.channel.send('Could not fetch monsters. Please try again later.');
    }
  }

  if (message.content.startsWith('!sets')) {
    const sets = await dofusService.fetchSets('en');
    if (sets.length > 0) {
      const setNames = sets.slice(0, 5).map((set) => set.name?.en || 'Unknown').join('\n');
      message.channel.send(`Here are the first 5 sets:\n${setNames}`);
    } else {
      message.channel.send('Could not fetch sets. Please try again later.');
    }
  }

  if (message.content.startsWith('!spells')) {
    const spells = await dofusService.fetchSpells('en');
    if (spells.length > 0) {
      const spellNames = spells.slice(0, 5).map((spell) => spell.name?.en || 'Unknown').join('\n');
      message.channel.send(`Here are the first 5 spells:\n${spellNames}`);
    } else {
      message.channel.send('Could not fetch spells. Please try again later.');
    }
  }
});

client.login(process.env.DISCORD_TOKEN);

Compile and run the bot

tsc
node dist/bot.js

Roadmap

  • Roadmap
  • Add search functionality for specific items, monsters, sets, or spells.
  • Improve logging with configurable log levels (INFO, WARN, ERROR).
  • Extend Discord bot commands to allow dynamic queries (e.g., !item ).
  • Add unit and integration tests for robustness.