mirror of
https://github.com/thedevs-network/the-guard-bot
synced 2025-08-29 13:17:56 +00:00
New handlers: regex/groupLinker, unmatched
This commit is contained in:
parent
38e1559804
commit
83a61353e5
27
handlers/regex/groupLinker.js
Normal file
27
handlers/regex/groupLinker.js
Normal file
@ -0,0 +1,27 @@
|
||||
'use strict';
|
||||
|
||||
const { hears } = require('telegraf');
|
||||
const XRegExp = require('xregexp');
|
||||
|
||||
const { managesGroup } = require.main.require('./stores/group');
|
||||
|
||||
const regex = /^(?:Link to\s+)?(.+) group(?: link)?\s*\?*$/i;
|
||||
|
||||
const handler = async (ctx, next) => {
|
||||
let [ , groupName ] = ctx.match;
|
||||
if (groupName.toLowerCase() === 'this') {
|
||||
if (!ctx.chat.title) return next();
|
||||
groupName = ctx.chat.title;
|
||||
}
|
||||
|
||||
const $regex = XRegExp.tag('i')`${groupName}`;
|
||||
|
||||
const group = await managesGroup({ title: { $regex } });
|
||||
const { link } = group || {};
|
||||
|
||||
if (!link) return next();
|
||||
|
||||
return ctx.reply(link, { reply_to_message_id: ctx.message.message_id });
|
||||
};
|
||||
|
||||
module.exports = hears(regex, handler);
|
9
handlers/regex/index.js
Normal file
9
handlers/regex/index.js
Normal file
@ -0,0 +1,9 @@
|
||||
'use strict';
|
||||
|
||||
const { compose } = require('telegraf');
|
||||
|
||||
/* eslint-disable global-require */
|
||||
|
||||
module.exports = compose([
|
||||
require('./groupLinker'),
|
||||
]);
|
9
handlers/unmatched.js
Normal file
9
handlers/unmatched.js
Normal file
@ -0,0 +1,9 @@
|
||||
'use strict';
|
||||
|
||||
const R = require('ramda');
|
||||
const Tf = require('telegraf');
|
||||
|
||||
module.exports = Tf.optional(
|
||||
R.pathEq([ 'chat', 'type' ], 'private'),
|
||||
Tf.reply('Sorry, I couldn\'t understand that, do you need /help?')
|
||||
);
|
3
index.js
3
index.js
@ -11,6 +11,7 @@ const bot = require('./bot');
|
||||
|
||||
bot.telegram.getMe().then((botInfo) => {
|
||||
bot.options.username = botInfo.username;
|
||||
bot.context.botInfo = botInfo;
|
||||
});
|
||||
|
||||
bot.use(
|
||||
@ -18,6 +19,8 @@ bot.use(
|
||||
require('./handlers/messages'),
|
||||
require('./plugins'),
|
||||
require('./handlers/commands'),
|
||||
require('./handlers/regex'),
|
||||
require('./handlers/unmatched'),
|
||||
);
|
||||
|
||||
|
||||
|
5
package-lock.json
generated
5
package-lock.json
generated
@ -1421,6 +1421,11 @@
|
||||
"mkdirp": "0.5.1"
|
||||
}
|
||||
},
|
||||
"xregexp": {
|
||||
"version": "4.0.0",
|
||||
"resolved": "https://registry.npmjs.org/xregexp/-/xregexp-4.0.0.tgz",
|
||||
"integrity": "sha512-PHyM+sQouu7xspQQwELlGwwd05mXUFqwFYfqPO0cC7x4fxyHnnuetmQr6CjJiafIDoH4MogHb9dOoJzR/Y4rFg=="
|
||||
},
|
||||
"xtend": {
|
||||
"version": "4.0.1",
|
||||
"resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.1.tgz",
|
||||
|
@ -32,7 +32,8 @@
|
||||
"millisecond": "^0.1.2",
|
||||
"nedb-promise": "^2.0.1",
|
||||
"ramda": "^0.25.0",
|
||||
"telegraf": "^3.15.3"
|
||||
"telegraf": "^3.15.3",
|
||||
"xregexp": "^4.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=8.1.3"
|
||||
|
Loading…
x
Reference in New Issue
Block a user