Skip to content

Add Command guide #214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions _guides/command-creation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
layout: documentation
title: Create a custom command
description: Customize The Lounge using built-in custom command support
---

This guide assumes you have setup your own plugin already. If you don't know how to do that, check out [this guide](/docs/plugin-creation)

The Lounge exposes an easy way to register your own commands, all you need to do is call the api in your `onServerStart` method like this:

```js
module.exports = {
onServerStart: thelounge => {
thelounge.Commands.add("testcommand", testCommand);
},
};
```

where `testCommand` is an object that defines an input field with the command function:
```js
const testCommand = {
input: function (client, target, command, args) {
// command logic
}
};
```
- `client` is the public client api of The Lounge
- `target` is an object that wraps the channel and the network the command was enteres in, access them via `target.chan` or `target.network`
- `command` is the command string that was entered
- and `args` is an array of strings that followed the command string

Optionally, you can add `allowDisconnected: true` to your command to be able to run the command while not being connected to an irc network:
```js
const testCommand = {
input: function (client, target, command, args) {
// command logic
},
allowDisconnected: true
};
```

You can make the client run commands or send messages via the `runAsUser` method like this:
```js
client.runAsUser("/whois MiniDigger", target.chan.id);
client.runAsUser("Hello world!", target.chan.id);
```
100 changes: 100 additions & 0 deletions _guides/plugin-creation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
---
layout: documentation
title: Create a custom plugin
description: Customize The Lounge using built-in plugin support, and share plugins with others
---

Plugins for The Lounge are npm packages hosted on [the npm registry](https://www.npmjs.com).
This works similarly to themes. A theme is a plugin but not every plugin is a theme.

In a directory named after your new plugin (for example, `thelounge-plugin-foo`), start by creating a new package with the following command:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instead of this info, it might be better if we create a blank template for packages as a repository in the org? How does that sound to you? Then we can have a bit more info there around exactly what all this stuff is doing, but instead of all this explanation in this guide for setting up a dir, using yarn, etc, we can just say "clone this repo" and go from there?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmh, yeah, that would work too. I was just adapting the themes guide. Do we want to for the same for that too?


```
yarn init -y
```

This will create a `package.json` file that you must edit as such:

- Make sure `"name"` is the name of your package, `"thelounge-plugin-foo"`
- Change the value of `"main"` to be `"package.json"`
- Add a `"keywords"` to make sure your plugin is discoverable:
```json
"keywords": [
"thelounge",
"thelounge-plugin"
]
```
- Add a `"thelounge"` section with required metadata. `"name"` is the display name that will appear in the client settings:
```json
"thelounge": {
"name": "Plugin Name",
"type": "plugin"
},
```
- Reference your main javascript file:
```json
"main": "index.js",
```
- Add a dependency on `"thelounge"`
```json
"dependencies": {
"thelounge": "3.0.1"
}
```
Although it is not required, we strongly recommend you also fill in the `"homepage"`, `"repository"`, and `"bugs"` sections.

You can then place all your plugin code in your `index.js`.

The Lounge defines an entry point for plugins, you can use it like this:
```js
module.exports = {
onServerStart: api => {
// do stuff with api
},
};
```
Refer to [the API reference](/docs/api) to learn what you can do or start implementing your own custom command with
[the custom command guide](/guides/command-creation).

## Helpful things for plugin developers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking these can be sorted by making some helper functions in the api, and then just having them in the API docs. We won't want to explain everything here, you know?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A helper for sending a message would be cool, I require could be helpful for other stuff tho I guess? For ppl who no experience with node like me, that might help.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, we don't want to make this seem "official" because then we have to support it, you know? The helper functions are about being able to abstract out the internal code, and if people decide to use internal stuff, and we change it, we don't have to worry about that because it's not supported. Putting it in a guide on our official docs makes it look official.


Here follows a helpful list of things that might be of use to plugin developers:

* You can import and use methods from The Lounge via node's `require` like this `const Msg = require("thelounge/src/models/msg");`
* You can send a message to a channel like this:
```js
chan.pushMessage(client.client, new Msg({
type: Msg.Type.ERROR,
text: "Hello World",
}));
```
where `chan` is the channel (`target.chan` if you are in a command) and `client` the public client.

## Installing a plugin locally

The Lounge currently doesn't allow you to install a plugin from source, thats why we have to do it manually.

For that we have to add our plugin as a new package in the `THELOUNGE_HOME/packages` dir.
For that you need to have a package.json in that packages dir that looks kinda like this:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
For that you need to have a package.json in that packages dir that looks kinda like this:
For that you need to edit the package.json in that dir. If it doesn't exist, you can create it by installing 1 plugin/theme as per the docs It will look something like this:

Also, maybe link docs to the docs for installing plugins.

```json
{
"private": true,
"description": "Packages for The Lounge. All packages in node_modules directory will be automatically loaded.",
"dependencies": {
"thelounge-plugin-foo": "1.0.1"
}
}
```
the important thing is the name here.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
the important thing is the name here.
the version number doesn't matter at this point in time, just ensure that the name is unique. This should be the name of your package.


You then need to create a folder with that name in the `node_modules` sub dir.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth just suggesting they copy the whole folder that we know they have already created above, if they have been following this guide?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmhm, maybe. Ideally this would all be replace by a proper way to install themes locally anyways. If they copy the whole dir, it might become confusing on what to edit, that's why I only symlinked the two files that actually matter

We then need to place our index.js and package.json in that dir.
You can do that manually by just copy pasting it, but that would involve copy pasting it for every change.
I would recommend symlinking the files from the project into the packages folder, kinda like this:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move away from I, and do something like "It is recommended" or something. We don't want these to be first person guides. Also, probably best to remove all of the bit above this with the "you could do X, but instead do Y" and just go straight to giving the command to symlink the whole directory that they have created.

So something like "in the directory that you have created your plugin, do ln ... etc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I just copy pasted that out of my readme, I'll rewrite it tomorrow.

```
ln package.json thelounge-home/packages/node_modules/thelounge-plugin-foo/package.json
ln index.js thelounge-home/packages/node_modules/thelounge-plugin-foo/index.js
```
You can then just edit and commit the files in the project dir and restart The Lounge
on every change you do and the changes will be picked up.