chz-telegram-bot 0.3.25 → 0.3.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/builtin/helpAction.ts +1 -1
- package/dist/builtin/helpAction.js +1 -1
- package/dist/dtos/cooldownInfo.d.ts +6 -6
- package/dist/dtos/cooldownInfo.d.ts.map +1 -1
- package/dist/dtos/cooldownInfo.js +4 -4
- package/dist/dtos/propertyProviderSets.d.ts +16 -0
- package/dist/dtos/propertyProviderSets.d.ts.map +1 -0
- package/dist/dtos/propertyProviderSets.js +2 -0
- package/dist/entities/actions/commandAction.d.ts +12 -12
- package/dist/entities/actions/commandAction.d.ts.map +1 -1
- package/dist/entities/actions/commandAction.js +33 -26
- package/dist/entities/actions/inlineQueryAction.d.ts +3 -2
- package/dist/entities/actions/inlineQueryAction.d.ts.map +1 -1
- package/dist/entities/actions/inlineQueryAction.js +4 -4
- package/dist/entities/actions/scheduledAction.d.ts +5 -5
- package/dist/entities/actions/scheduledAction.d.ts.map +1 -1
- package/dist/entities/actions/scheduledAction.js +14 -13
- package/dist/helpers/builders/commandActionBuilder.d.ts +18 -7
- package/dist/helpers/builders/commandActionBuilder.d.ts.map +1 -1
- package/dist/helpers/builders/commandActionBuilder.js +35 -14
- package/dist/helpers/builders/inlineQueryActionBuilder.d.ts +8 -1
- package/dist/helpers/builders/inlineQueryActionBuilder.d.ts.map +1 -1
- package/dist/helpers/builders/inlineQueryActionBuilder.js +11 -3
- package/dist/helpers/builders/scheduledActionBuilder.d.ts +13 -4
- package/dist/helpers/builders/scheduledActionBuilder.d.ts.map +1 -1
- package/dist/helpers/builders/scheduledActionBuilder.js +24 -8
- package/dist/types/propertyProvider.d.ts +8 -0
- package/dist/types/propertyProvider.d.ts.map +1 -0
- package/dist/types/propertyProvider.js +2 -0
- package/dtos/cooldownInfo.ts +3 -3
- package/dtos/propertyProviderSets.ts +20 -0
- package/entities/actions/commandAction.ts +59 -36
- package/entities/actions/inlineQueryAction.ts +5 -4
- package/entities/actions/scheduledAction.ts +26 -15
- package/helpers/builders/commandActionBuilder.ts +57 -21
- package/helpers/builders/inlineQueryActionBuilder.ts +16 -3
- package/helpers/builders/scheduledActionBuilder.ts +36 -10
- package/package.json +44 -44
- package/types/propertyProvider.ts +14 -0
- package/dist/dtos/actionPermissionsData.d.ts +0 -7
- package/dist/dtos/actionPermissionsData.d.ts.map +0 -1
- package/dist/dtos/actionPermissionsData.js +0 -14
- package/dtos/actionPermissionsData.ts +0 -7
|
@@ -2,18 +2,19 @@ import { ScheduledAction } from '../../entities/actions/scheduledAction';
|
|
|
2
2
|
import { ActionStateBase } from '../../entities/states/actionStateBase';
|
|
3
3
|
import { IActionState } from '../../types/actionState';
|
|
4
4
|
import { ScheduledHandler } from '../../types/handlers';
|
|
5
|
+
import { ScheduledActionPropertyProvider } from '../../types/propertyProvider';
|
|
5
6
|
import { Hours, HoursOfDay } from '../../types/timeValues';
|
|
6
7
|
/**
|
|
7
8
|
* Builder for `ScheduledAction` with state represented by `TActionState`
|
|
8
9
|
*/
|
|
9
10
|
export declare class ScheduledActionBuilderWithState<TActionState extends IActionState> {
|
|
10
|
-
private
|
|
11
|
-
private time;
|
|
11
|
+
private readonly name;
|
|
12
12
|
private readonly cachedStateFactories;
|
|
13
|
-
private whitelist;
|
|
14
13
|
private readonly stateConstructor;
|
|
15
14
|
private handler;
|
|
16
|
-
private
|
|
15
|
+
private whitelistProvider;
|
|
16
|
+
private activeProvider;
|
|
17
|
+
private timeinHoursProvider;
|
|
17
18
|
/**
|
|
18
19
|
* Builder for `ScheduledAction` with state represented by `TActionState`
|
|
19
20
|
* @param name Action name, will be used for logging and storage
|
|
@@ -44,6 +45,14 @@ export declare class ScheduledActionBuilderWithState<TActionState extends IActio
|
|
|
44
45
|
withSharedCache(key: string, itemFactory: () => Promise<unknown>, invalidationTimeoutInHours?: Hours): this;
|
|
45
46
|
/** If called during building, action is marked as disabled and never checked. */
|
|
46
47
|
disabled(): this;
|
|
48
|
+
/**
|
|
49
|
+
* Configures action to use property value providers instead of static value to allow changes in runtime
|
|
50
|
+
*/
|
|
51
|
+
withConfiguration(configuration: {
|
|
52
|
+
timeinHoursProvider?: ScheduledActionPropertyProvider<HoursOfDay>;
|
|
53
|
+
isActiveProvider?: ScheduledActionPropertyProvider<boolean>;
|
|
54
|
+
chatsWhitelistProvider?: ScheduledActionPropertyProvider<number[]>;
|
|
55
|
+
}): this;
|
|
47
56
|
/** Builds action */
|
|
48
57
|
build(): ScheduledAction<TActionState>;
|
|
49
58
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scheduledActionBuilder.d.ts","sourceRoot":"","sources":["../../../helpers/builders/scheduledActionBuilder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,wCAAwC,CAAC;AAEzE,OAAO,EAAE,eAAe,EAAE,MAAM,uCAAuC,CAAC;AACxE,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AACxD,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAG3D;;GAEG;AACH,qBAAa,+BAA+B,CACxC,YAAY,SAAS,YAAY;IAEjC,OAAO,CAAC,
|
|
1
|
+
{"version":3,"file":"scheduledActionBuilder.d.ts","sourceRoot":"","sources":["../../../helpers/builders/scheduledActionBuilder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,wCAAwC,CAAC;AAEzE,OAAO,EAAE,eAAe,EAAE,MAAM,uCAAuC,CAAC;AACxE,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AACxD,OAAO,EAAE,+BAA+B,EAAE,MAAM,8BAA8B,CAAC;AAC/E,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAG3D;;GAEG;AACH,qBAAa,+BAA+B,CACxC,YAAY,SAAS,YAAY;IAEjC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAS;IAC9B,OAAO,CAAC,QAAQ,CAAC,oBAAoB,CAGjC;IACJ,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAqB;IACtD,OAAO,CAAC,OAAO,CAA6C;IAE5D,OAAO,CAAC,iBAAiB,CACZ;IACb,OAAO,CAAC,cAAc,CACb;IACT,OAAO,CAAC,mBAAmB,CACf;IAEZ;;;;OAIG;gBACS,IAAI,EAAE,MAAM,EAAE,gBAAgB,EAAE,MAAM,YAAY;IAK9D;;;OAGG;IACH,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE;IAMpB;;;OAGG;IACH,KAAK,CAAC,IAAI,EAAE,UAAU;IAMtB;;OAEG;IACH,EAAE,CAAC,OAAO,EAAE,gBAAgB,CAAC,YAAY,CAAC;IAM1C;;;;;;OAMG;IACH,eAAe,CACX,GAAG,EAAE,MAAM,EACX,WAAW,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,EACnC,0BAA0B,GAAE,KAAmB;IAUnD,iFAAiF;IACjF,QAAQ;IAMR;;OAEG;IACH,iBAAiB,CAAC,aAAa,EAAE;QAC7B,mBAAmB,CAAC,EAAE,+BAA+B,CAAC,UAAU,CAAC,CAAC;QAClE,gBAAgB,CAAC,EAAE,+BAA+B,CAAC,OAAO,CAAC,CAAC;QAC5D,sBAAsB,CAAC,EAAE,+BAA+B,CAAC,MAAM,EAAE,CAAC,CAAC;KACtE;IAaD,oBAAoB;IACpB,KAAK;CAaR;AAED;;GAEG;AACH,qBAAa,sBAAuB,SAAQ,+BAA+B,CAAC,eAAe,CAAC;IACxF;;OAEG;gBACS,IAAI,EAAE,MAAM;CAG3B"}
|
|
@@ -9,13 +9,13 @@ const noop_1 = require("../noop");
|
|
|
9
9
|
* Builder for `ScheduledAction` with state represented by `TActionState`
|
|
10
10
|
*/
|
|
11
11
|
class ScheduledActionBuilderWithState {
|
|
12
|
-
|
|
13
|
-
time = 0;
|
|
12
|
+
name;
|
|
14
13
|
cachedStateFactories = new Map();
|
|
15
|
-
whitelist = [];
|
|
16
14
|
stateConstructor;
|
|
17
15
|
handler = noop_1.Noop.call;
|
|
18
|
-
|
|
16
|
+
whitelistProvider = () => [];
|
|
17
|
+
activeProvider = () => true;
|
|
18
|
+
timeinHoursProvider = () => 0;
|
|
19
19
|
/**
|
|
20
20
|
* Builder for `ScheduledAction` with state represented by `TActionState`
|
|
21
21
|
* @param name Action name, will be used for logging and storage
|
|
@@ -30,7 +30,7 @@ class ScheduledActionBuilderWithState {
|
|
|
30
30
|
* @param chatIds Chat ids to execute in.
|
|
31
31
|
*/
|
|
32
32
|
in(chatIds) {
|
|
33
|
-
this.
|
|
33
|
+
this.whitelistProvider = () => chatIds;
|
|
34
34
|
return this;
|
|
35
35
|
}
|
|
36
36
|
/**
|
|
@@ -38,7 +38,7 @@ class ScheduledActionBuilderWithState {
|
|
|
38
38
|
* @param time Time of day (0 - 23) to execute action.
|
|
39
39
|
*/
|
|
40
40
|
runAt(time) {
|
|
41
|
-
this.
|
|
41
|
+
this.timeinHoursProvider = () => time;
|
|
42
42
|
return this;
|
|
43
43
|
}
|
|
44
44
|
/** Defines action logic itself, will be executed on timer.
|
|
@@ -61,12 +61,28 @@ class ScheduledActionBuilderWithState {
|
|
|
61
61
|
}
|
|
62
62
|
/** If called during building, action is marked as disabled and never checked. */
|
|
63
63
|
disabled() {
|
|
64
|
-
this.
|
|
64
|
+
this.activeProvider = () => false;
|
|
65
|
+
return this;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Configures action to use property value providers instead of static value to allow changes in runtime
|
|
69
|
+
*/
|
|
70
|
+
withConfiguration(configuration) {
|
|
71
|
+
if (configuration.chatsWhitelistProvider)
|
|
72
|
+
this.whitelistProvider = configuration.chatsWhitelistProvider;
|
|
73
|
+
if (configuration.timeinHoursProvider)
|
|
74
|
+
this.timeinHoursProvider = configuration.timeinHoursProvider;
|
|
75
|
+
if (configuration.isActiveProvider)
|
|
76
|
+
this.activeProvider = configuration.isActiveProvider;
|
|
65
77
|
return this;
|
|
66
78
|
}
|
|
67
79
|
/** Builds action */
|
|
68
80
|
build() {
|
|
69
|
-
return new scheduledAction_1.ScheduledAction(this.name, this.handler,
|
|
81
|
+
return new scheduledAction_1.ScheduledAction(this.name, this.handler, {
|
|
82
|
+
chatsWhitelistProvider: this.whitelistProvider,
|
|
83
|
+
isActiveProvider: this.activeProvider,
|
|
84
|
+
timeinHoursProvider: this.timeinHoursProvider
|
|
85
|
+
}, this.cachedStateFactories, this.stateConstructor);
|
|
70
86
|
}
|
|
71
87
|
}
|
|
72
88
|
exports.ScheduledActionBuilderWithState = ScheduledActionBuilderWithState;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ChatContext } from '../entities/context/chatContext';
|
|
2
|
+
import { InlineQueryContext } from '../entities/context/inlineQueryContext';
|
|
3
|
+
import { MessageContext } from '../entities/context/messageContext';
|
|
4
|
+
import { IActionState } from './actionState';
|
|
5
|
+
export type CommandActionPropertyProvider<T> = (ctx: MessageContext<IActionState>) => T;
|
|
6
|
+
export type InlineActionPropertyProvider<T> = (ctx: InlineQueryContext) => T;
|
|
7
|
+
export type ScheduledActionPropertyProvider<T> = (ctx: ChatContext<IActionState>) => T;
|
|
8
|
+
//# sourceMappingURL=propertyProvider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"propertyProvider.d.ts","sourceRoot":"","sources":["../../types/propertyProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAC9D,OAAO,EAAE,kBAAkB,EAAE,MAAM,wCAAwC,CAAC;AAC5E,OAAO,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAC;AACpE,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAE7C,MAAM,MAAM,6BAA6B,CAAC,CAAC,IAAI,CAC3C,GAAG,EAAE,cAAc,CAAC,YAAY,CAAC,KAChC,CAAC,CAAC;AAEP,MAAM,MAAM,4BAA4B,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,kBAAkB,KAAK,CAAC,CAAC;AAE7E,MAAM,MAAM,+BAA+B,CAAC,CAAC,IAAI,CAC7C,GAAG,EAAE,WAAW,CAAC,YAAY,CAAC,KAC7B,CAAC,CAAC"}
|
package/dtos/cooldownInfo.ts
CHANGED
|
@@ -2,9 +2,9 @@ import { Seconds } from '../types/timeValues';
|
|
|
2
2
|
|
|
3
3
|
export class CooldownInfo {
|
|
4
4
|
constructor(
|
|
5
|
-
/**
|
|
6
|
-
readonly
|
|
5
|
+
/** Cooldown configuration */
|
|
6
|
+
readonly cooldown: Seconds,
|
|
7
7
|
/** Cooldown message to be shown */
|
|
8
|
-
readonly message
|
|
8
|
+
readonly message?: string
|
|
9
9
|
) {}
|
|
10
10
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { CooldownInfo } from './cooldownInfo';
|
|
2
|
+
import {
|
|
3
|
+
CommandActionPropertyProvider,
|
|
4
|
+
ScheduledActionPropertyProvider
|
|
5
|
+
} from '../types/propertyProvider';
|
|
6
|
+
import { HoursOfDay } from '../types/timeValues';
|
|
7
|
+
|
|
8
|
+
export type CommandActionProviders = {
|
|
9
|
+
cooldownProvider: CommandActionPropertyProvider<CooldownInfo>;
|
|
10
|
+
isActiveProvider: CommandActionPropertyProvider<boolean>;
|
|
11
|
+
chatsBlacklistProvider: CommandActionPropertyProvider<number[]>;
|
|
12
|
+
chatsWhitelistProvider: CommandActionPropertyProvider<number[]>;
|
|
13
|
+
usersWhitelistProvider: CommandActionPropertyProvider<number[]>;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type ScheduledActionProviders = {
|
|
17
|
+
timeinHoursProvider: ScheduledActionPropertyProvider<HoursOfDay>;
|
|
18
|
+
isActiveProvider: ScheduledActionPropertyProvider<boolean>;
|
|
19
|
+
chatsWhitelistProvider: ScheduledActionPropertyProvider<number[]>;
|
|
20
|
+
};
|
|
@@ -16,26 +16,36 @@ import { CooldownInfo } from '../../dtos/cooldownInfo';
|
|
|
16
16
|
import { TextMessage } from '../../dtos/responses/textMessage';
|
|
17
17
|
import { ReplyInfo } from '../../dtos/replyInfo';
|
|
18
18
|
import { Seconds } from '../../types/timeValues';
|
|
19
|
-
import {
|
|
19
|
+
import { CommandActionPropertyProvider } from '../../types/propertyProvider';
|
|
20
|
+
import { CommandActionProviders } from '../../dtos/propertyProviderSets';
|
|
21
|
+
import { BotResponse } from '../../types/response';
|
|
20
22
|
|
|
21
23
|
export class CommandAction<TActionState extends IActionState>
|
|
22
24
|
implements IActionWithState<TActionState>
|
|
23
25
|
{
|
|
26
|
+
private readonly cooldownInfoProvider: CommandActionPropertyProvider<CooldownInfo>;
|
|
27
|
+
private readonly isActiveProvider: CommandActionPropertyProvider<boolean>;
|
|
28
|
+
private readonly chatsBlacklistProvider: CommandActionPropertyProvider<
|
|
29
|
+
number[]
|
|
30
|
+
>;
|
|
31
|
+
private readonly chatsWhitelistProvider: CommandActionPropertyProvider<
|
|
32
|
+
number[]
|
|
33
|
+
>;
|
|
34
|
+
private readonly usersWhitelistProvider: CommandActionPropertyProvider<
|
|
35
|
+
number[]
|
|
36
|
+
>;
|
|
37
|
+
|
|
38
|
+
readonly key: ActionKey;
|
|
39
|
+
readonly name: string;
|
|
24
40
|
readonly ratelimitSemaphores = new Map<number, Semaphore>();
|
|
41
|
+
readonly maxAllowedSimultaniousExecutions: number;
|
|
25
42
|
|
|
26
43
|
readonly triggers: CommandTrigger[];
|
|
44
|
+
|
|
27
45
|
readonly handler: CommandHandler<TActionState>;
|
|
28
|
-
readonly name: string;
|
|
29
|
-
readonly cooldownInfo: CooldownInfo;
|
|
30
|
-
readonly active: boolean;
|
|
31
|
-
readonly chatsBlacklist: number[];
|
|
32
|
-
readonly chatsWhitelist: number[];
|
|
33
|
-
readonly usersWhitelist: number[];
|
|
34
46
|
readonly condition: CommandCondition<TActionState>;
|
|
35
47
|
readonly stateConstructor: () => TActionState;
|
|
36
|
-
readonly key: ActionKey;
|
|
37
48
|
readonly readmeFactory: (botName: string) => string;
|
|
38
|
-
readonly maxAllowedSimultaniousExecutions: number;
|
|
39
49
|
|
|
40
50
|
private lastCustomCooldown: Seconds | undefined;
|
|
41
51
|
|
|
@@ -43,32 +53,35 @@ export class CommandAction<TActionState extends IActionState>
|
|
|
43
53
|
trigger: CommandTrigger | CommandTrigger[],
|
|
44
54
|
handler: CommandHandler<TActionState>,
|
|
45
55
|
name: string,
|
|
46
|
-
|
|
47
|
-
cooldownInfo: CooldownInfo,
|
|
48
|
-
permissionsData: ActionPermissionsData,
|
|
56
|
+
providers: CommandActionProviders,
|
|
49
57
|
maxAllowedSimultaniousExecutions: number,
|
|
50
58
|
condition: CommandCondition<TActionState>,
|
|
51
59
|
stateConstructor: () => TActionState,
|
|
52
60
|
readmeFactory: (botName: string) => string
|
|
53
61
|
) {
|
|
54
62
|
this.triggers = toArray(trigger);
|
|
55
|
-
this.handler = handler;
|
|
56
63
|
this.name = name;
|
|
57
|
-
|
|
58
|
-
this.
|
|
59
|
-
this.
|
|
60
|
-
this.
|
|
61
|
-
this.
|
|
64
|
+
|
|
65
|
+
this.cooldownInfoProvider = providers.cooldownProvider;
|
|
66
|
+
this.isActiveProvider = providers.isActiveProvider;
|
|
67
|
+
this.chatsBlacklistProvider = providers.chatsBlacklistProvider;
|
|
68
|
+
this.chatsWhitelistProvider = providers.chatsWhitelistProvider;
|
|
69
|
+
this.usersWhitelistProvider = providers.usersWhitelistProvider;
|
|
70
|
+
|
|
71
|
+
this.handler = handler;
|
|
62
72
|
this.condition = condition;
|
|
63
73
|
this.stateConstructor = stateConstructor;
|
|
64
74
|
this.readmeFactory = readmeFactory;
|
|
75
|
+
|
|
65
76
|
this.maxAllowedSimultaniousExecutions =
|
|
66
77
|
maxAllowedSimultaniousExecutions;
|
|
67
78
|
|
|
68
79
|
this.key = `command:${this.name.replace('.', '-')}` as ActionKey;
|
|
69
80
|
}
|
|
70
81
|
|
|
71
|
-
async exec(
|
|
82
|
+
async exec(
|
|
83
|
+
ctx: MessageContextInternal<TActionState>
|
|
84
|
+
): Promise<BotResponse[]> {
|
|
72
85
|
if (!ctx.isInitialized)
|
|
73
86
|
throw new Error(
|
|
74
87
|
`Context for ${this.key} is not initialized or already consumed`
|
|
@@ -100,16 +113,22 @@ export class CommandAction<TActionState extends IActionState>
|
|
|
100
113
|
);
|
|
101
114
|
|
|
102
115
|
if (!shouldExecute) {
|
|
103
|
-
if (reason == 'OnCooldown'
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
116
|
+
if (reason == 'OnCooldown') {
|
|
117
|
+
const cooldownMessage =
|
|
118
|
+
this.cooldownInfoProvider(ctx).message;
|
|
119
|
+
|
|
120
|
+
return cooldownMessage
|
|
121
|
+
? [
|
|
122
|
+
new TextMessage(
|
|
123
|
+
cooldownMessage,
|
|
124
|
+
ctx.chatInfo,
|
|
125
|
+
ctx.traceId,
|
|
126
|
+
this,
|
|
127
|
+
new ReplyInfo(ctx.messageInfo.id)
|
|
128
|
+
)
|
|
129
|
+
]
|
|
130
|
+
: Noop.NoResponse;
|
|
131
|
+
}
|
|
113
132
|
|
|
114
133
|
return Noop.NoResponse;
|
|
115
134
|
}
|
|
@@ -148,15 +167,18 @@ export class CommandAction<TActionState extends IActionState>
|
|
|
148
167
|
trigger: CommandTrigger,
|
|
149
168
|
state: TActionState
|
|
150
169
|
) {
|
|
151
|
-
if (!this.
|
|
170
|
+
if (!this.isActiveProvider(ctx))
|
|
152
171
|
return CommandTriggerCheckResult.DontTriggerAndSkipCooldown(
|
|
153
172
|
'ActionDisabled'
|
|
154
173
|
);
|
|
155
174
|
|
|
156
|
-
const
|
|
175
|
+
const chatsBlacklist = this.chatsBlacklistProvider(ctx);
|
|
176
|
+
const chatsWhitelist = this.chatsWhitelistProvider(ctx);
|
|
177
|
+
|
|
178
|
+
const isChatInBlacklist = chatsBlacklist.includes(ctx.chatInfo.id);
|
|
157
179
|
const isChatInWhitelist =
|
|
158
|
-
|
|
159
|
-
|
|
180
|
+
chatsWhitelist.length == 0 ||
|
|
181
|
+
chatsWhitelist.includes(ctx.chatInfo.id);
|
|
160
182
|
|
|
161
183
|
if (isChatInBlacklist || !isChatInWhitelist)
|
|
162
184
|
return CommandTriggerCheckResult.DontTriggerAndSkipCooldown(
|
|
@@ -172,9 +194,10 @@ export class CommandAction<TActionState extends IActionState>
|
|
|
172
194
|
'UserIdMissing'
|
|
173
195
|
);
|
|
174
196
|
|
|
197
|
+
const usersWhitelist = this.usersWhitelistProvider(ctx);
|
|
175
198
|
const isUserAllowed =
|
|
176
|
-
|
|
177
|
-
|
|
199
|
+
usersWhitelist.length == 0 ||
|
|
200
|
+
usersWhitelist.includes(ctx.userInfo.id);
|
|
178
201
|
|
|
179
202
|
if (!isUserAllowed)
|
|
180
203
|
return CommandTriggerCheckResult.DontTriggerAndSkipCooldown(
|
|
@@ -183,7 +206,7 @@ export class CommandAction<TActionState extends IActionState>
|
|
|
183
206
|
|
|
184
207
|
const lastExecutedDate = moment(state.lastExecutedDate);
|
|
185
208
|
const cooldownInMilliseconds = secondsToMilliseconds(
|
|
186
|
-
this.lastCustomCooldown ?? this.
|
|
209
|
+
this.lastCustomCooldown ?? this.cooldownInfoProvider(ctx).cooldown
|
|
187
210
|
);
|
|
188
211
|
const onCooldown =
|
|
189
212
|
moment().diff(lastExecutedDate) < cooldownInMilliseconds;
|
|
@@ -2,10 +2,11 @@ import { Noop } from '../../helpers/noop';
|
|
|
2
2
|
import { ActionKey, IAction } from '../../types/action';
|
|
3
3
|
import { InlineQueryContextInternal } from '../context/inlineQueryContext';
|
|
4
4
|
import { InlineQueryHandler } from '../../types/handlers';
|
|
5
|
+
import { InlineActionPropertyProvider } from '../../types/propertyProvider';
|
|
5
6
|
|
|
6
7
|
export class InlineQueryAction implements IAction {
|
|
7
8
|
readonly key: ActionKey;
|
|
8
|
-
readonly
|
|
9
|
+
readonly isActiveProvider: InlineActionPropertyProvider<boolean>;
|
|
9
10
|
readonly handler: InlineQueryHandler;
|
|
10
11
|
readonly name: string;
|
|
11
12
|
readonly pattern: RegExp;
|
|
@@ -13,12 +14,12 @@ export class InlineQueryAction implements IAction {
|
|
|
13
14
|
constructor(
|
|
14
15
|
handler: InlineQueryHandler,
|
|
15
16
|
name: string,
|
|
16
|
-
|
|
17
|
+
activeProvider: InlineActionPropertyProvider<boolean>,
|
|
17
18
|
pattern: RegExp
|
|
18
19
|
) {
|
|
19
20
|
this.handler = handler;
|
|
20
21
|
this.name = name;
|
|
21
|
-
this.
|
|
22
|
+
this.isActiveProvider = activeProvider;
|
|
22
23
|
this.pattern = pattern;
|
|
23
24
|
|
|
24
25
|
this.key = `inline:${this.name.replace('.', '-')}` as ActionKey;
|
|
@@ -30,7 +31,7 @@ export class InlineQueryAction implements IAction {
|
|
|
30
31
|
`Context for ${this.key} is not initialized or already consumed`
|
|
31
32
|
);
|
|
32
33
|
|
|
33
|
-
if (!this.
|
|
34
|
+
if (!this.isActiveProvider(ctx)) return Noop.NoResponse;
|
|
34
35
|
|
|
35
36
|
const matchResults: RegExpExecArray[] = [];
|
|
36
37
|
|
|
@@ -10,6 +10,8 @@ import { ChatContextInternal } from '../context/chatContext';
|
|
|
10
10
|
import { Noop } from '../../helpers/noop';
|
|
11
11
|
import { IScheduler } from '../../types/scheduler';
|
|
12
12
|
import { getOrSetIfNotExists, getOrThrow } from '../../helpers/mapUtils';
|
|
13
|
+
import { ScheduledActionPropertyProvider } from '../../types/propertyProvider';
|
|
14
|
+
import { ScheduledActionProviders } from '../../dtos/propertyProviderSets';
|
|
13
15
|
|
|
14
16
|
export class ScheduledAction<TActionState extends IActionState>
|
|
15
17
|
implements IActionWithState<TActionState>
|
|
@@ -17,11 +19,14 @@ export class ScheduledAction<TActionState extends IActionState>
|
|
|
17
19
|
static readonly locks = new Map<string, Semaphore>();
|
|
18
20
|
|
|
19
21
|
readonly name: string;
|
|
20
|
-
readonly timeinHours: HoursOfDay;
|
|
21
|
-
readonly active: boolean;
|
|
22
|
-
readonly chatsWhitelist: number[];
|
|
23
22
|
readonly key: ActionKey;
|
|
24
23
|
|
|
24
|
+
private readonly timeinHoursProvider: ScheduledActionPropertyProvider<HoursOfDay>;
|
|
25
|
+
private readonly activeProvider: ScheduledActionPropertyProvider<boolean>;
|
|
26
|
+
private readonly chatsWhitelistProvider: ScheduledActionPropertyProvider<
|
|
27
|
+
number[]
|
|
28
|
+
>;
|
|
29
|
+
|
|
25
30
|
readonly cachedState = new Map<string, unknown>();
|
|
26
31
|
readonly stateConstructor: () => TActionState;
|
|
27
32
|
readonly cachedStateFactories: Map<string, CachedStateFactory>;
|
|
@@ -30,20 +35,20 @@ export class ScheduledAction<TActionState extends IActionState>
|
|
|
30
35
|
constructor(
|
|
31
36
|
name: string,
|
|
32
37
|
handler: ScheduledHandler<TActionState>,
|
|
33
|
-
|
|
34
|
-
active: boolean,
|
|
35
|
-
whitelist: number[],
|
|
38
|
+
providers: ScheduledActionProviders,
|
|
36
39
|
cachedStateFactories: Map<string, CachedStateFactory>,
|
|
37
40
|
stateConstructor: () => TActionState
|
|
38
41
|
) {
|
|
39
42
|
this.name = name;
|
|
40
|
-
this.handler = handler;
|
|
41
|
-
this.timeinHours = timeinHours;
|
|
42
|
-
this.active = active;
|
|
43
|
-
this.chatsWhitelist = whitelist;
|
|
44
|
-
this.cachedStateFactories = cachedStateFactories;
|
|
45
43
|
this.key = `scheduled:${this.name.replace('.', '-')}` as ActionKey;
|
|
44
|
+
|
|
45
|
+
this.timeinHoursProvider = providers.timeinHoursProvider;
|
|
46
|
+
this.activeProvider = providers.isActiveProvider;
|
|
47
|
+
this.chatsWhitelistProvider = providers.chatsWhitelistProvider;
|
|
48
|
+
|
|
49
|
+
this.cachedStateFactories = cachedStateFactories;
|
|
46
50
|
this.stateConstructor = stateConstructor;
|
|
51
|
+
this.handler = handler;
|
|
47
52
|
}
|
|
48
53
|
|
|
49
54
|
async exec(ctx: ChatContextInternal<TActionState>) {
|
|
@@ -52,7 +57,10 @@ export class ScheduledAction<TActionState extends IActionState>
|
|
|
52
57
|
`Context for ${this.key} is not initialized or already consumed`
|
|
53
58
|
);
|
|
54
59
|
|
|
55
|
-
if (
|
|
60
|
+
if (
|
|
61
|
+
!this.activeProvider(ctx) ||
|
|
62
|
+
!this.chatsWhitelistProvider(ctx).includes(ctx.chatInfo.id)
|
|
63
|
+
)
|
|
56
64
|
return Noop.NoResponse;
|
|
57
65
|
|
|
58
66
|
const state = await ctx.storage.getActionState<TActionState>(
|
|
@@ -60,7 +68,7 @@ export class ScheduledAction<TActionState extends IActionState>
|
|
|
60
68
|
ctx.chatInfo.id
|
|
61
69
|
);
|
|
62
70
|
|
|
63
|
-
const isAllowedToTrigger = this.checkIfShouldBeExecuted(state);
|
|
71
|
+
const isAllowedToTrigger = this.checkIfShouldBeExecuted(state, ctx);
|
|
64
72
|
if (!isAllowedToTrigger) return Noop.NoResponse;
|
|
65
73
|
|
|
66
74
|
ctx.logger.logWithTraceId(
|
|
@@ -129,13 +137,16 @@ export class ScheduledAction<TActionState extends IActionState>
|
|
|
129
137
|
}
|
|
130
138
|
}
|
|
131
139
|
|
|
132
|
-
private checkIfShouldBeExecuted(
|
|
140
|
+
private checkIfShouldBeExecuted(
|
|
141
|
+
state: IActionState,
|
|
142
|
+
ctx: ChatContextInternal<TActionState>
|
|
143
|
+
): boolean {
|
|
133
144
|
const startOfToday = moment().startOf('day').valueOf();
|
|
134
145
|
const lastExecutedDate = moment(state.lastExecutedDate);
|
|
135
146
|
const currentTime = moment();
|
|
136
147
|
const scheduledTime = moment()
|
|
137
148
|
.startOf('day')
|
|
138
|
-
.add(this.
|
|
149
|
+
.add(this.timeinHoursProvider(ctx), 'hours');
|
|
139
150
|
|
|
140
151
|
const isAllowedToTrigger = currentTime.isSameOrAfter(scheduledTime);
|
|
141
152
|
const hasTriggeredToday = lastExecutedDate.isAfter(startOfToday);
|
|
@@ -8,7 +8,7 @@ import { toArray } from '../toArray';
|
|
|
8
8
|
import { Noop } from '../noop';
|
|
9
9
|
import { CommandTrigger } from '../../types/commandTrigger';
|
|
10
10
|
import { CooldownInfo } from '../../dtos/cooldownInfo';
|
|
11
|
-
import {
|
|
11
|
+
import { CommandActionPropertyProvider } from '../../types/propertyProvider';
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* Builder for `CommandAction` with state represented by `TActionState`
|
|
@@ -17,17 +17,21 @@ export class CommandActionBuilderWithState<TActionState extends IActionState> {
|
|
|
17
17
|
private readonly name: string;
|
|
18
18
|
private trigger: CommandTrigger | CommandTrigger[] = [];
|
|
19
19
|
|
|
20
|
-
private
|
|
21
|
-
private
|
|
22
|
-
|
|
23
|
-
private
|
|
24
|
-
|
|
20
|
+
private activeProvider: CommandActionPropertyProvider<boolean> = () => true;
|
|
21
|
+
private cooldownSettingsProvider: CommandActionPropertyProvider<CooldownInfo> =
|
|
22
|
+
() => new CooldownInfo(0 as Seconds);
|
|
23
|
+
private blacklistProvider: CommandActionPropertyProvider<number[]> =
|
|
24
|
+
() => [];
|
|
25
|
+
private whitelistProvider: CommandActionPropertyProvider<number[]> =
|
|
26
|
+
() => [];
|
|
27
|
+
private allowedUsersProvider: CommandActionPropertyProvider<number[]> =
|
|
28
|
+
() => [];
|
|
29
|
+
|
|
25
30
|
private readmeFactory: (botName: string) => string = Noop.emptyString;
|
|
26
31
|
private readonly stateConstructor: () => TActionState;
|
|
27
32
|
private handler: CommandHandler<TActionState> = Noop.call;
|
|
28
33
|
private condition: CommandCondition<TActionState> = Noop.true;
|
|
29
34
|
private maxAllowedSimultaniousExecutions: number = 0;
|
|
30
|
-
private cooldownMessage: string | undefined;
|
|
31
35
|
|
|
32
36
|
/**
|
|
33
37
|
* Builder for `CommandAction` with state represented by `TActionState`
|
|
@@ -55,7 +59,8 @@ export class CommandActionBuilderWithState<TActionState extends IActionState> {
|
|
|
55
59
|
* @param id User id or ids
|
|
56
60
|
*/
|
|
57
61
|
from(id: number | number[]) {
|
|
58
|
-
|
|
62
|
+
const ids = toArray(id);
|
|
63
|
+
this.allowedUsersProvider = () => ids;
|
|
59
64
|
|
|
60
65
|
return this;
|
|
61
66
|
}
|
|
@@ -65,7 +70,7 @@ export class CommandActionBuilderWithState<TActionState extends IActionState> {
|
|
|
65
70
|
* @param chatIds Chats ids to allow.
|
|
66
71
|
*/
|
|
67
72
|
in(chatIds: number[]) {
|
|
68
|
-
this.
|
|
73
|
+
this.whitelistProvider = () => chatIds;
|
|
69
74
|
|
|
70
75
|
return this;
|
|
71
76
|
}
|
|
@@ -75,7 +80,7 @@ export class CommandActionBuilderWithState<TActionState extends IActionState> {
|
|
|
75
80
|
* @param chatIds Chats ids to ignore.
|
|
76
81
|
*/
|
|
77
82
|
notIn(chatIds: number[]) {
|
|
78
|
-
this.
|
|
83
|
+
this.blacklistProvider = () => chatIds;
|
|
79
84
|
|
|
80
85
|
return this;
|
|
81
86
|
}
|
|
@@ -110,7 +115,7 @@ export class CommandActionBuilderWithState<TActionState extends IActionState> {
|
|
|
110
115
|
|
|
111
116
|
/** If called during building, action is marked as disabled and never checked. */
|
|
112
117
|
disabled() {
|
|
113
|
-
this.
|
|
118
|
+
this.activeProvider = () => false;
|
|
114
119
|
|
|
115
120
|
return this;
|
|
116
121
|
}
|
|
@@ -126,9 +131,40 @@ export class CommandActionBuilderWithState<TActionState extends IActionState> {
|
|
|
126
131
|
/** Sets action cooldown settings.
|
|
127
132
|
* @param cooldownSettings Settings.
|
|
128
133
|
*/
|
|
129
|
-
withCooldown(cooldownSettings: {
|
|
130
|
-
|
|
131
|
-
|
|
134
|
+
withCooldown(cooldownSettings: { cooldown: Seconds; message?: string }) {
|
|
135
|
+
const settings = new CooldownInfo(
|
|
136
|
+
cooldownSettings.cooldown,
|
|
137
|
+
cooldownSettings.message
|
|
138
|
+
);
|
|
139
|
+
this.cooldownSettingsProvider = () => settings;
|
|
140
|
+
|
|
141
|
+
return this;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Configures action to use property value providers instead of static value to allow changes in runtime
|
|
146
|
+
*/
|
|
147
|
+
withConfiguration(configuration: {
|
|
148
|
+
cooldownProvider?: CommandActionPropertyProvider<CooldownInfo>;
|
|
149
|
+
isActiveProvider?: CommandActionPropertyProvider<boolean>;
|
|
150
|
+
chatsBlacklistProvider?: CommandActionPropertyProvider<number[]>;
|
|
151
|
+
chatsWhitelistProvider?: CommandActionPropertyProvider<number[]>;
|
|
152
|
+
usersWhitelistProvider?: CommandActionPropertyProvider<number[]>;
|
|
153
|
+
}) {
|
|
154
|
+
if (configuration.cooldownProvider)
|
|
155
|
+
this.cooldownSettingsProvider = configuration.cooldownProvider;
|
|
156
|
+
|
|
157
|
+
if (configuration.chatsWhitelistProvider)
|
|
158
|
+
this.whitelistProvider = configuration.chatsWhitelistProvider;
|
|
159
|
+
|
|
160
|
+
if (configuration.chatsBlacklistProvider)
|
|
161
|
+
this.blacklistProvider = configuration.chatsBlacklistProvider;
|
|
162
|
+
|
|
163
|
+
if (configuration.usersWhitelistProvider)
|
|
164
|
+
this.allowedUsersProvider = configuration.usersWhitelistProvider;
|
|
165
|
+
|
|
166
|
+
if (configuration.isActiveProvider)
|
|
167
|
+
this.activeProvider = configuration.isActiveProvider;
|
|
132
168
|
|
|
133
169
|
return this;
|
|
134
170
|
}
|
|
@@ -139,13 +175,13 @@ export class CommandActionBuilderWithState<TActionState extends IActionState> {
|
|
|
139
175
|
this.trigger,
|
|
140
176
|
this.handler,
|
|
141
177
|
this.name,
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
this.
|
|
146
|
-
this.
|
|
147
|
-
this.
|
|
148
|
-
|
|
178
|
+
{
|
|
179
|
+
cooldownProvider: this.cooldownSettingsProvider,
|
|
180
|
+
isActiveProvider: this.activeProvider,
|
|
181
|
+
chatsBlacklistProvider: this.blacklistProvider,
|
|
182
|
+
chatsWhitelistProvider: this.whitelistProvider,
|
|
183
|
+
usersWhitelistProvider: this.allowedUsersProvider
|
|
184
|
+
},
|
|
149
185
|
this.maxAllowedSimultaniousExecutions,
|
|
150
186
|
this.condition,
|
|
151
187
|
this.stateConstructor,
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { InlineQueryHandler } from '../../types/handlers';
|
|
2
2
|
import { Noop } from '../noop';
|
|
3
3
|
import { InlineQueryAction } from '../../entities/actions/inlineQueryAction';
|
|
4
|
+
import { InlineActionPropertyProvider } from '../../types/propertyProvider';
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Builder for `InlineQueryAction`
|
|
@@ -9,7 +10,7 @@ export class InlineQueryActionBuilder {
|
|
|
9
10
|
private readonly name: string;
|
|
10
11
|
private pattern: RegExp = /.+/gi;
|
|
11
12
|
|
|
12
|
-
private
|
|
13
|
+
private activeProvider: InlineActionPropertyProvider<boolean> = () => true;
|
|
13
14
|
private handler: InlineQueryHandler = Noop.call;
|
|
14
15
|
|
|
15
16
|
/**
|
|
@@ -41,7 +42,19 @@ export class InlineQueryActionBuilder {
|
|
|
41
42
|
|
|
42
43
|
/** If called during building, action is marked as disabled and never checked. */
|
|
43
44
|
disabled() {
|
|
44
|
-
this.
|
|
45
|
+
this.activeProvider = () => false;
|
|
46
|
+
|
|
47
|
+
return this;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Configures action to use property value providers instead of static value to allow changes in runtime
|
|
52
|
+
*/
|
|
53
|
+
withConfiguration(configuration: {
|
|
54
|
+
isActiveProvider?: InlineActionPropertyProvider<boolean>;
|
|
55
|
+
}) {
|
|
56
|
+
if (configuration.isActiveProvider)
|
|
57
|
+
this.activeProvider = configuration.isActiveProvider;
|
|
45
58
|
|
|
46
59
|
return this;
|
|
47
60
|
}
|
|
@@ -51,7 +64,7 @@ export class InlineQueryActionBuilder {
|
|
|
51
64
|
return new InlineQueryAction(
|
|
52
65
|
this.handler,
|
|
53
66
|
this.name,
|
|
54
|
-
this.
|
|
67
|
+
this.activeProvider,
|
|
55
68
|
this.pattern
|
|
56
69
|
);
|
|
57
70
|
}
|