-
-
Notifications
You must be signed in to change notification settings - Fork 71
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
base: master
Are you sure you want to change the base?
Add Command guide #214
Changes from all commits
797544b
1448f02
de14ee9
86984ee
d30b50f
d1048a3
1cd8cdd
9bcfc33
c9de53c
1bbbebd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
``` |
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: | ||||||
|
||||||
``` | ||||||
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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Also, maybe link |
||||||
```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. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
You then need to create a folder with that name in the `node_modules` sub dir. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's move away from So something like "in the directory that you have created your plugin, do There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||||||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?