Skip to content

Commit ad20bb4

Browse files
author
Brian Le
committed
Update documentation
1 parent 201b588 commit ad20bb4

File tree

1 file changed

+117
-20
lines changed

1 file changed

+117
-20
lines changed

Documentation.md

Lines changed: 117 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Introduction
44

5-
Ruby is a library for command resolving and parsing.
5+
Ruby is a library for command resolving and parsing. Designed to be platform-agnostic, it can be used for Slack and Twitch bots as well as Discord bots.
66

77
## Installation
88

@@ -14,41 +14,138 @@ npm install @botsocket/ruby
1414

1515
## Usage
1616

17+
Setting up Ruby is often a 3 step process.
18+
19+
First, create a new registry. Note that only one registry should be created for the whole application:
20+
21+
```js
22+
const Ruby = require('@botsocket/ruby');
23+
24+
const registry = Ruby.registry();
25+
```
26+
27+
Next, define your commands and pass definition-specific data (recommended to store command handlers):
28+
29+
```js
30+
31+
// !ban member reason
32+
33+
registry.add({
34+
name: 'ban',
35+
args: ['member', 'reason'],
36+
37+
data: {
38+
handler(args, flags) {
39+
40+
// Do stuff
41+
}
42+
}
43+
});
44+
```
45+
46+
Last, find the matching definitions by parsing the command:
47+
48+
```js
49+
const matches = registry.match('!ban member reason');
50+
for (const match of matches) {
51+
52+
// Invoke the handler
53+
54+
match.definition.data.handler(match.args, match.flags);
55+
}
56+
```
57+
1758
Usage with Discord.js:
1859

1960
```js
2061
const Ruby = require('@botsocket/ruby');
2162
const Discord = require('discord.js');
2263

23-
const registry = Ruby.registry({ prefix: '!', delimiter: /\s+/, });
64+
const registry = Ruby.registry();
65+
66+
// !repeat message
67+
68+
registry.add({
69+
name: 'repeat',
70+
args: ['message'],
2471

25-
registry.register({
26-
syntax: 'repeat {message}',
27-
alias: 'say',
28-
prefix: '?', // Overriding prefix
29-
delimiter: ',', // Overriding delimiter
72+
data: {
73+
handler(message, args, flags) {
74+
75+
if (!args.message) {
76+
message.channel.send('Message is required');
77+
return;
78+
}
79+
80+
if (args.message.length > 5) {
81+
message.channel.send('Message must have at least 5 characters');
82+
return;
83+
}
84+
85+
message.channel.send(args.message);
86+
}
87+
}
3088
});
3189

32-
const commands = {
33-
async repeat(message, args) {
90+
const client = new Discord.Client({
91+
// Stuff
92+
});
3493

35-
message.channel.send(args.message);
36-
},
37-
};
94+
client.connect();
3895

3996
client.on('message', (message) => {
4097

41-
const match = registry.match(message.content);
42-
43-
if (!match) {
44-
return;
98+
const matches = registry.match(message.content);
99+
for (const match of matches) {
100+
match.definition.data.handler(message, match.args, match.flags);
45101
}
102+
});
103+
```
104+
105+
Usage with Tmi.js:
106+
107+
```js
108+
const Ruby = require('@botsocket/ruby');
109+
const Tmi = require('tmi.js');
110+
111+
const registry = Ruby.registry();
46112

47-
const name = match.name;
48-
const args = match.args;
113+
// !repeat message
49114

50-
return commands[name](message, args);
115+
registry.add({
116+
name: 'repeat',
117+
args: ['message'],
118+
119+
data: {
120+
handler(client, channel, args, flags) {
121+
122+
if (!args.message) {
123+
client.say(channel, 'Message is required');
124+
return;
125+
}
126+
127+
if (args.message.length > 5) {
128+
client.say(channel, 'Message must have at least 5 characters');
129+
return;
130+
}
131+
132+
client.say(channel, args.message);
133+
}
134+
}
135+
});
136+
137+
const client = new Tmi.Client({
138+
// Stuff
51139
});
52140

53-
client.cconnect('your_token');
141+
client.connect();
142+
143+
client.on('message', (channel, tags, message, self) => {
144+
145+
const matches = registry.match(message);
146+
for (const match of matches) {
147+
match.definition.data.handler(client, channel, match.args, match.flags);
148+
}
149+
});
54150
```
151+

0 commit comments

Comments
 (0)