Coder Social home page Coder Social logo

discore.js's Introduction

Based on discord.js

Examples

index.js structure:

const { Core } = require('discore.js');
new Core({
  // options
}).login('token');
  • Alternate login system
const { Core } = require('discore.js');
new Core({
  token: 'token',
});
  • All options (defaults)
const { Core } = require('discore.js');
new Core({
  eventsFolder: 'events',
  commandsFolder: 'commands',
  token: null,
  // To make multiple prefixes you can make an array
  // Example: ['!', '.']
  prefix: undefined,
  spaceAfterPrefix: false,
  splitArgs: ' ',
  ignoreCase: true,
  permLevels: new PermissionLevels(),
  ignoreSelf: true,
  ignoreBots: true,
  db: null,
});
  • Per-guild configuration.
this.client.config.guild.set('guild_id', {
  // Default settings:
  prefix: undefined,
  splitArgs: ' ',
  ignoreCase: true,
  permLevels: new PermissionLevels(),
  ignoreSelf: true,
  ignoreBots: true,
});

// If you want to leave current default prefixes
// and add new one then you can use add() method.
this.client.config.guild.add('guild_id', {
  prefix: '.', // Example.
});

Methods

  • uniqid.gen() // Generates unique identificator

Events

Events are placed in .\events\(eventsFolder option). For instance creating .\events\Main\ready.js will be an event ready in the Main category. Subcategories are also allowed and gonna be a second and more folder levels.

Their structure (options argument defined with default configuration):

const { Event } = require('discore.js');

module.exports = class extends Event {
  get options() {
    return {
      // Will run run() method if true, otherwise disabledRun() method.
      enabled: true,
      key: null, // Same as name but more important.
      name: null, // Key is going to be event name.
      once: false, // If true, event will emitted only once.
      id: undefined, // UniqID if not defined. Used to get the event.
    };
    // If key and name are null then they will be defined as file name.
    // For example, ready.js is gonna be 'ready'
  }

  get customOptions() {
    return {
      // You can put any options you want.
      // And use it via this.custom.
    };
  }

  get cOptions() {
    return {
      // Same as customOptions property.
      /*
        If you define both customOptions and cOptions
        then customOptions becomes more priority
      */
    };
  }

  run(...params) {
    // Event code.
    // Runs only if enabled.
  }

  disabledRun(...params) {
    // Same as run but runs only if disabled.
  }

  init() {
    // Optional method. Runs on 'ready'
    // event so you are able to use discord
    // data via this.client.
  }
};

Methods

  • toggle()
  • enable()
  • disable()
  • unload()
  • reload()
  • toString()

Properties

  • categories

Commands

Commands are placed in .\commands\ (commandsFolder option). For instance creating .\events\Main\Command.js will be a command Command in the Main category. Subcategories are also allowed and gonna be a second and more folder levels.

Their structure (options argument defined with default configuration):

const { Command } = require('discore.js');

module.exports = class extends Command {
  get options() {
    return {
      // Will run run() method if true, otherwise disabledRun() method.
      enabled: true,
      key: null, // Same as name but more important.
      name: null, // Key is going to be command name.
      id: undefined, // UniqID if not defined. Used to get the event.
      cooldown: 0, // In milliseconds
      aliases: [],
      permLevel: 0, // Runs noPermsRun() method if tests not passed.
      description: undefined,
      usage: undefined,
    };
    // If key and name are null then they will be defined as file name.
    // For example, test.js is gonna be 'test'
  }

  get customOptions() {
    return {
      // You can put any options you want.
      // And use it via this.custom.
    };
  }

  get cOptions() {
    return {
      // Same as customOptions property.
      /*
        If you define both customOptions and cOptions
        then customOptions becomes more priority.
      */
    };
  }

  run(message, args) {
    // Command code.
    // Runs only if enabled.
  }

  disabledRun(message, args) {
    // Same as run but runs only if disabled.
  }

  noPermsRun(message, args) {
    // Same as run
    // but runs only if Permission Level test is not passed.
  }

  init() {
    // Optional method. Runs on 'ready'
    // event so you are able to use discord
    // data via this.client.
  }
};

Methods

  • toggle()
  • enable()
  • disable()
  • unload()
  • reload()
  • toString()

Properties

  • categories
Method Examples
const command = this.client.commands.get('command');
command
  .toggle()
  .enable()
  .disable()
  .unload()
  .reload()
  .toString();

Store

Do you want to load event or command in live mode? You can use load() method!

.\ is gonna be your main file's root folder.

Methods

  • load()
  • get()
Method Examples
this.client.events.load('./events/event');
this.client.commands.load('./commands/command');

this.client.events.get('event_id');
this.client.events.get('event_name'); // Same as previus example

this.client.commands.get('command_id');
this.client.commands.get('command_name'); // Same as previus example
this.client.commands.get('command_alias'); // Same as previus example

Permission Levels

Their structure:

const { Core, PermissionLevels } = require('discore.js');
const config = require('./config');

const permLevels = new PermissionLevels();
permLevels
  .add(0, true, msg => msg.author.id === '1') // Throws error.
  // Permissions Level 1 is true only if message author id is '1'
  .add(1, false, msg => msg.author.id === '1')
  // Same as previus example
  .addLevel(2, false, (msg, client) => {
    return msg.author.id === client.user.id;
  });

// Test for a role.
permLevels.add(3, true, msg => msg.member.roles.has('roleid'));

// Testing. Returns boolean.
permLevels.test(3, msg);

// You can define client as third argument if needed.
permLevels.test(2, msg, this.client);

new Core(config);

Methods

  • addLevel()
  • add()
  • test()

Properties

  • length

Pages

Their structure:

const { Pages, Embed } = require('discore.js');

const pages = new Pages(this.client, {
  prevPage: '⏮', // Emoji which is used to switch to the previus page.
  nextPage: '⏭', // Emoji which is used to switch to the next page.
  filter: (reaction, user) => user.id === message.author.id, // Example.
});

const embed = new Embed()
  .setTitle('Embedded Page!')
  .setDescription('Yay! You can add embedded page!')
  .setFooter('Page: 2');

pages
  // AddPage method adds only one page.
  .addPage('Hey! You are on the first page!')
  // With add method you can add one page.
  .add(embed)
  // Or tons of pages!
  .add('`Third page.`', '`Fourth and the last page.`');

const msg = await pages.send(message.channel);

const timeout = 5000; // 5000 milliseconds = 5 seconds.
// To turn off pages just delete the message!
// Example:
msg.delete(timeout);
// or
setTimeout(() => msg.delete(), timeout);

Methods

  • addPage()
  • add()
  • send()

Properties

  • client
  • options
  • emojis
  • pages
  • filter

Embed ( RichEmbed )

Their structure:

const { Embed } = require('discore.js');

const embed = new Embed()
  .addBlankField()
  .addField('Title', 'Description')
  .attachFile(file)
  .attachFiles(files)
  .setAuthor('author', 'image url')
  .setColor(color)
  .setDescription('description')
  .setFooter('footer', 'image url')
  .setImage('image url')
  .setThumbnail('image url')
  .setTimestamp()
  .setTitle('title')
  .setURL('url');

Methods

  • addBlankField()
  • addField()
  • attachFile()
  • attachFiles()
  • setAuthor()
  • setColor()
  • setDescription()
  • setFooter()
  • setImage()
  • setThumbnail()
  • setTimestamp()
  • setTitle()
  • setURL()

Properties

  • author
  • color
  • description
  • fields
  • file
  • files
  • footer
  • image
  • length
  • thumbnail
  • timestamp
  • title
  • url

Databases

MongoDB

Structure:

const { Core, Mongo } = require('discore.js');

const db = new Mongo('url', {
  /* Options */
});

new Core({
  db,
});

Events

  • dbConnected
  • dbError
  • dbDisconnected

Methods

  • addModel()
  • open() ( Open connection )
  • close() ( Close connection )

Properties

  • collection

DB Models

Their structure:

// Must define all default properties.
// You can leave properties as undefined.
const data = {
  id: { type: Mongo.Types.String, default: undefined },
  messageCount: { type: Mongo.Types.Number, default: 0 },
};

db.addModel('modelName', data);

Types

  • Number
  • Double
  • String
  • Object
  • Array
  • ObjectId
  • Boolean
  • Date
  • RegExp

Methods

  • hasOne()
  • findOne()
  • insertOne()
  • deleteOne()
  • updateOne()
  • upsertOne()
hasOne()
// Working with model from previus example.
// You can use `db['modelName']`

// Searches for document with `id` of '123'.
let res1 = db.modelName.hasOne({ id: '123' });
let res2 = db.modelName.hasOne('id', '123'); // Same.
let res3 = db.modelName.hasOne(val => val.id === '123'); // Same.

console.log(typeof res); // Returns true or false (Boolean).
console.log(typeof res2); // Same.
console.log(typeof res3); // Same.
findOne()
// Working with model from previus example.
// You can use `db['modelName']` or `db.modelName`

// Searches for document with `id` of '123'.
let res1 = db.modelName.findOne({ id: '123' });
let res2 = db.modelName.findOne('id', '123'); // Same.
let res3 = db.modelName.findOne(val => val.id === '123'); // Same.

/*
  Returns document. If there is no document
  then you will getdefault settings which
  were defined by yourself.

  That means you can not get undefined or
  null.
*/
console.log(typeof res);
console.log(typeof res2); // Same.
console.log(typeof res3); // Same.
insertOne()
// **upsertOne() method is recommended to use!**
db.modelName.insertOne({
  id: '3213',
  messageCount: 1, // If not defined, going to be 0.
});
deleteOne()
// returns null or document.
db.modelName.deleteOne({ id: '3213' });

/*
  Does the same thing but returns null
  because document is already deleted.
*/
db.modelName.deleteOne('id', '3212');

// Same as previus example.
db.modelName.deleteOne(val => val.id === '3212');
updateOne()
// **upsertOne() method is recommended to use!**

/*
  All of these examples are going to search
  for `id` of '3213' and update 
*/
db.modelName.updateOne({ id: '3213' }, { messageCount: 2 });
db.modelName.updateOne('id', '3212', { messageCount: 2 });
db.modelName.updateOne(val => val.id === '3212', { messageCount: 2 });
upsertOne()
/*
  upsertOne() method is trying to update
  a document. If document is not exists then
  is going to insert it.
*/

// All of these examples are going to search
// for `id` of '3213' and update
// messageCount to 2.
db.modelName.upsertOne({ id: '3213' }, { messageCount: 2 });
db.modelName.upsertOne('id', '3212', { messageCount: 2 });
db.modelName.upsertOne(val => val.id === '3212', { messageCount: 2 });

MySQL

Structure:

const { Core, MySql } = require('discore.js');

const db = new MySql('url');

new Core({
  db,
});

Events

  • dbConnected
  • dbError
  • dbDisconnected

Methods

  • addModel()
  • open() ( Open connection )
  • close() ( Close connection )

Properties

  • collection

DB Models

Their structure:

// Must define all default properties.
// You can leave properties as undefined.
const data = {
  id: { type: MySql.Types.VarChar(18), default: undefined },
  messageCount: { type: MySql.Types.Int, default: 0 },
  rowId: {
    type: MySql.Types.Int(null, 'NOT NULL', 'AUTO_INCREMENT', 'PRIMARY'),
    default: 0,
  },
};

db.addModel('modelName', data);

Types

  • Double
  • Boolean
  • Date
  • VarChar
  • TinyText
  • Text
  • Blob
  • MediumText
  • LongText
  • LongBlob
  • TinyInt
  • SmallInt
  • MediumInt
  • Int
  • BigInt
  • Float
  • Decimal
  • DateTime
  • Timestamp
  • Time
  • Enum
  • Set

Methods

  • hasOne()
  • findOne()
  • insertOne()
  • deleteOne()
  • updateOne()
  • upsertOne()
hasOne()
// Working with model from previus example.
// You can use `db['modelName']`

// Searches for document with `id` of '123'.
let res1 = db.modelName.hasOne({ id: '123' });
let res2 = db.modelName.hasOne('id', '123'); // Same.
let res3 = db.modelName.hasOne(val => val.id === '123'); // Same.

console.log(typeof res); // Returns true or false (Boolean).
console.log(typeof res2); // Same.
console.log(typeof res3); // Same.
findOne()
// Working with model from previus example.
// You can use `db['modelName']` or `db.modelName`

// Searches for document with `id` of '123'.
let res1 = db.modelName.findOne({ id: '123' });
let res2 = db.modelName.findOne('id', '123'); // Same.
let res3 = db.modelName.findOne(val => val.id === '123'); // Same.

/*
  Returns document. If there is no document
  then you will getdefault settings which
  were defined by yourself.

  That means you can not get undefined or
  null.
*/
console.log(typeof res);
console.log(typeof res2); // Same.
console.log(typeof res3); // Same.
insertOne()
// **upsertOne() method is recommended to use!**
db.modelName.insertOne({
  id: '3213',
  messageCount: 1, // If not defined, going to be 0.
});
deleteOne()
// returns null or document.
db.modelName.deleteOne({ id: '3213' });

/*
  Does the same thing but returns null
  because document is already deleted.
*/
db.modelName.deleteOne('id', '3212');

// Same as previus example.
db.modelName.deleteOne(val => val.id === '3212');
updateOne()
// **upsertOne() method is recommended to use!**

/*
  All of these examples are going to search
  for `id` of '3213' and update 
*/
db.modelName.updateOne({ id: '3213' }, { messageCount: 2 });
db.modelName.updateOne('id', '3212', { messageCount: 2 });
db.modelName.updateOne(val => val.id === '3212', { messageCount: 2 });
upsertOne()
/*
  upsertOne() method is trying to update
  a document. If document is not exists then
  is going to insert it.
*/

// All of these examples are going to search
// for `id` of '3213' and update
// messageCount to 2.
db.modelName.upsertOne({ id: '3213' }, { messageCount: 2 });
db.modelName.upsertOne('id', '3212', { messageCount: 2 });
db.modelName.upsertOne(val => val.id === '3212', { messageCount: 2 });

License

FOSSA Status

discore.js's People

Contributors

zargovv avatar fossabot avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.