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

pioucord

v1.2.9

Published

A little help to use the discord api. Handle connection to the gateway and provide a fast way to make requests to discord. This package is not finished, it is still in development.

Downloads

110

Readme

Pioucord

Don't except too much from this package for now, if you want something easier to use , then use discord.js.

pioucord is an ES package that allows you to create a discord bot with ease. It helps in different point, such as:

  • Connection to the gateway with a WebSocket (including shards)
  • Requests to the API
  • Rate limits

Installation

npm install pioucord

A simple bot

To know more about intents, check here.

To know more about presence, check here.

import {Client, ActivityType, Routes} from 'pioucord';

const client = new Client({
    intents: ['Guilds', 'GuildMessages', 'MessageContent'],
    presence: {
        status: 'dnd',
        activities: [
            {
                name: '!ping',
                type: ActivityType.Playing
            }
        ]
    }
});

client.ws.on('READY', (data) => {
    console.log(`Logged in as ${client.user.username}#${client.user.discriminator} (${client.user.id})`);
});

client.ws.on('MESSAGE_CREATE', message => {
    if (message.content == "!ping") {
        client.rest.post(Routes.channelMessages(message.channel_id), {
            content: 'Pong!'
        });
    }
});

client.login('Some secret token goes here');

Here, when the bot will see a message with !ping as content, it will send Pong! in the same channel.

Since there's no classes for now, to reply to a message, you need to add the field message_reference in the message payload, check here for more infos.

It will look like:

client.rest.post(Routes.channelMessages(message.channel_id), {
    content: 'Pong!',
    message_reference: {
        message_id: message.id
    }
});

You may have noticed two points:

  • We should use client.ws.on instead of client.on to listen to events. (client.on is not implemented yet, and will be used for constructed objects)
  • We can use client.user as soon as the READY event is thrown.
  • Events are in screaming case, and the provided parameters are raw data from the gateway, check event list here.

Sharding

To know more about shards, check here.

You can use specific shards to start your bot with:

import {Client} from 'pioucord';

const client = new Client({
    intents: 'some intents, check above',
    shards: [0, 2],
    shardsCount: 3
});

If you don't put any, it will identify to the gateway without providing shards.

Uploading files

To know how files upload works, check here.

To upload files, you just need to use the files field in the payload, and put an array of files.

// ...
client.ws.on('MESSAGE_CREATE', message => {
    if (message.content == "!image") {
        client.rest.post(Routes.channelMessages(message.channel_id), {
            content: 'Beautiful image!',
            message_reference: {
                message_id: message.id
            },
            files: [{
              name: 'image.png',
              description: 'Image',
              file: "A buffer goes here"
            }]
        });
    }
});
// ...

Handler

It isn't supported by the package itself, it's much better to let the user create it himself.

A little handler example

If you want, you can create a commands' handler, which will make your bot easier to manage.

You can create an events one if you want, but I will not show it here.

import {readdir} from 'node:fs/promises'; // Used to read dirs, need an absolute path
import {Client} from 'pioucord';

// Simple client creation
const client = new Client({
    intents: ['GuildMessages', 'MessageContent']
});

// Reading the commands folder
client.commands = new Map();
const path = 'absolute path goes here';
for (const file of await readdir(path)) {
    const command = (await import('file://' + path + file)).default;
    client.commands.set(command.name, command);
}
;

// Listening messages
const prefix = '!';
client.ws.on('MESSAGE_CREATE', message => {
    if (message.author.bot || !message.content.startsWith(prefix)) return;

    const args = message.content.split(/ +/);
    const command = client.commands.get(args.shift().slice(prefix.length));

    if (!command) return;

    command.execute(message, args);
});

client.login('token goes here as always');

And then, put some files in the commands folder which looks like:

import {Routes} from 'pioucord';

export default {
    name: 'ping',
    execute: (message, args) => {
        // The client is in each events objects
        message.client.rest.post(Routes.channelMessages(message.channel_id), {
            content: 'Pong!',
            message_reference: {
                message_id: message.id
            }
        });
    }
};

You can access the client from every event.

Call it as you want, it won't change anything, but try to make something understandable.

Upgrade the handler

You may be tired because of all these client.rest, because the package still don't have classes (it should be out for the V2).

If you don't want to wait (and you are right), you are free to add some functions to your handler, here is a little example:

client.on('MESSAGE_CREATE', message => {
    if (message.author.bot || !message.content.startsWith(prefix)) return;

    const args = message.content.split(/ +/);
    const command = client.commands.get(args.shift().slice(prefix.length));

    if (!command) return;

    // Here, modifications
    const functions = {
        channelSend: (data) => {
            return client.rest.post(Routes.channelMessages(message.channel_id), data);
        },
        messageReply: (data) => {
            return client.rest.post(Routes.channelMessages(message.channel_id), Object.assign(data,
                {
                    message_reference: {
                        message_id: message.id
                    }
                }
            ));
        }
    };

    // Don't forget to send them
    command.execute(message, args, functions);
});

And then, the ping command file:

export default {
    name: 'ping',
    execute: (message, args, functions) => {
        functions.messageReply({content: 'Pong!'});
    }
};

See? Much clearer!

Debug

That's not something very important, but you can use the debug event on the ws, like:

client.ws.on('debug', (shardId, text) => {
    console.log(`Shard ${shardId ?? 'MAIN'} | ${text}`);
});