0.0.61 • Published 8 months ago

shop-manager v0.0.61

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

Shop-manager

Shop manager to manage items in your project with MySQL

Installation

To install it, run npm install shop-manager

Documentation

ContentSection
Initiationstart
Propretiespropreties list
All methodsmethods list
Typestypes list

Initiation

Here is how to start the manager

const { ShopManager } = require('shop-manager');
const { createConnection } = require('mysql')

const database = createConnection({
    // Database credits
})

const manager = new ShopManager(database);

Propreties

Here are the propreties you can access

Items

You can get all the items cached

manager.items

Is is a Collection of items by their id

Inventories

You can get all the inventories

manager.inventories

It is a Collection of Collection of inventories by user id by guild id (the main collection is by guild id and the sub collections are users id)

Methods

guildItems

Get all the items of a guild

manager.guildItems(guild);
manager.guildItems(interaction);
manager.guildItems(member);
manager.guildItems('1234');

Parameter :

Returns a collection of items by the id of the guild, containing only guild's items

addItem

Add an item to the guild's items

manager.addItem({
    guild: '1234',
    type: 'string',
    name: "name of the item",
    content: "super string content to show",
    quantity: 0, // The item will be infinite
    price: 10000
});

manager.addItem({
    guild: message.guild,
    type: 'role',
    name: "Admin role",
    content: "141592653589713238",
    price: 10000,
    quantity: 2 // Only 2 will be available
});

Returns a promise, either string typed, with "no reply from database" as content, when the database didn't reply, and the item, when the request is done and the item is added to the list ( Promise<'no reply from database' | item> )

Parameter :

removeItem

Remove an item from the manager, using its ID

manager.removeItem(17); // Remove the item with the ID 17

Returns nothing ( void )

Parameters :

  • id : number, corresponding to an ID of the items

addInventory

Add one or more items to a user's inventory

const item = manager.items.first();

manager.addItem(message.guild, message.author, item, 3); // Add 3 items "item"
manager.addItem(message.guild, message.author, item); // Add 1 item "item"

Returns a true Promise ( Promise<true> )

Parameters :

removeInventory

Remove one or more items from a user's inventory

const item = manager.items.first();

manager.removeInventory(message.guild, message.author, item, 2); // Remove 2 items "item"
manager.removeInvetory(message.guild, message.author, item); // Remove 1 item "item"

Returns a true Promise ( Promise<true> )

Parameters :

getInventory

Get the inventory of an user in a guild

manager.getInventory(message.guild, message.author);

Return an inventory object

Parameters :

buyItem

Removes an item from the shop and add it in the inventory of the user

manager.buyItem(message.guild, message.author, 16); // Buy the item with the id 16
manager.buyItem(message.guild, message.author, 18, 6); // Buy 6 items with ID 18

Returns a promise, returning either 'no item' if no item is found in the cache. Returns nothing buyable if there is no item remaining ( remaining proprety of items equals 0). Returns true when everything is done

Parameters :

updateQuantity

Update the quantity of an item ( quantity proprety of items )

manager.updateQuantity(message.guild, 14, 8); // Set the quantity of item with 14 ID to 8

Returns a promise with no item, item is infinite or true

When no item is found with the ID, no item is the value of the promise

When the item to edit is infinite ( when its quantity is 0 ), the value is item is infinite

Returns true when everything is done

Promise<'no item' | 'item is infinite' | true>;

Parameters :

updateRemaining

Update the remaining quantity of an item ( remaining proprety of items )

manager.updateRemaining(message.guild, 9, 2); // Set the remaining quantity of item with 9 ID to 2

Returns a promise with no item, item is infinite or true

When no item is found with the ID, no item is the value of the promise

When the item to edit is infinite ( when its quantity is 0 ), the value is item is infinite

Returns true when everything is done

Promise<'no item' | 'item is infinite' | true>;

Parameters :

setInfinite

Make an item infinite or not

manager.setInfinite(message.guild, 7, true); // Make item with ID 7 infinite
manager.setInfinite(message.guild, 4, false, 10); // Make item with ID 4 not infinite, with quantity 10

When you set an item to not infinite, you have to specify value parameter

Returns a Promise with no item, set as not infinite, but no value or true

Returns Promise<'no item'> when no item is found with the id

Returns Promise<'set as not infinite'> when you set an item to not infinite, but you don't specify the value parameter

Returns Promise<true> when everything is done

Parameters :

  • guild : guild resolvable
  • itemId : number, corredponding to an item ID
  • infinite : boolean
  • value : number. This is optional, but must be specified when infinite is false

updatePrice

Update the price of an item

manager.updatePrice(message.guild, 5, 10000); // Set the price of the item with id 5 to 10000

Parameters :

  • guild : guild resolvable
  • itemId : number, corresponding to an item ID
  • price : number, this is the new price

Returns Promise<'no item' | true>.

The promise returns 'no item' when no item is found

The promise returns true when everything is done

updateName

Update the name of an item

manager.updateName(message.guild, 6, "Sword"); // Set the name of the item with id 6 to 'Sword'

Parameters :

  • guild : guild resolvable
  • itemId : number, corresponding to an item ID
  • name : string, this is the new name

Returns Promise<'no item' | true>.

The promise returns 'no item' when no item is found

The promise returns true when everything is done

updateContent

Update the content of an item

manager.updatePrice(message.guild, 3, 'The best item you can find on the marketplace.'); // Update the content of the item with id 3

Parameters :

  • guild : guild resolvable
  • itemId : number, corresponding to an item ID
  • content : string, this is the new content

Returns Promise<'no item' | true>.

The promise returns 'no item' when no item is found

The promise returns true when everything is done

Types

Types of the manager

Item

type item<Raw extends boolean = false> = {
    guild_id: string;
    name: string;
    content: string;
    type: itemType; // 'role' | 'string'
    id: number;
    price: If<Raw, string, number>;
    remaining: If<Raw, string, number>;
    quantity: If<Raw, string, number>; // 0 if infinite
}

ItemType

Type of an item

type itemType = 'string' | 'role'

Inventory

type inventory<Raw extends boolean = false> = {
    guild_id: string;
    user_id: string;
    items: If<Raw, string, inventoryItem[]>; // { name: string; content: string; price: number; id: number; type: itemType; }[]
}

Inventory item

type inventoryItem = {
    name: string;
    content: string;
    quantity: number;
    price: number;
    id: number;
    type: itemType;
}

Types :

guild resolvable

Anything that contains a guild

import { Guild, BaseInteraction, GuildMember, Message } from 'discord.js';

type guildResolvable = Guild | BaseInteraction | GuildMember | Message | string;

addOptions

Options to provide when adding a new item

export type addOptions = {
    guild: guildResolvable; // Anything that can give a guild
    name: string; // Name of the item
    content: string; // Value of the item : string or role id
    type: itemType; // Type of the item
    quantity?: number; // Optional quantity (default 0, infinite)
    price: number; // Price of the item
};

The content is either the ID of the role, in case of #addOptions.type = 'role', or the content of the string item, when #addOptions.type = 'string'

Types :

user resolvable

Any thing that contains a user

import { User, GuildMember, BaseInteraction, Message } from 'discord.js';

type userResolvable = User | GuildMember | BaseInteraction | Message | string;

DefaultQueryResult

Default result of a MySQL request

export type DefaultQueryResult = {
    fieldCount: number;
    affectedRows: number;
    insertId: number;
    serverStatus: number;
    warningCount: number;
    message: string;
    protocol41: boolean;
    changedRows: number;
};

QueryResult

Result of a MySQL query

export type QueryResult<T> = T extends DefaultQueryResult ? DefaultQueryResult : T[];

Support

Get support on the support server

0.0.61

8 months ago

0.0.6

8 months ago

0.0.5

8 months ago

0.0.4

8 months ago

0.0.3

8 months ago

0.0.2

8 months ago

0.0.1

9 months ago