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

safeness-prevnames

v1.5.4

Published

A lightweight Node.js client for interacting with an API or SQLite database to store and manage previous usernames of Discord users. Supports retrieval, saving, deletion, counting, and duplicate management of previous usernames.

Downloads

41

Readme

Safeness Prevnames Client

The safeness-prevnames module is a lightweight Node.js client designed to interact with an API that stores previous usernames of Discord users. This client allows you to retrieve, save, delete, count, and manage previous usernames both via an API and locally using SQLite. It supports both HTTP and HTTPS connections and can be configured with an optional access key for secure requests.

Important Links

Installation

You can install this module via npm:

npm install safeness-prevnames

Usage

Client Initialization

To get started, create an instance of the Client by specifying the base URL of your API. You can also provide an optional access key if your API requires authentication.

You can import the Client class directly or via the Safeness namespace:

// Import Client directly
const { Client } = require('safeness-prevnames');

// or Import via Safeness namespace
const Safeness = require('safeness-prevnames'); 

Initializing the Client

// Initialize the client without an access key
const client = new Client({ url: 'https://example.com' });
// or
const client = new Safeness.Client({ url: 'https://example.com' }); 

// Initialize the client with an access key
const clientWithKey = new Client({ url: 'https://example.com', key: 'your-access-key' });
// or
const clientWithKey = new Safeness.Client({ url: 'https://example.com', key: 'your-access-key' }); 

Offline Mode

The client can operate in offline mode using SQLite to store and manage previous usernames. To use this feature, simply initialize the client without specifying a URL or access key:

const client = new Safeness.Client({ api: false });

Available Methods

prevnames(userId)

Retrieves the previous usernames for a given Discord user ID.

async function getPrevnames() {
    try {
        const userId = '123456789'; // a Discord user ID
        const prevnames = await client.prevnames(userId);
        console.log(prevnames);
    } catch (error) {
        console.error('Error retrieving prevnames:', error);
    }
}

save(data)

Saves a new username for a given Discord user.

async function savePrevname() {
    try {
        const data = {
            user_id: '123456789',  // a Discord user ID
            username: 'newUsername',
            name: 'newName',
            changedAt: new Date().toISOString()  // Date in ISO format
        };

        await client.save(data);
        console.log('Name saved successfully');
    } catch (error) {
        console.error('Error saving name:', error);
    }
}

clear(userId)

Deletes all previous usernames for a given Discord user ID.

async function clearPrevnames() {
    try {
        const userId = '123456789';  // a Discord user ID
        await client.clear(userId);
        console.log('Prevnames deleted successfully');
    } catch (error) {
        console.error('Error deleting prevnames:', error);
    }
}

count()

Retrieves the total number of previous usernames stored.

async function getPrevnamesCount() {
    try {
        const count = await client.count();
        console.log(`Total number of prevnames: ${count}`);
    } catch (error) {
        console.error('Error retrieving prevnames count:', error);
    }
}

clearDouble(userId)

Clears duplicate previous usernames for a given Discord user ID. If no user ID is provided, it clears duplicates for all users. If there are no previous usernames to clear, it simply logs a message.

async function clearDuplicatePrevnames() {
    try {
        const userId = '123456789';  // Optional: a Discord user ID
        await client.clearDouble(userId);
        console.log('Duplicate prevnames cleared successfully');
    } catch (error) {
        console.error('Error clearing duplicate prevnames:', error);
    }
}

Automated Duplicate Clearing

The client can automatically clear duplicate previous usernames at startup and every 24 hours. This feature is available when the client is initialized without the API (api: false):

const client = new Safeness.Client({ api: false });

// The client will automatically clear duplicates at startup
// and every 24 hours.

Usage Examples

Here is a complete example demonstrating how to use the various methods of the prevnames client:

const { Client } = require('safeness-prevnames');
// or
const Safeness = require('safeness-prevnames'); 

(async () => {
    const client = new Client({ url: 'https://example.com', key: 'your-access-key' });

    try {
        const userId = '123456789';  // a Discord user ID
        
        // Retrieve previous usernames
        const prevNames = await client.prevnames(userId);
        console.log('Prevnames:', prevNames);
        
        // Save a new username
        await client.save({ user_id: userId, username: 'newUsername', name: 'newName', changedAt: new Date().toISOString() });
        console.log('Name saved successfully');
        
        // Delete previous usernames
        await client.clear(userId);
        console.log('Prevnames deleted successfully');
        
        // Count the total number of previous usernames
        const count = await client.count();
        console.log(`Total number of prevnames: ${count}`);

        // Clear duplicate prevnames
        await client.clearDouble(userId);
        console.log('Duplicate prevnames cleared successfully');
    } catch (error) {
        console.error('Error:', error);
    }
})();

Configuration

  • url: The base URL of the API to which the client should connect. This is a required parameter if api is set to true.
  • key: An optional access key to include in request headers for secured APIs.
  • api: Set to false to use the client in offline mode with SQLite.

Warnings

  • Ensure that your API is properly configured to accept requests from this client.
  • The access key should be kept secret and should not be exposed in insecure environments.
  • Ensure the URL does not end with a trailing slash (/). The client will automatically correct this, but it's best practice to provide the correct format.

License

This module is open source and distributed under the MIT license. You are free to modify it as needed, but please retain credit to the original authors.