Skip to content

Commit

Permalink
waife graph!
Browse files Browse the repository at this point in the history
  • Loading branch information
Lhcfl committed Sep 3, 2023
1 parent 65f1e0d commit 7a953ba
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 19 deletions.
49 changes: 30 additions & 19 deletions src/plugins/set_title/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,34 @@ import { PluginInit } from '@/types/plugin.js';
import { Message } from 'node-telegram-bot-api';

async function setTitle(app: App, msg: Message, title: string = '') {
if (title.length <= 0 || title.length >= 16) {
app.bot.sendMessage(msg.chat.id, '你想要的头衔太长/太短了哦', {
reply_to_message_id: msg.message_id
});
return;
}
if (!(['group', 'supergroup'].includes(msg.chat.type))) {
app.bot.sendMessage(msg.chat.id, '需要在群里才能设置头衔哦', {
reply_to_message_id: msg.message_id
});
return;
}
if (msg.from?.id) {
try {
await app.bot.promoteChatMember(msg.chat.id, msg.from?.id, {
can_pin_messages: true,
if (title.length >= 16) {
app.bot.sendMessage(msg.chat.id, '你想要的头衔太长了哦', {
reply_to_message_id: msg.message_id
});
await app.bot.setChatAdministratorCustomTitle(msg.chat.id, msg.from?.id, title);
app.bot.sendMessage(msg.chat.id, `设置成功,你现在是${title}了`, {
return;
}
if (!(['group', 'supergroup'].includes(msg.chat.type))) {
app.bot.sendMessage(msg.chat.id, '需要在群里才能设置头衔哦', {
reply_to_message_id: msg.message_id
});
return;
}
try {
if (title.length <= 0) {
await app.bot.promoteChatMember(msg.chat.id, msg.from?.id);
app.bot.sendMessage(msg.chat.id, '清除头衔成功', {
reply_to_message_id: msg.message_id
});
} else {
await app.bot.promoteChatMember(msg.chat.id, msg.from?.id, {
can_pin_messages: true,
});
await app.bot.setChatAdministratorCustomTitle(msg.chat.id, msg.from?.id, title);
app.bot.sendMessage(msg.chat.id, `设置成功,你现在是${title}了`, {
reply_to_message_id: msg.message_id
});
}
} catch (err) {
const errobj = JSON.parse(JSON.stringify(err));
console.log(JSON.stringify(err));
Expand All @@ -33,7 +40,11 @@ async function setTitle(app: App, msg: Message, title: string = '') {
});
return;
}
if (errobj.message.includes('not enough rights') || errobj.message.includes('user is not an administrator')) {
if (
errobj.message.includes('not enough rights') ||
errobj.message.includes('user is not an administrator') ||
errobj.message.includes('CHAT_ADMIN_REQUIRED')
) {
app.bot.sendMessage(msg.chat.id, `${app.config.bot_name}还没这个权限哦`, {
reply_to_message_id: msg.message_id
});
Expand All @@ -54,7 +65,7 @@ const init: PluginInit = (app) => {
chat_type: ['group', 'supergroup'],
command: 't',
handle: setTitle,
description: '设置头衔!'
description: '/t [头衔] 设置头衔!'
});
};

Expand Down
163 changes: 163 additions & 0 deletions src/plugins/waife/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import { App } from '@/types/app.js';
import { PluginInit } from '@/types/plugin.js';
import { Message, User } from 'node-telegram-bot-api';

function getName(user: User): string {
let username:string = user.first_name ?
user.first_name : (user.username ? user.username : '');
if (user.last_name) {
username += ' ' + user.last_name;
}
return username;
}

function htmlify(str: string | undefined): string {
if (!str) {str = '';}
str = str.replaceAll('&', '&amp;');
str = str.replaceAll('<', '&lt;');
str = str.replaceAll('>', '&gt;');
str = str.replaceAll('"', '&quot;');
return str;
}


function initialize_waifes(app: App, msg: Message) {
if (!app.db.chat(msg.chat.id).waifes) {
app.db.chat(msg.chat.id).waifes = [];
app.db.chat(msg.chat.id).waife_ids = {};
}
if (!app.db.chat(msg.chat.id).waifemap) {
app.db.chat(msg.chat.id).waifemap = {};
}
if (!app.db.chat(msg.chat.id).iduserMap) {
app.db.chat(msg.chat.id).iduserMap = {};
}
if (!app.db.chat(msg.chat.id).lastwaifedate) {
app.db.chat(msg.chat.id).lastwaifedate = (new Date).toDateString();
}
}

async function add_to_wife(app: App, msg: Message, uid?: number) {
initialize_waifes(app, msg);
if (!uid) { return; }
if (app.db.chat(msg.chat.id).waife_ids[uid]) {
return;
}
let you;
if (msg.from?.id !== uid) {
you = (await app.bot.getChatMember(msg.chat.id, uid)).user;
} else {
you = msg.from;
}
app.db.chat(msg.chat.id).waife_ids[uid] = true;
app.db.chat(msg.chat.id).waifes.push(you);
app.db.chat(msg.chat.id).iduserMap[uid] = you;
}

async function getWaifesList(app: App, msg: Message) {
initialize_waifes(app, msg);
if (app.db.chat(msg.chat.id).waifes.length === 0) {
const defaultWaifes = await app.bot.getChatAdministrators(msg.chat.id);
for (const waife of defaultWaifes) {
app.db.chat(msg.chat.id).waifes.push(waife.user);
app.db.chat(msg.chat.id).waife_ids[waife.user.id];
app.db.chat(msg.chat.id).iduserMap[waife.user.id] = waife;
}
await add_to_wife(app, msg, msg.from?.id);
}
return app.db.chat(msg.chat.id).waifes;
}

async function getWaife(app: App, msg: Message) {
if (!msg.from?.id) { return; }
if ((new Date).toDateString() !== app.db.chat(msg.chat.id).lastwaifedate) {
app.db.chat(msg.chat.id).waifemap = {};
app.db.chat(msg.chat.id).lastwaifedate = (new Date).toDateString();
}
if (app.db.chat(msg.chat.id).waifemap[msg.from?.id]) {
app.bot.sendMessage(msg.chat.id, '你今天已经抽过老婆了哦!', {
reply_to_message_id: msg.message_id,
});
return;
}
const lst = await getWaifesList(app, msg);
if (lst.length <= 1) {
app.bot.sendMessage(msg.chat.id, '抱歉,我还没从群里获取到足够多的人哦,请先回复我一些消息', {
reply_to_message_id: msg.message_id,
});
return;
}
let waife: User = lst[Math.floor(Math.random() * lst.length)];
let cnt = 0;
while (waife.id === msg.from?.id && cnt < 20) {
waife = lst[Math.floor(Math.random() * lst.length)];
cnt++;
}
if (waife.id === msg.from?.id) {
app.bot.sendMessage(msg.chat.id, '获取失败……理论上不会出现这个情况, 请查看bot代码', {
reply_to_message_id: msg.message_id,
});
return;
}
add_to_wife(app, msg, msg.from.id);
const res = await app.bot.sendMessage(
msg.chat.id,
`${htmlify('获取成功~ 你今天的群友老婆是')} <a href="tg://user?id=${waife.id}">${htmlify(getName(waife))}</a>`,
{
parse_mode: 'HTML',
reply_to_message_id: msg.message_id,
}
);
app.db.chat(msg.chat.id).waifemap[msg.from?.id] = {
msg_id: res.message_id,
date: res.date,
waife,
};
}

async function getWaifeGraph(app: App, msg: Message) {
initialize_waifes(app, msg);
if (!msg.from?.id) { return; }
const wfMap = app.db.chat(msg.chat.id).waifemap;
const iduM = app.db.chat(msg.chat.id).iduserMap;
let txt = '```mermaid\n';
txt += '\ngraph TD';
for (const id in wfMap) {
if (wfMap[id]?.waife) {
txt += `\n ${id}["${getName(iduM[id])}"] --> ${wfMap[id]?.waife?.id}["${getName(wfMap[id].waife)}"]`;
}
}
txt += '\n```';
app.bot.sendMessage(
msg.chat.id,
`老婆图(mermaid):\n<pre>${htmlify(txt)}</pre>`,
{
parse_mode: 'HTML',
reply_to_message_id: msg.message_id,
}
);
}

const init: PluginInit = (app) => {
app.registCommand({
chat_type: ['group', 'supergroup'],
command: 'waife',
handle: getWaife,
description: '获取老婆!'
});
app.registCommand({
chat_type: ['group', 'supergroup'],
command: 'waife_graph',
handle: getWaifeGraph,
description: '获取老婆关系图!'
});
app.registReplyHandle({
chat_type: ['group', 'supergroup'],
handle: (appl, msg) => {
add_to_wife(appl, msg, msg.from?.id);
},
description: '添加老婆!'
});
};

export { init };

0 comments on commit 7a953ba

Please sign in to comment.