lint
This commit is contained in:
977 files changed
+198426
-158324
No files matched your search
@@ -2,54 +2,66 @@ import type { OneBotMessageEvent } from '../types/index';
|
||||
import type { CommandMatch, CommandOptions } from './types';
|
||||
|
||||
export function matchCommand(
|
||||
event: OneBotMessageEvent,
|
||||
command: string | RegExp,
|
||||
options: CommandOptions = {},
|
||||
event: OneBotMessageEvent,
|
||||
command: string | RegExp,
|
||||
options: CommandOptions = {},
|
||||
): CommandMatch | null {
|
||||
const trim = options.trim ?? true;
|
||||
const raw = trim ? event.raw_message.trim() : event.raw_message;
|
||||
const prefixes = normalizePrefixes(options.prefixes ?? '/');
|
||||
const trim = options.trim ?? true;
|
||||
const raw = trim ? event.raw_message.trim() : event.raw_message;
|
||||
const prefixes = normalizePrefixes(options.prefixes ?? '/');
|
||||
|
||||
for (const prefix of prefixes) {
|
||||
if (prefix && !raw.startsWith(prefix)) continue;
|
||||
for (const prefix of prefixes) {
|
||||
if (prefix && !raw.startsWith(prefix)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const text = prefix ? raw.slice(prefix.length).trimStart() : raw;
|
||||
if (!text) continue;
|
||||
const text = prefix ? raw.slice(prefix.length).trimStart() : raw;
|
||||
|
||||
if (command instanceof RegExp) {
|
||||
const match = text.match(command);
|
||||
if (!match || match.index !== 0) continue;
|
||||
const matchedText = match[0] ?? '';
|
||||
const rest = text.slice(matchedText.length).trim();
|
||||
return {
|
||||
command: matchedText,
|
||||
text,
|
||||
args: rest ? rest.split(/\s+/) : [],
|
||||
rest,
|
||||
prefix,
|
||||
match,
|
||||
};
|
||||
if (!text) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (command instanceof RegExp) {
|
||||
const match = text.match(command);
|
||||
|
||||
if (!match || match.index !== 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const matchedText = match[0] ?? '';
|
||||
const rest = text.slice(matchedText.length).trim();
|
||||
return {
|
||||
command: matchedText,
|
||||
text,
|
||||
args: rest ? rest.split(/\s+/) : [],
|
||||
rest,
|
||||
prefix,
|
||||
match,
|
||||
};
|
||||
}
|
||||
|
||||
const [head = '', ...args] = text.split(/\s+/);
|
||||
const matches = options.caseSensitive
|
||||
? head === command
|
||||
: head.toLowerCase() === command.toLowerCase();
|
||||
|
||||
if (!matches) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return {
|
||||
command: head,
|
||||
text,
|
||||
args,
|
||||
rest: text.slice(head.length).trim(),
|
||||
prefix,
|
||||
match: null,
|
||||
};
|
||||
}
|
||||
|
||||
const [head = '', ...args] = text.split(/\s+/);
|
||||
const matches = options.caseSensitive
|
||||
? head === command
|
||||
: head.toLowerCase() === command.toLowerCase();
|
||||
if (!matches) continue;
|
||||
|
||||
return {
|
||||
command: head,
|
||||
text,
|
||||
args,
|
||||
rest: text.slice(head.length).trim(),
|
||||
prefix,
|
||||
match: null,
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
return null;
|
||||
}
|
||||
|
||||
function normalizePrefixes(prefixes: string | string[]): string[] {
|
||||
return Array.isArray(prefixes) ? prefixes : [prefixes];
|
||||
return Array.isArray(prefixes) ? prefixes : [prefixes];
|
||||
}
|
||||
@@ -3,71 +3,82 @@ import type { SnowLumaEvent } from '../types/index';
|
||||
import { isGroupMessageEvent, isPrivateMessageEvent, isRequestEvent } from './guards';
|
||||
import type { SnowLumaEventContext } from './types';
|
||||
|
||||
|
||||
export function createEventContext<TEvent extends SnowLumaEvent>(
|
||||
event: TEvent,
|
||||
client: SnowLumaApiClient,
|
||||
event: TEvent,
|
||||
client: SnowLumaApiClient,
|
||||
): SnowLumaEventContext<TEvent> {
|
||||
let stopped = false;
|
||||
let stopped = false;
|
||||
|
||||
const context: SnowLumaEventContext<TEvent> = {
|
||||
event,
|
||||
client,
|
||||
get stopped() {
|
||||
return stopped;
|
||||
},
|
||||
stopPropagation() {
|
||||
stopped = true;
|
||||
},
|
||||
async reply(message, options) {
|
||||
if (isGroupMessageEvent(event)) {
|
||||
return client.sendGroupMessage(event.group_id, message, options);
|
||||
}
|
||||
if (isPrivateMessageEvent(event)) {
|
||||
return client.sendPrivateMessage(event.user_id, message, options);
|
||||
}
|
||||
throw new Error('reply() is only available for message events');
|
||||
},
|
||||
async approve(options) {
|
||||
if (!isRequestEvent(event)) {
|
||||
throw new Error('approve() is only available for request events');
|
||||
}
|
||||
if (event.request_type === 'friend') {
|
||||
return client.setFriendAddRequest(event.flag, true, options);
|
||||
}
|
||||
if (event.request_type === 'group') {
|
||||
return client.setGroupAddRequest(event.flag, {
|
||||
...options,
|
||||
subType: options?.subType ?? event.sub_type,
|
||||
approve: true,
|
||||
});
|
||||
}
|
||||
throw new Error(`approve() does not support request_type=${event.request_type}`);
|
||||
},
|
||||
async reject(reason, options) {
|
||||
if (!isRequestEvent(event)) {
|
||||
throw new Error('reject() is only available for request events');
|
||||
}
|
||||
if (event.request_type === 'friend') {
|
||||
return client.setFriendAddRequest(event.flag, false, options);
|
||||
}
|
||||
if (event.request_type === 'group') {
|
||||
return client.setGroupAddRequest(event.flag, {
|
||||
...options,
|
||||
subType: options?.subType ?? event.sub_type,
|
||||
approve: false,
|
||||
reason: reason ?? options?.reason,
|
||||
});
|
||||
}
|
||||
throw new Error(`reject() does not support request_type=${event.request_type}`);
|
||||
},
|
||||
async quickOperation(operation, options) {
|
||||
return client.raw('.handle_quick_operation', {
|
||||
context: event,
|
||||
operation,
|
||||
}, options);
|
||||
},
|
||||
};
|
||||
const context: SnowLumaEventContext<TEvent> = {
|
||||
event,
|
||||
client,
|
||||
get stopped() {
|
||||
return stopped;
|
||||
},
|
||||
stopPropagation() {
|
||||
stopped = true;
|
||||
},
|
||||
async reply(message, options) {
|
||||
if (isGroupMessageEvent(event)) {
|
||||
return client.sendGroupMessage(event.group_id, message, options);
|
||||
}
|
||||
|
||||
return context;
|
||||
if (isPrivateMessageEvent(event)) {
|
||||
return client.sendPrivateMessage(event.user_id, message, options);
|
||||
}
|
||||
|
||||
throw new Error('reply() is only available for message events');
|
||||
},
|
||||
async approve(options) {
|
||||
if (!isRequestEvent(event)) {
|
||||
throw new Error('approve() is only available for request events');
|
||||
}
|
||||
|
||||
if (event.request_type === 'friend') {
|
||||
return client.setFriendAddRequest(event.flag, true, options);
|
||||
}
|
||||
|
||||
if (event.request_type === 'group') {
|
||||
return client.setGroupAddRequest(event.flag, {
|
||||
...options,
|
||||
subType: options?.subType ?? event.sub_type,
|
||||
approve: true,
|
||||
});
|
||||
}
|
||||
|
||||
throw new Error(`approve() does not support request_type=${event.request_type}`);
|
||||
},
|
||||
async reject(reason, options) {
|
||||
if (!isRequestEvent(event)) {
|
||||
throw new Error('reject() is only available for request events');
|
||||
}
|
||||
|
||||
if (event.request_type === 'friend') {
|
||||
return client.setFriendAddRequest(event.flag, false, options);
|
||||
}
|
||||
|
||||
if (event.request_type === 'group') {
|
||||
return client.setGroupAddRequest(event.flag, {
|
||||
...options,
|
||||
subType: options?.subType ?? event.sub_type,
|
||||
approve: false,
|
||||
reason: reason ?? options?.reason,
|
||||
});
|
||||
}
|
||||
|
||||
throw new Error(`reject() does not support request_type=${event.request_type}`);
|
||||
},
|
||||
async quickOperation(operation, options) {
|
||||
return client.raw(
|
||||
'.handle_quick_operation',
|
||||
{
|
||||
context: event,
|
||||
operation,
|
||||
},
|
||||
options,
|
||||
);
|
||||
},
|
||||
};
|
||||
|
||||
return context;
|
||||
}
|
||||
@@ -1,51 +1,61 @@
|
||||
import type {
|
||||
OneBotBotStatusEvent,
|
||||
OneBotGroupMessageEvent,
|
||||
OneBotMessageEvent,
|
||||
OneBotMetaEvent,
|
||||
OneBotNoticeEvent,
|
||||
OneBotPrivateMessageEvent,
|
||||
OneBotRequestEvent,
|
||||
SnowLumaEvent,
|
||||
OneBotBotStatusEvent,
|
||||
OneBotGroupMessageEvent,
|
||||
OneBotMessageEvent,
|
||||
OneBotMetaEvent,
|
||||
OneBotNoticeEvent,
|
||||
OneBotPrivateMessageEvent,
|
||||
OneBotRequestEvent,
|
||||
SnowLumaEvent,
|
||||
} from '../types/index';
|
||||
import type { EventPredicate } from './types';
|
||||
|
||||
export function isMessageEvent(event: SnowLumaEvent): event is OneBotMessageEvent {
|
||||
return (event.post_type === 'message' || event.post_type === 'message_sent')
|
||||
&& (event.message_type === 'private' || event.message_type === 'group');
|
||||
return (
|
||||
(event.post_type === 'message' || event.post_type === 'message_sent') &&
|
||||
(event.message_type === 'private' || event.message_type === 'group')
|
||||
);
|
||||
}
|
||||
|
||||
export function isPrivateMessageEvent(event: SnowLumaEvent): event is OneBotPrivateMessageEvent {
|
||||
return isMessageEvent(event) && event.message_type === 'private';
|
||||
return isMessageEvent(event) && event.message_type === 'private';
|
||||
}
|
||||
|
||||
export function isGroupMessageEvent(event: SnowLumaEvent): event is OneBotGroupMessageEvent {
|
||||
return isMessageEvent(event) && event.message_type === 'group';
|
||||
return isMessageEvent(event) && event.message_type === 'group';
|
||||
}
|
||||
|
||||
export function isNoticeEvent(event: SnowLumaEvent): event is OneBotNoticeEvent {
|
||||
return event.post_type === 'notice' && typeof event.notice_type === 'string';
|
||||
return event.post_type === 'notice' && typeof event.notice_type === 'string';
|
||||
}
|
||||
|
||||
export function isBotStatusEvent(event: SnowLumaEvent): event is OneBotBotStatusEvent {
|
||||
return isNoticeEvent(event)
|
||||
&& event.notice_type === 'bot_status'
|
||||
&& (event.sub_type === 'online' || event.sub_type === 'offline')
|
||||
&& typeof event.user_id === 'number';
|
||||
return (
|
||||
isNoticeEvent(event) &&
|
||||
event.notice_type === 'bot_status' &&
|
||||
(event.sub_type === 'online' || event.sub_type === 'offline') &&
|
||||
typeof event.user_id === 'number'
|
||||
);
|
||||
}
|
||||
|
||||
export function isRequestEvent(event: SnowLumaEvent): event is OneBotRequestEvent {
|
||||
return event.post_type === 'request' && typeof event.request_type === 'string' && typeof event.flag === 'string';
|
||||
return (
|
||||
event.post_type === 'request' &&
|
||||
typeof event.request_type === 'string' &&
|
||||
typeof event.flag === 'string'
|
||||
);
|
||||
}
|
||||
|
||||
export function isMetaEvent(event: SnowLumaEvent): event is OneBotMetaEvent {
|
||||
return event.post_type === 'meta_event' && typeof event.meta_event_type === 'string';
|
||||
return event.post_type === 'meta_event' && typeof event.meta_event_type === 'string';
|
||||
}
|
||||
|
||||
export function noticeType(type: string): EventPredicate<OneBotNoticeEvent> {
|
||||
return (event): event is OneBotNoticeEvent => isNoticeEvent(event) && event.notice_type === type;
|
||||
return (event): event is OneBotNoticeEvent =>
|
||||
isNoticeEvent(event) && event.notice_type === type;
|
||||
}
|
||||
|
||||
export function requestType(type: string): EventPredicate<OneBotRequestEvent> {
|
||||
return (event): event is OneBotRequestEvent => isRequestEvent(event) && event.request_type === type;
|
||||
return (event): event is OneBotRequestEvent =>
|
||||
isRequestEvent(event) && event.request_type === type;
|
||||
}
|
||||
@@ -1,66 +1,66 @@
|
||||
import type { SnowLumaApiClient } from '../client/api-client';
|
||||
import type {
|
||||
JsonObject,
|
||||
OneBotMessageEvent,
|
||||
OutgoingMessage,
|
||||
RequestOptions,
|
||||
SnowLumaEvent,
|
||||
JsonObject,
|
||||
OneBotMessageEvent,
|
||||
OutgoingMessage,
|
||||
RequestOptions,
|
||||
SnowLumaEvent,
|
||||
} from '../types/index';
|
||||
|
||||
export type MaybePromise<T = void> = T | Promise<T>;
|
||||
export type EventNext = () => Promise<void>;
|
||||
|
||||
|
||||
export type EventPredicate<TEvent extends SnowLumaEvent = SnowLumaEvent> = (event: SnowLumaEvent) => event is TEvent;
|
||||
|
||||
export type EventPredicate<TEvent extends SnowLumaEvent = SnowLumaEvent> = (
|
||||
event: SnowLumaEvent,
|
||||
) => event is TEvent;
|
||||
|
||||
export type EventHandler<TEvent extends SnowLumaEvent = SnowLumaEvent> = (
|
||||
event: TEvent,
|
||||
context: SnowLumaEventContext<TEvent>,
|
||||
event: TEvent,
|
||||
context: SnowLumaEventContext<TEvent>,
|
||||
) => MaybePromise;
|
||||
|
||||
|
||||
export type EventMiddleware = (
|
||||
event: SnowLumaEvent,
|
||||
context: SnowLumaEventContext,
|
||||
next: EventNext,
|
||||
event: SnowLumaEvent,
|
||||
context: SnowLumaEventContext,
|
||||
next: EventNext,
|
||||
) => MaybePromise;
|
||||
|
||||
|
||||
export interface RequestDecisionOptions extends RequestOptions {
|
||||
reason?: string;
|
||||
subType?: string;
|
||||
reason?: string;
|
||||
subType?: string;
|
||||
}
|
||||
|
||||
|
||||
export interface SnowLumaEventContext<TEvent extends SnowLumaEvent = SnowLumaEvent> {
|
||||
readonly event: TEvent;
|
||||
readonly client: SnowLumaApiClient;
|
||||
readonly stopped: boolean;
|
||||
stopPropagation(): void;
|
||||
reply(message: OutgoingMessage, options?: RequestOptions & { autoEscape?: boolean }): Promise<unknown>;
|
||||
approve(options?: RequestDecisionOptions): Promise<unknown>;
|
||||
reject(reason?: string, options?: RequestDecisionOptions): Promise<unknown>;
|
||||
quickOperation(operation: JsonObject, options?: RequestOptions): Promise<unknown>;
|
||||
readonly event: TEvent;
|
||||
readonly client: SnowLumaApiClient;
|
||||
readonly stopped: boolean;
|
||||
stopPropagation(): void;
|
||||
reply(
|
||||
message: OutgoingMessage,
|
||||
options?: RequestOptions & { autoEscape?: boolean },
|
||||
): Promise<unknown>;
|
||||
approve(options?: RequestDecisionOptions): Promise<unknown>;
|
||||
reject(reason?: string, options?: RequestDecisionOptions): Promise<unknown>;
|
||||
quickOperation(operation: JsonObject, options?: RequestOptions): Promise<unknown>;
|
||||
}
|
||||
|
||||
export interface CommandOptions {
|
||||
prefixes?: string | string[];
|
||||
trim?: boolean;
|
||||
caseSensitive?: boolean;
|
||||
prefixes?: string | string[];
|
||||
trim?: boolean;
|
||||
caseSensitive?: boolean;
|
||||
}
|
||||
|
||||
export interface CommandMatch {
|
||||
command: string;
|
||||
text: string;
|
||||
args: string[];
|
||||
rest: string;
|
||||
prefix: string;
|
||||
match: RegExpMatchArray | null;
|
||||
command: string;
|
||||
text: string;
|
||||
args: string[];
|
||||
rest: string;
|
||||
prefix: string;
|
||||
match: RegExpMatchArray | null;
|
||||
}
|
||||
|
||||
export type CommandHandler<TEvent extends OneBotMessageEvent = OneBotMessageEvent> = (
|
||||
event: TEvent,
|
||||
context: SnowLumaEventContext<TEvent> & { command: CommandMatch },
|
||||
match: CommandMatch,
|
||||
event: TEvent,
|
||||
context: SnowLumaEventContext<TEvent> & { command: CommandMatch },
|
||||
match: CommandMatch,
|
||||
) => MaybePromise;
|
||||
Reference in new issue
Block a user