feat: 自动设置私聊指令

This commit is contained in:
凌莞 2022-02-22 16:24:43 +08:00
parent 1a76375d42
commit 6eb67006a3
No known key found for this signature in database
GPG Key ID: 05F8479BA63A8E92
4 changed files with 56 additions and 20 deletions

View File

@ -5,4 +5,20 @@ export default {
command: 'setup',
description: '执行初始化配置',
})],
personalPrivateCommands: [
new Api.BotCommand({
command: 'addfriend',
description: '添加新的好友转发',
}),
new Api.BotCommand({
command: 'addgroup',
description: '添加新的群转发',
}),
],
groupPrivateCommands: [
new Api.BotCommand({
command: 'add',
description: '添加新的群转发',
}),
],
};

View File

@ -13,7 +13,7 @@ export default class ConfigController {
private readonly oicq: OicqClient) {
this.configService = new ConfigService(tgBot, tgUser, oicq);
tgBot.addNewMessageEventHandler(this.handleMessage);
tgBot.setCommands([], new Api.BotCommandScopeUsers());
this.configService.configCommands();
}
private handleMessage = async (message: Api.Message) => {
@ -30,19 +30,29 @@ export default class ConfigController {
return false;
}
else if (message.isPrivate) {
switch (messageSplit[0]) {
case '/add':
// 加的参数永远是正的群号
if (messageSplit.length === 3 && regExps.qq.test(messageSplit[1]) && !isNaN(Number(messageSplit[2]))) {
await this.configService.createLinkGroup(-Number(messageSplit[1]), Number(messageSplit[2]));
}
else if (messageSplit[1] && regExps.qq.test(messageSplit[1])) {
await this.configService.addExact(Number(messageSplit[1]));
}
else {
await this.configService.add();
}
return true;
if (config.workMode === 'personal') {
switch (messageSplit[0]) {
case '/addfriend':
return true;
case '/addgroup':
return true;
}
}
else {
switch (messageSplit[0]) {
case '/add':
// 加的参数永远是正的群号
if (messageSplit.length === 3 && regExps.qq.test(messageSplit[1]) && !isNaN(Number(messageSplit[2]))) {
await this.configService.createLinkGroup(-Number(messageSplit[1]), Number(messageSplit[2]));
}
else if (messageSplit[1] && regExps.qq.test(messageSplit[1])) {
await this.configService.addExact(Number(messageSplit[1]));
}
else {
await this.configService.add();
}
return true;
}
}
}
};

View File

@ -7,7 +7,8 @@ import axios from 'axios';
import { getAvatarUrl } from '../utils/urls';
import { CustomFile } from 'telegram/client/uploads';
import db from '../providers/db';
import { Api } from 'telegram';
import { Api, utils } from 'telegram';
import commands from '../constants/commands';
export default class ConfigService {
private owner: TelegramChat;
@ -23,6 +24,17 @@ export default class ConfigService {
return `https://t.me/${this.tgBot.me.username}?startgroup=${roomId}`;
}
public async configCommands() {
// 这个在一初始化好就要调用,所以不能直接用 this.owner
await this.tgBot.setCommands([], new Api.BotCommandScopeUsers());
await this.tgBot.setCommands(
config.workMode === 'personal' ? commands.personalPrivateCommands : commands.groupPrivateCommands,
new Api.BotCommandScopePeer({
peer: utils.getInputPeer((await this.tgBot.getChat(config.owner)).entity),
}),
);
}
// 开始添加转发群组流程
public async add() {
const qGroups = Array.from(this.oicq.gl).map(e => e[1]);

View File

@ -1,3 +1,5 @@
import crypto from 'crypto';
const random = {
int(min: number, max: number) {
min = Math.ceil(min);
@ -5,11 +7,7 @@ const random = {
return Math.floor(Math.random() * (max - min + 1)) + min; //含最大值,含最小值
},
hex(length: number) {
let result = '';
for (let i = 0; i < length; i++) {
result += random.int(0, 15).toString(16);
}
return result;
return crypto.randomBytes(length / 2).toString('hex');
},
pick<T>(...array: T[]) {
const index = random.int(0, array.length - 1);