Giter VIP home page Giter VIP logo

discord-rose's Introduction

Discord-Rose

The simple Discord library for advanced users.

Installation

Run npm i --save discord-rose

Links

Wiki Docs

Support Server

NPM, GitHub

Simple bot

You can easily use the SingleWorker class for easy use of discord-rose, for scaled solution, look below

./index.js

const { SingleWorker } = require('discord-rose')

const worker = new SingleWorker({
  token: 'BOT TOKEN'
})

worker.commands
  .prefix('!')
  .add({
    command: 'hello',
    exec: (ctx) => {
      ctx.reply('World!')
    }
  })

Scaled Bot

You can instead use a Master & Worker solution, one master managing multiple workers/clusters, which hold x amount of shards, making it much more efficient.

./master.js

const { Master } = require('discord-rose')
const path = require('path')

const master = new Master(path.resolve(__dirname, './worker.js'), {
  token: 'BOT TOKEN'
})

master.start()

./worker.js

const { Worker } = require('discord-rose')

const worker = new Worker()

worker.commands
  .prefix('!')
  .add({
    command: 'hello',
    exec: (ctx) => {
      ctx.reply('World!')
    }
  })

Do node ./master.js and you're off to the races. Scaled automatically.

Do note if your bot only ever fits into 1 cluster (< 5000 servers by default), you should consider using SingleWorker since master & worker introduce more process overhead

You can even easily implement slash commands directly within message commands.

Ready to take it to the next level? Take a look out our Wiki

discord-rose's People

Contributors

alexanderpaolini avatar cmakh avatar jpbberry avatar lulalaby avatar matthew-st avatar murky-momo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

discord-rose's Issues

Idea: subcommands for normal message commands

The ability to add sub commands on a parent command like

{
command: "test",
subcommands: [{
command: "sub",
exec: async () => {
console.log("Sub command")
}
}],
exec: async () => {
console.log("Parent command")
}
}

and it would work like !test for the parent and !test sub for the sub command

feature request: Support for different message types

The MessagesResource.send function forces strings or embeds to be sent, when it wouldn't be an issue to send numbers.
The MessagesResource._formMessage takes a string and changes message to { content: message } as well as parsing embeds. It does not allow numbers, functions, bigints, or undefined to be sent.
A simple fix to this would be to add

if (['number', 'undefined', 'bigint', 'function'].includes(typeof message)) {
  message = { 
    content: inspect(message) 
  }
}

Additionally, the MessagesResource._formMessage function checks if the message is an instance of an Embed, then checks if it is a string. The second if could be changed to an else if as the second check is unnecessary.

possible memory leak

When a bot is started without any code on the worker, the logs are spammed with

Cluster 0 | Closed with code 0
Cluster 0 | Started

About a second in, (node:4007) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 KILL listeners added to [Cluster]. Use emitter.setMaxListeners() to increase limit (Use "node --trace-warnings ..." to show where the warning was created) is logged.

Reproducing the error:

master.js:

const { Master } = require('discord-rose')
const path = require('path')

const master = new Master(path.resolve(__dirname, 'worker.js'), {
  token: "BOT-TOKEN-HERE"
});

master.start();

worker.js:


This was found by starting a master before any code was written.

bug: `Worker.comms.broadcastEval()` crashing entire bot

The function Worker.comms.broadcastEval() is unable to handle an asynchronous function that resolves to undefined.

If a function resolves to undefined, the console will error

TypeError: Cannot read property 'error' of undefined
    at PATH/node_modules/discord-rose/dist/clustering/ThreadComms.js:72:25
    at Worker.<anonymous> (PATH/node_modules/discord-rose/dist/clustering/ThreadComms.js:48:21)
    at Worker.emit (events.js:315:20)
    at MessagePort.<anonymous> (internal/worker.js:207:53)
    at MessagePort.[nodejs.internal.kHybridDispatch] (internal/event_target.js:354:41)
    at MessagePort.exports.emitMessage (internal/per_context/messageport.js:18:26)
Emitted 'error' event on process instance at:
    at emitUnhandledRejectionOrErr (internal/event_target.js:543:11)
    at MessagePort.[nodejs.internal.kHybridDispatch] (internal/event_target.js:358:9)
    at MessagePort.exports.emitMessage (internal/per_context/messageport.js:18:26)

Example code:

// In a command exec function
const evaled: undefined[] = await worker.comms.broadcastEval("(async () => undefined)()")
return ctx.send('```' + evaled + '```')

Expected output: [ undefined ]
Actual output: N/A

@ mention commands erroring.

With the new @ mentions prefix, the bot throws the error: (node:8241) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'toLocaleLowerCase' of undefined.

Wiki for using commands instead of tell is wrong.

https://github.com/discord-rose/discord-rose/wiki/Using-Clusters#using-commands-instead-of-tell is wrong the code block indicates there only being 2 parameters passed in (data & respond). In reality there are 3 (cluster, data & respond). Fix it

const thing = 'abc'

master.handlers.on('GET_THING', (cluster, data, respond) => {
  if (data === true) return respond(thing) // responds to the event with this data
  respond({ error: 'Error message' }) // can also throw errors back on the worker
})

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.