HistoryShow HN: Solaire - A lightweight framework for creating...

Show HN: Solaire – A lightweight framework for creating Discord bots in Node

-

- Advertisment -

npm version

A lightweight framework with a simple interface for creating Discord bots in Node

!ban @someUser being mean
“ban <...reason>“: {
execute({ args, message }) {
// args.user: Discord.js::GuildMember(someUser)
// args.offense: [“being”, “mean”]
message.channel.send(`Banning ${args.user.displayName} for ${args.reason.join(‘ ‘)}!`;
},
},
},
});

bot.start();
“>

import Discord from 'discord.js';
import { Solaire } from "solaire-discord";

const client = new Discord.Client({
  intents: [ Discord.Intents.FLAGS.GUILDS, Discord.Intents.FLAGS.GUILD_MESSAGES ]
});

const bot = Solaire.create({
  discordClient: client,
  token: process.env.TOKEN,
  commandPrelude: "!",
  commands: {
    // In a Discord channel...
    // > !ban @someUser being mean
    "ban  <...reason>": {
      execute({ args, message }) {
        // args.user: Discord.js::GuildMember(someUser)
        // args.offense: ["being", "mean"]
        message.channel.send(`Banning ${args.user.displayName} for ${args.reason.join(' ')}!`;
      },
    },
  },
});

bot.start();

Discord.js

Solaire interacts heavily with Discord.js, and many of the objects exposed from the Solaire API will be directly from Discord.js.

Solaire requires that you provide a Discord.js client version >=13.0.0

📣 Simplicity & Limitations 📣

Solaire is very much targetted at developers working on smaller or simpler Discord bots that don’t require some of the more advanced features of existing popular Discord bot frameworks, and just want something that will get their bot up and running quickly. More advanced features may be added in the future, but the guiding principle of the framework will always be simplicity in its API.

- Advertisement -

If you don’t find Solaire’s feature-set to be advanced enough for your use case, there are other great Discord/Node frameworks to take a look at

Slash Commands

Solaire does not utilize the new Discord slash commands feature, instead utilizing the old-fashioned method of listening to new message events.


Example Bot

Install ·
Example Config ·
Defining Commands ·
Command Configuration ·
Events


Install

npm install solaire-discord

Config

PropertyRequiredTypeDesc
discordClientYesDiscord.js::ClientA Discord.js Client object. This client must have the GUILD_MESSAGES intent enabled for Solaire to work properly.
tokenYesstringYour bot’s Discord token (see https://discord.com/developers/docs/intro)
commandPreludeNostringThe string that must precede a command’s name in a Discord message for the command to be invoked. Common values are !, ?, ;;, but any string would technically work.
commandCooldownNonumberThe amount of time in milliseconds that a command is un-invokable after being used. This cooldown is per-command.
commandsYesRecordSee Defining commands and Command configuration

Defining commands

In Solaire, bot commands are defined using a definition string that resembles how you would actually use the command in Discord. For example, a command that is used like !ban @someAnnoyingUser being mean, would be defined using the string ban [...reason].

This string, along with associated configuration for the command, is passed in via your Solaire config’s commands property.

Command Name & Aliases

A command’s name is defined as the first word in your command definition string

You can define aliases for a command by appending the command’s name with |, e.g.

Command Arguments

After your command’s name, you can define any number of arguments that can be passed into your command.

Required Arguments

Required arguments are denoted in the definition string by being wrapped in <>, e.g.

Optional Arguments

Optional arguments are denoted by being wrapped in [], e.g.

When an optional argument is defined, the remaining arguments in the command must also be optional.

Rest Arguments

A “rest” argument is an arg whose value is defined as all remaining words in a message. They are denoted by the arg’s name being preceded with .... e.g.

[...reason] > !ban @someAnnoyingUser being mean ^----- "reason" arg has value "being mean"

A rest argument must be the last argument of a command. When accessing the argument in your execute, guard, etc. functions, the value of the argument will be an array.

Argument Types

An argument’s value can be constrained by defining an explicit type for that argument, denoted in the command definition string by appending the argument’s name with :, e.g.

Defining an argument type has a few benefits

  • It validates that the passed in value is valid
  • It automatically parses the argument and fits it to its type, transforming the value to a more convenient data type for use when processing and executing the command
  • It provides documentation for how our command is supposed to be used

The available argument types are:

Argument TypeValidationResolved JS Type
IntValidates using parseIntNumber
FloatValidates using parseFloatNumber
GuildMemberValidates that ID passed in resolves to a member of the message’s serverDiscord.js::GuildMember
DateValidates using new Date()Date

Command Configuration

Command Execute Function

When your command is invoked, the command’s execute function gets called.

!ban @someAnnoyingUser mean
< Banning Some Annoying User for mean">

> !ban @someAnnoyingUser mean
< Banning Some Annoying User for mean

The payload that gets passed into the execute function contains the following properties

PropertyTypeDesc
argsRecordThe arguments passed into the command
messageDiscord.js::MessageThe message that

Join the pack! Join 8000+ others registered users, and get chat, make groups, post updates and make friends around the world!
www.knowasiak.com/register/
Read More

- Advertisement -
Charlie avatar
Charliehttps://plus.google.com/105215503769457384118
Fill your life with experiences so you always have a great story to tell

1 Comment

  1. I created this framework after finding the existing Node Discord bot frameworks to be too heavy for my use cases, and wanted a really simple and straightforward way to get a bot off of the ground. It's not nearly as feature-rich as some of the other frameworks out there, but I'm pretty happy with how effective it is at providing an expressive way to create bot commands.

You might also likeRELATED
Recommended to you

Doctor Web discovered vulnerabilities in children’s smart watches

November 30, 2021 Parents always strive to take care of their children. Technology innovations help them reach this goal, through various wearables like smartwatches and GPS trackers. More and more models of these devices are getting close to smartphones in functionality. For example, many of them can track the child’s location and travel route. These…

Guest post on 36000 websites using a simple search engine for a fair price

Publish articles in 21 languages Sign up for freeto check prices and websites Add or orderarticle content You can copy/paste your article or order a new one from our journalists and copywriters. Select a placement Using a simple search engine, find a publication and placement offer whose detailed description meets your needs. Place your order We accept…

Present HN: JWEB (a recent implementation of the CWEB Literate Programming intention)

JWEB is a modern, simplified implementation of the CWEB system of literate programming. Program texts are written in Markdown with fenced code blocks instead of TeX. ctangle has been re-implemented as jtangle which has been enhanced to support the Javascript and Typescript languages in addition to the original ctangle support for C and C++. jtangle…
- Advertisement -

Thich Nhat Hanh, Vietnamese Zen Master, Dies at 95

Thich Nhat Hanh at the Plum Village monastery in southern France | Courtesy Plum Village Community of Engaged Buddhism Vietnamese Zen Master Thich Nhat Hanh—a world-renowned spiritual leader, author, poet, and peace activist—died on January 22, 2022 at midnight (ICT) at his root temple, Tu Hien Temple, in Hue, Vietnam. He was 95. “Our beloved…

Before wave of train thefts, Union Pacific laid off some of its police force

News organizations both locally and nationally have been covering the rise of cargo theft in L.A.’s northeast train tracks in the past few days. Anchors on morning news have been quick to point out that there have been over 100 arrests, and even Forbes have been quick to point out the staggering $5 million worth…

Must read

One of the Most Famous Victorian Dishes Is a Hilarious Lie

“Good heavens! Issue umbrellas, the Scots are firing porridge!”...

Remote control, Even if it Can’t Boot to an Operating Syste m

 You've definitely used VPN or other remote access tools...
- Advertisement -