catto.js 0.4.6 → 0.4.8
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/README.md +2 -1
- package/TelegramBot.js +113 -0
- package/TelegramChannel.js +43 -0
- package/TelegramMessage.js +26 -0
- package/User.js +1 -1
- package/index.js +1 -1
- package/package.json +6 -2
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|

|
|
5
5
|

|
|
6
6
|
|
|
7
|
-
✨ catto.js is a new version of universal catto package, which allows easy web server making, random generation and Discord
|
|
7
|
+
✨ catto.js is a new version of universal catto package, which allows easy web server making, random generation, Discord authorization and Discord/Telegram bots. ✨
|
|
8
8
|
## Features
|
|
9
9
|
- Creating web server.
|
|
10
10
|
- Adding SSL certificate and key to your web server.
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
- Authorizing users using Discord API OAuth2.
|
|
14
14
|
- Generating random numbers and booleans.
|
|
15
15
|
- Easy removing elements from Arrays.
|
|
16
|
+
- Creating Discord and Telegram bots using simple API.
|
|
16
17
|
|
|
17
18
|
## Installation
|
|
18
19
|
```sh
|
package/TelegramBot.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
var events = require("events");
|
|
2
|
+
var Telegram = require("node-telegram-bot-api");
|
|
3
|
+
var TelegramMessage = require("./TelegramMessage");
|
|
4
|
+
if (typeof EventEmitter === "undefined") {
|
|
5
|
+
var { EventEmitter } = events;
|
|
6
|
+
}
|
|
7
|
+
module.exports = class extends EventEmitter {
|
|
8
|
+
constructor(options, client) {
|
|
9
|
+
super();
|
|
10
|
+
this.options = Object.assign({
|
|
11
|
+
"speed": 300,
|
|
12
|
+
"token": "",
|
|
13
|
+
"debug": !1
|
|
14
|
+
}, options || {});
|
|
15
|
+
if (client) {
|
|
16
|
+
this.client = client;
|
|
17
|
+
} else {
|
|
18
|
+
this.client = new Telegram(this.options.token, {
|
|
19
|
+
"polling": {
|
|
20
|
+
"interval": this.options.speed,
|
|
21
|
+
"autoStart": !1
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
this.commands = new Map();
|
|
26
|
+
this.slashCommands = new Map();
|
|
27
|
+
this.menubtn = "commands";
|
|
28
|
+
if (this.options.debug) {
|
|
29
|
+
this.client.on("polling_error", console.log);
|
|
30
|
+
this.client.on("webhook_error", console.log);
|
|
31
|
+
this.client.on("error", console.log);
|
|
32
|
+
} else {
|
|
33
|
+
this.client.on("polling_error", () => {});
|
|
34
|
+
this.client.on("webhook_error", () => {});
|
|
35
|
+
}
|
|
36
|
+
this.client.on("message", message => {
|
|
37
|
+
message = new TelegramMessage(message, this);
|
|
38
|
+
var args = message.content.split(" ");
|
|
39
|
+
var cmd = args.shift();
|
|
40
|
+
var command = this.slashCommands.get(cmd) || this.commands.get(cmd);
|
|
41
|
+
if (command) {
|
|
42
|
+
try {
|
|
43
|
+
command.execute({
|
|
44
|
+
message,
|
|
45
|
+
command,
|
|
46
|
+
args,
|
|
47
|
+
"bot": this
|
|
48
|
+
});
|
|
49
|
+
} catch(e) {
|
|
50
|
+
console.log(e);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
this.emit("message", message);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
command(basic, executor) {
|
|
57
|
+
if (typeof basic === "string") {
|
|
58
|
+
basic = {
|
|
59
|
+
"name": basic
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
this.commands.set(basic.name, Object.assign(basic, {
|
|
63
|
+
"execute": executor
|
|
64
|
+
}));
|
|
65
|
+
return this;
|
|
66
|
+
}
|
|
67
|
+
slashCommand(basic, executor) {
|
|
68
|
+
if (!basic.name.startsWith("/")) {
|
|
69
|
+
throw new Error("Slash command starts with /.");
|
|
70
|
+
}
|
|
71
|
+
this.slashCommands.set(basic.name, Object.assign(basic, {
|
|
72
|
+
"execute": executor
|
|
73
|
+
}));
|
|
74
|
+
return this;
|
|
75
|
+
}
|
|
76
|
+
menuButton(target, label, link) {
|
|
77
|
+
this.menubtn = { target, label, link };
|
|
78
|
+
return this;
|
|
79
|
+
}
|
|
80
|
+
async run() {
|
|
81
|
+
await this.client.startPolling();
|
|
82
|
+
var commands = [];
|
|
83
|
+
for (var cmd of this.slashCommands.values()) {
|
|
84
|
+
commands.push({
|
|
85
|
+
"command": cmd.name.replace("/", ""),
|
|
86
|
+
"description": cmd.description
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
this.client.setMyCommands(commands);
|
|
90
|
+
if (this.menubtn.target == "web") {
|
|
91
|
+
this.client.setChatMenuButton({
|
|
92
|
+
"menu_button": JSON.stringify({
|
|
93
|
+
"type": "web_app",
|
|
94
|
+
"text": this.menubtn.label,
|
|
95
|
+
"web_app": {
|
|
96
|
+
"url": this.menubtn.link
|
|
97
|
+
}
|
|
98
|
+
})
|
|
99
|
+
});
|
|
100
|
+
} else {
|
|
101
|
+
this.client.setChatMenuButton({
|
|
102
|
+
"menu_button": JSON.stringify({
|
|
103
|
+
"type": "commands"
|
|
104
|
+
})
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
return this;
|
|
108
|
+
}
|
|
109
|
+
async stop() {
|
|
110
|
+
await this.client.stopPolling();
|
|
111
|
+
return this;
|
|
112
|
+
}
|
|
113
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
module.exports = class {
|
|
2
|
+
constructor(data, bot) {
|
|
3
|
+
this.data = data;
|
|
4
|
+
this.bot = bot;
|
|
5
|
+
this.typingLoop = null;
|
|
6
|
+
}
|
|
7
|
+
get id() {
|
|
8
|
+
return this.data.id;
|
|
9
|
+
}
|
|
10
|
+
async type() {
|
|
11
|
+
await this.bot.client.sendChatAction(this.id, "typing");
|
|
12
|
+
}
|
|
13
|
+
async startTyping() {
|
|
14
|
+
if (this.typingLoop === null) {
|
|
15
|
+
await this.bot.client.sendChatAction(this.id, "typing");
|
|
16
|
+
this.typingLoop = setInterval(() => this.type(), 4e3);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
stopTyping() {
|
|
20
|
+
clearInterval(this.typingLoop);
|
|
21
|
+
this.typingLoop = null;
|
|
22
|
+
}
|
|
23
|
+
async send(data) {
|
|
24
|
+
if (typeof data !== "object") {
|
|
25
|
+
data = {
|
|
26
|
+
"content": data
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
await this.bot.client.sendMessage(this.id, data.content, {
|
|
30
|
+
"reply_parameters": data.replyParameters ? {
|
|
31
|
+
"message_id": data.replyParameters.message.id,
|
|
32
|
+
"chat_id": (data.replyParameters.channel ? data.replyParameters.channel.id : this.id),
|
|
33
|
+
"allow_sending_without_reply": !1
|
|
34
|
+
} : void 0,
|
|
35
|
+
"reply_markup": data.replyMarkup ? data.replyMarkup : {
|
|
36
|
+
"remove_keyboard": !0
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
if (this.typingLoop !== null) {
|
|
40
|
+
await this.type();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
var TelegramChannel = require("./TelegramChannel");
|
|
2
|
+
module.exports = class {
|
|
3
|
+
constructor(data, bot) {
|
|
4
|
+
this.data = data;
|
|
5
|
+
this.bot = bot;
|
|
6
|
+
this.channel = new TelegramChannel(this.data.chat, bot);
|
|
7
|
+
}
|
|
8
|
+
get id() {
|
|
9
|
+
return this.data.message_id;
|
|
10
|
+
}
|
|
11
|
+
get content() {
|
|
12
|
+
return this.data.text;
|
|
13
|
+
}
|
|
14
|
+
reply(data) {
|
|
15
|
+
if (typeof data !== "object") {
|
|
16
|
+
data = {
|
|
17
|
+
"content": data
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
return this.channel.send(Object.assign({
|
|
21
|
+
"replyParameters": {
|
|
22
|
+
"message": this
|
|
23
|
+
}
|
|
24
|
+
}, data));
|
|
25
|
+
}
|
|
26
|
+
};
|
package/User.js
CHANGED
|
@@ -82,7 +82,7 @@ module.exports = class {
|
|
|
82
82
|
}
|
|
83
83
|
get badges() {
|
|
84
84
|
var i = 23;
|
|
85
|
-
var p = (this.options.flags ? (this.options.flags.bitfield ? this.options.flags.bitfield : this.options.flags) : (this.options.
|
|
85
|
+
var p = (this.options.flags ? (this.options.flags.bitfield ? this.options.flags.bitfield : this.options.flags) : (this.options.publicFlags.bitfield ? this.options.publicFlags.bitfield : this.options.publicFlags));
|
|
86
86
|
var f = [];
|
|
87
87
|
while (--i > -1) {
|
|
88
88
|
if (![21, 20, 15, 13, 12, 11, 5, 4].includes(i) && p >= (1 << i)) {
|
package/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
var mod = {};
|
|
2
|
-
["random", "Server", "HTML", "request", "AuthClient", "utils", "GitHub", "Base64", "User", "Bitfield", "Flags", "Bot", "Application", "MessageBuilder"].forEach(part => {
|
|
2
|
+
["random", "Server", "HTML", "request", "AuthClient", "utils", "GitHub", "Base64", "User", "Bitfield", "Flags", "Bot", "Application", "MessageBuilder", "TelegramBot", "TelegramMessage", "TelegramChannel"].forEach(part => {
|
|
3
3
|
mod[part] = require(`./${part}`);
|
|
4
4
|
});
|
|
5
5
|
module.exports = mod;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "catto.js",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.8",
|
|
4
4
|
"description": "Universal module for everything.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -27,7 +27,10 @@
|
|
|
27
27
|
"cattoes",
|
|
28
28
|
"easy",
|
|
29
29
|
"slash",
|
|
30
|
-
"random"
|
|
30
|
+
"random",
|
|
31
|
+
"telegram",
|
|
32
|
+
"api",
|
|
33
|
+
"telegraf"
|
|
31
34
|
],
|
|
32
35
|
"author": "cat1234",
|
|
33
36
|
"license": "ISC",
|
|
@@ -45,6 +48,7 @@
|
|
|
45
48
|
"express": "^4.18.2",
|
|
46
49
|
"express-session": "^1.18.0",
|
|
47
50
|
"express-ws": "^5.0.2",
|
|
51
|
+
"node-telegram-bot-api": "^0.65.1",
|
|
48
52
|
"request": "^2.88.2",
|
|
49
53
|
"session-file-store": "^1.5.0",
|
|
50
54
|
"tweetnacl": "^1.0.3"
|