Skip to content

Commit 4e9d35d

Browse files
committed
feat: add help text for most ctrls (#17)
1 parent b73d42a commit 4e9d35d

19 files changed

+297
-68
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ help: ## print this help
6363
| awk 'BEGIN {FS = ":[^:]*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
6464

6565
git-push: ## push to both gitlab and github (this assumes you have both remotes set up)
66-
git push gitlab ${GIT_BRANCH}
6766
git push github ${GIT_BRANCH}
67+
git push gitlab ${GIT_BRANCH}
6868

6969
# from https://gist.github.com/amitchhajer/4461043#gistcomment-2349917
7070
git-stats: ## print git contributor line counts (approx, for fun)

src/Bot.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,6 @@ export class Bot extends BaseService<BotData> implements Service {
375375

376376
/**
377377
* Log an otherwise-unhandled but non-fatal error (typically leaked from one of the observables).
378-
*
379-
* Note: this method is already bound, so it can be passed with `this.looseError`. Using that requires
380-
* `tslint:disable:no-unbound-method` as well.
381378
*/
382379
protected looseError(err: Error) {
383380
this.logger.error(err, 'bot stream did not handle error');

src/controller/AccountController.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ export class AccountController extends BaseController<AccountControllerData> imp
8080
return this.reply(ctx, results);
8181
}
8282

83+
@Handler(NOUN_GRANT, CommandVerb.Help)
84+
public async getGrantHelp(cmd: Command, ctx: Context): Promise<void> {
85+
return this.reply(ctx, this.defaultHelp(cmd));
86+
}
87+
8388
@Handler(NOUN_ACCOUNT, CommandVerb.Create)
8489
public async createAccount(cmd: Command, ctx: Context): Promise<void> {
8590
if (!this.data.join.allow && !this.checkGrants(ctx, 'account:create')) {
@@ -89,7 +94,7 @@ export class AccountController extends BaseController<AccountControllerData> imp
8994
const name = cmd.getHeadOrDefault('name', ctx.name);
9095
const existing = await this.userRepository.count({ name });
9196
if (existing > 0) {
92-
return this.reply(ctx, this.locale.translate('service.controller.account.account.create.exists', {
97+
return this.reply(ctx, this.translate('account-create.exists', {
9398
name,
9499
}));
95100
}
@@ -106,7 +111,7 @@ export class AccountController extends BaseController<AccountControllerData> imp
106111
}));
107112

108113
const jwt = await this.createToken(user);
109-
return this.reply(ctx, this.locale.translate('service.controller.account.account.create.success', {
114+
return this.reply(ctx, this.translate('account-create.success', {
110115
jwt,
111116
name,
112117
}));
@@ -119,7 +124,7 @@ export class AccountController extends BaseController<AccountControllerData> imp
119124
const name = user.name;
120125

121126
if (cmd.getHeadOrDefault('confirm', 'no') !== 'yes') {
122-
const completion = createCompletion(cmd, 'confirm', this.locale.translate('service.controller.account.account.delete.confirm', {
127+
const completion = createCompletion(cmd, 'confirm', this.translate('account-delete.confirm', {
123128
name,
124129
}));
125130
await this.bot.executeCommand(completion);
@@ -131,12 +136,17 @@ export class AccountController extends BaseController<AccountControllerData> imp
131136
});
132137

133138
const jwt = await this.createToken(user);
134-
return this.reply(ctx, this.locale.translate('service.controller.account.account.delete.success', {
139+
return this.reply(ctx, this.translate('account-delete.success', {
135140
jwt,
136141
name,
137142
}));
138143
}
139144

145+
@Handler(NOUN_ACCOUNT, CommandVerb.Help)
146+
public async getAccountHelp(cmd: Command, ctx: Context): Promise<void> {
147+
return this.reply(ctx, this.defaultHelp(cmd));
148+
}
149+
140150
@Handler(NOUN_SESSION, CommandVerb.Create)
141151
public async createSession(cmd: Command, ctx: Context): Promise<void> {
142152
const jwt = cmd.getHead('token');
@@ -155,7 +165,7 @@ export class AccountController extends BaseController<AccountControllerData> imp
155165
const source = this.getSourceOrFail(ctx);
156166
const session = await source.createSession(ctx.uid, user);
157167
this.logger.debug({ session, user }, 'created session');
158-
return this.reply(ctx, this.locale.translate('service.controller.account.session.create.success'));
168+
return this.reply(ctx, this.translate('session-create.success'));
159169
}
160170

161171
@Handler(NOUN_SESSION, CommandVerb.Get)
@@ -171,6 +181,11 @@ export class AccountController extends BaseController<AccountControllerData> imp
171181
return this.reply(ctx, session.toString());
172182
}
173183

184+
@Handler(NOUN_SESSION, CommandVerb.Help)
185+
public async getSessionHelp(cmd: Command, ctx: Context): Promise<void> {
186+
return this.reply(ctx, this.defaultHelp(cmd));
187+
}
188+
174189
protected async createToken(user: User): Promise<string> {
175190
const issued = this.clock.getSeconds();
176191
const expires = issued + this.data.token.duration;

src/controller/BaseController.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import { BotService, INJECT_LOCALE } from 'src/BotService';
66
import { getHandlerOptions, HandlerOptions } from 'src/controller';
77
import { Controller, ControllerData, ControllerOptions } from 'src/controller/Controller';
88
import { User } from 'src/entity/auth/User';
9-
import { Command } from 'src/entity/Command';
9+
import { Command, CommandVerb } from 'src/entity/Command';
1010
import { Context } from 'src/entity/Context';
1111
import { Message } from 'src/entity/Message';
1212
import { Listener } from 'src/listener/Listener';
13-
import { Locale } from 'src/locale';
13+
import { Locale, TranslateOptions } from 'src/locale';
1414
import { ServiceModule } from 'src/module/ServiceModule';
1515
import { ServiceDefinition } from 'src/Service';
1616
import { applyTransforms } from 'src/transform/helpers';
@@ -181,4 +181,23 @@ export abstract class BaseController<TData extends ControllerData> extends BotSe
181181
}
182182
return user;
183183
}
184+
185+
protected translate(key: string, options: TranslateOptions = {}): string {
186+
return this.locale.translate(`service.${this.kind}.${key}`, options);
187+
}
188+
189+
protected defaultHelp(cmd: Command): string {
190+
const data = {
191+
data: this.data,
192+
};
193+
const desc = this.translate('help.desc', data);
194+
195+
if (cmd.has('topic')) {
196+
const topic = cmd.getHead('topic');
197+
const topicDesc = this.translate(`help.${topic}`, data);
198+
return `${desc}\n${topicDesc}`;
199+
}
200+
201+
return desc;
202+
}
184203
}

src/controller/CompletionController.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export class CompletionController extends BaseController<CompletionControllerDat
6262
}));
6363

6464
this.logger.debug({ context, fragment }, 'creating fragment for later completion');
65-
return this.reply(context, this.locale.translate('service.controller.completion.fragment.prompt', {
65+
return this.reply(context, this.translate('create.prompt', {
6666
id: fragment.id,
6767
key,
6868
msg,
@@ -77,7 +77,7 @@ export class CompletionController extends BaseController<CompletionControllerDat
7777

7878
const fragment = await this.getFragment(ctx, id);
7979
if (isNil(fragment)) {
80-
return this.reply(ctx, this.locale.translate('service.controller.completion.fragment.missing'));
80+
return this.reply(ctx, this.translate('update.missing'));
8181
}
8282

8383
this.logger.debug({ fragment, parserId: fragment.parserId }, 'attempting to complete fragment');
@@ -93,6 +93,11 @@ export class CompletionController extends BaseController<CompletionControllerDat
9393
await this.bot.executeCommand(...commands);
9494
}
9595

96+
@Handler(NOUN_FRAGMENT, CommandVerb.Help)
97+
public async getHelp(cmd: Command, ctx: Context): Promise<void> {
98+
return this.reply(ctx, this.defaultHelp(cmd));
99+
}
100+
96101
protected async createContext(maybeCtx?: Context) {
97102
const ctx = mustExist(maybeCtx);
98103
if (isNil(ctx.target)) {

src/controller/CountController.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ export class CountController extends BaseController<CountControllerData> impleme
6969
await this.counterRepository.save(counter);
7070
}
7171

72+
@Handler(NOUN_COUNTER, CommandVerb.Help)
73+
public async getHelp(cmd: Command, ctx: Context): Promise<void> {
74+
return this.reply(ctx, this.defaultHelp(cmd));
75+
}
76+
7277
public async findOrCreateCounter(name: string, roomId: string): Promise<Counter> {
7378
const counter = await this.counterRepository.findOne({
7479
where: {

src/controller/DiceController.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { CheckRBAC, Handler } from 'src/controller';
66
import { BaseController } from 'src/controller/BaseController';
77
import { Controller, ControllerData, ControllerOptions } from 'src/controller/Controller';
88
import { Command, CommandVerb } from 'src/entity/Command';
9+
import { Context } from 'src/entity/Context';
910
import { mustExist } from 'src/utils';
1011

1112
const DICE_MINIMUM = 1;
@@ -41,9 +42,14 @@ export class DiceController extends BaseController<DiceControllerData> implement
4142
this.logger.debug({ count, sides }, 'handling dice results');
4243
const sum = results.reduce((a, b) => a + b, 0);
4344

44-
return this.reply(mustExist(cmd.context), this.locale.translate('service.controller.dice.roll.create', {
45+
return this.reply(mustExist(cmd.context), this.translate('create.success', {
4546
results,
4647
sum,
4748
}));
4849
}
50+
51+
@Handler(NOUN_ROLL, CommandVerb.Help)
52+
public async getHelp(cmd: Command, ctx: Context): Promise<void> {
53+
return this.reply(ctx, this.defaultHelp(cmd));
54+
}
4955
}

src/controller/EchoController.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { CheckRBAC, Handler } from 'src/controller';
44
import { BaseController } from 'src/controller/BaseController';
55
import { Controller, ControllerData, ControllerOptions } from 'src/controller/Controller';
66
import { Command, CommandVerb } from 'src/entity/Command';
7+
import { Context } from 'src/entity/Context';
78

89
export const NOUN_ECHO = 'echo';
910

@@ -18,8 +19,13 @@ export class EchoController extends BaseController<EchoControllerData> implement
1819

1920
@Handler(NOUN_ECHO, CommandVerb.Create)
2021
@CheckRBAC()
21-
public async createEcho(cmd: Command): Promise<void> {
22+
public async createEcho(cmd: Command, ctx: Context): Promise<void> {
2223
this.logger.debug({ cmd }, 'echoing command');
2324
return this.transformJSON(cmd, {});
2425
}
26+
27+
@Handler(NOUN_ECHO, CommandVerb.Help)
28+
public async getHelp(cmd: Command, ctx: Context): Promise<void> {
29+
return this.reply(ctx, this.defaultHelp(cmd));
30+
}
2531
}

src/controller/LearnController.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export class LearnController extends BaseController<LearnControllerData> impleme
6262
}
6363

6464
await this.keywordRepository.save(keyword);
65-
return this.reply(ctx, this.locale.translate('service.controller.learn.keyword.create', {
65+
return this.reply(ctx, this.translate('create.success', {
6666
key,
6767
}));
6868
}
@@ -83,7 +83,7 @@ export class LearnController extends BaseController<LearnControllerData> impleme
8383
id: keyword.id,
8484
key,
8585
});
86-
return this.reply(ctx, this.locale.translate('service.controller.learn.keyword.delete', {
86+
return this.reply(ctx, this.translate('delete.success', {
8787
key,
8888
}));
8989
}
@@ -115,4 +115,9 @@ export class LearnController extends BaseController<LearnControllerData> impleme
115115
await this.bot.executeCommand(merged);
116116
return;
117117
}
118+
119+
@Handler(NOUN_KEYWORD, CommandVerb.Help)
120+
public async getHelp(cmd: Command, ctx: Context): Promise<void> {
121+
return this.reply(ctx, this.defaultHelp(cmd));
122+
}
118123
}

src/controller/MathController.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,19 @@ export class MathController extends BaseController<MathControllerData> implement
4949
return this.reply(ctx, body);
5050
}
5151

52+
@Handler(NOUN_MATH, CommandVerb.Help)
53+
public async getHelp(cmd: Command, ctx: Context): Promise<void> {
54+
return this.reply(ctx, this.defaultHelp(cmd));
55+
}
56+
5257
protected solve(expr: string, scope: object): string {
5358
try {
5459
const body = this.math.eval(expr, scope);
5560
this.logger.debug({ body, expr }, 'evaluated expression');
5661

5762
return formatResult(body, scope, this.data.format);
5863
} catch (err) {
59-
return this.locale.translate('service.controller.math.math.error', {
64+
return this.translate('create.error', {
6065
msg: err.message,
6166
});
6267
}

src/controller/PickController.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,9 @@ export class PickController extends BaseController<PickControllerData> implement
4242
this.logger.debug({ count, data, list, puck }, 'picking item');
4343
return this.reply(ctx, puck.join(','));
4444
}
45+
46+
@Handler(NOUN_PICK, CommandVerb.Help)
47+
public async getHelp(cmd: Command, ctx: Context): Promise<void> {
48+
return this.reply(ctx, this.defaultHelp(cmd));
49+
}
4550
}

src/controller/SearchController.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,9 @@ export class SearchController extends BaseController<SearchControllerData> imple
5454

5555
return this.transformJSON(cmd, response);
5656
}
57+
58+
@Handler(NOUN_SEARCH, CommandVerb.Help)
59+
public async getHelp(cmd: Command, ctx: Context): Promise<void> {
60+
return this.reply(ctx, this.defaultHelp(cmd));
61+
}
5762
}

src/controller/SedController.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class SedController extends BaseController<SedControllerData> implements
3030
const parts = expr.match(/\/((?:[^\\]|\\.)*)\/((?:[^\\]|\\.)*)\/([gmiuy]*)/);
3131
if (isNil(parts)) {
3232
this.logger.debug({ expr }, 'invalid input.');
33-
return this.reply(ctx, this.locale.translate('service.controller.sed.invalid'));
33+
return this.reply(ctx, this.translate('create.invalid'));
3434
}
3535

3636
this.logger.debug({ parts }, 'fetching messages');
@@ -47,12 +47,17 @@ export class SedController extends BaseController<SedControllerData> implements
4747
}
4848
}
4949

50-
return this.reply(ctx, this.locale.translate('service.controller.sed.missing'));
50+
return this.reply(ctx, this.translate('create.missing'));
5151
} catch (error) {
5252
this.logger.error('Failed to fetch messages.');
5353
}
5454
}
5555

56+
@Handler(NOUN_SED, CommandVerb.Help)
57+
public async getHelp(cmd: Command, ctx: Context): Promise<void> {
58+
return this.reply(ctx, this.defaultHelp(cmd));
59+
}
60+
5661
private async processMessage(message: Message, command: Command, parts: RegExpMatchArray): Promise<boolean> {
5762
if (doesExist(message.context) && doesExist(command.context) && message.context.channel.thread === command.context.channel.thread) {
5863
return false;

src/controller/TimeController.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,13 @@ export class TimeController extends BaseController<TimeControllerData> implement
3636
const zone = cmd.getHeadOrDefault('zone', this.data.zone);
3737

3838
this.logger.debug({ locale, time, zone }, 'handling time');
39-
return this.reply(ctx, this.locale.translate('service.controller.time.get', {
39+
return this.reply(ctx, this.translate('get.success', {
4040
time,
4141
}));
4242
}
43+
44+
@Handler(NOUN_TIME, CommandVerb.Help)
45+
public async getHelp(cmd: Command, ctx: Context): Promise<void> {
46+
return this.reply(ctx, this.defaultHelp(cmd));
47+
}
4348
}

src/controller/TokenController.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export class TokenController extends BaseController<TokenControllerData> impleme
7070
const user = this.getUserOrFail(ctx);
7171
const before = cmd.getHeadOrNumber('before', this.clock.getSeconds());
7272
if (cmd.getHeadOrDefault('confirm', 'no') !== 'yes') {
73-
const completion = createCompletion(cmd, 'confirm', this.locale.translate('service.controller.token.delete.confirm', {
73+
const completion = createCompletion(cmd, 'confirm', this.translate('delete.confirm', {
7474
before,
7575
name: user.name,
7676
}));
@@ -83,7 +83,7 @@ export class TokenController extends BaseController<TokenControllerData> impleme
8383
subject: Equal(mustExist(user.id)),
8484
});
8585

86-
return this.reply(ctx, `tokens deleted`);
86+
return this.reply(ctx, this.translate('delete.success'));
8787
}
8888

8989
@Handler(NOUN_TOKEN, CommandVerb.Get)
@@ -97,13 +97,13 @@ export class TokenController extends BaseController<TokenControllerData> impleme
9797
});
9898
return this.reply(ctx, JSON.stringify(data));
9999
} catch (err) {
100-
return this.reply(ctx, this.locale.translate('service.controller.token.get.invalid', {
100+
return this.reply(ctx, this.translate('get.invalid', {
101101
msg: err.message,
102102
}));
103103
}
104104
} else {
105105
if (isNil(ctx.token)) {
106-
return this.reply(ctx, this.locale.translate('service.controller.token.get.missing'));
106+
return this.reply(ctx, this.translate('get.missing'));
107107
} else {
108108
return this.reply(ctx, ctx.token.toString());
109109
}
@@ -122,4 +122,9 @@ export class TokenController extends BaseController<TokenControllerData> impleme
122122

123123
return this.reply(ctx, JSON.stringify(tokens));
124124
}
125+
126+
@Handler(NOUN_TOKEN, CommandVerb.Help)
127+
public async getHelp(cmd: Command, ctx: Context): Promise<void> {
128+
return this.reply(ctx, this.defaultHelp(cmd));
129+
}
125130
}

0 commit comments

Comments
 (0)