laifutil
TypeScript icon, indicating that this package has built-in type declarations

3.0.1 • Public • Published

laifutil

About

laifutil is a library to identify and parse Discord embeds sent from LaifuBot.

Installation

npm install laifutil

Example usage

We can make a simple bot to remind a user to drop after six minutes upon completing a drop code.

Using discord.js v13:

const { setTimeout } = require('node:timers/promises');
const { Client, Intents, Formatters } = require('discord.js');
const { isLaifuBot, isDropOpenedEmbed, DropOpenedEmbed } = require('laifutil');

const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });

// Six minutes in milliseconds
const dropInterval = 360_000;

client.once('ready', () => {
    console.log(`Logged in as ${client.user.tag}!`);
});

client.on('messageCreate', async message => {
    const srcEmbed = message.embeds[0];

    if (isLaifuBot(message.author.id) && srcEmbed) {
        const apiEmbed = srcEmbed.toJSON();

        if (isDropOpenedEmbed(apiEmbed)) {
            const embed = new DropOpenedEmbed(apiEmbed);

            if (embed.userId) {
                await setTimeout(dropInterval);
                const content = `Time to drop, ${Formatters.userMention(embed.userId)}!`;
                message.reply({ content });
            }
        }
    }
});

client.login('token');

Using discord.js v14:

const { setTimeout } = require('node:timers/promises');
const { Client, GatewayIntentBits, userMention } = require('discord.js');
const { isLaifuBot, isDropOpenedEmbed, DropOpenedEmbed } = require('laifutil');

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

// Six minutes in milliseconds
const dropInterval = 360_000;

client.once('ready', () => {
    console.log(`Logged in as ${client.user.tag}!`);
});

client.on('messageCreate', async message => {
    const srcEmbed = message.embeds[0];

    if (isLaifuBot(message.author.id) && srcEmbed) {
        const apiEmbed = srcEmbed.toJSON();

        if (isDropOpenedEmbed(apiEmbed)) {
            const embed = new DropOpenedEmbed(apiEmbed);

            if (embed.userId) {
                await setTimeout(dropInterval);
                const content = `Time to drop, ${userMention(embed.userId)}!`;
                message.reply({ content });
            }
        }
    }
});

client.login('token');

Links

Package Sidebar

Install

npm i laifutil

Weekly Downloads

0

Version

3.0.1

License

MIT

Unpacked Size

185 kB

Total Files

87

Last publish

Collaborators

  • minidomo