-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
124 lines (93 loc) · 3.39 KB
/
index.js
File metadata and controls
124 lines (93 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const Bot = require("./bot");
const striptags = require('striptags');
const fs = require('fs');
const config = require('./config.json');
// Init
const client = new Bot(config, [{api_point: "public", event: "update", emit_on: "federated"},
{api_point: "user", event: "notification", emit_on: "mentions"},
{api_point: "user", event: "update", emit_on: "home"}]);
const admins = new Set(config.admins);
const following = new Set();
const commands = new Map();
const commandFiles = fs.readdirSync(__dirname + "/commands/");
for (const file of commandFiles) {
const commandClass = require(__dirname + "/commands/" + file);
const command = new commandClass(client);
commands.set(command.name, command)
}
// Start the bot
client.start().then(() => {
// Populate following list
client.following_list().then((result) => {
for (const account of result) following.add(account.acct)
console.log(`I'm currently following ${following.size} accounts.`)
});
console.log("Federation Bot started ! 🎉")
});
// When a toot arrives in Federated Timeline
client.on('federated', (msg) => {
follow_or_not_follow(msg);
});
// Listen reblogs on Home
client.on('home', (msg) => {
if (msg.reblog !== null) follow_or_not_follow(msg.reblog);
});
function follow_or_not_follow(msg) {
const acct = msg.account.acct;
const id = parseInt(msg.account.id);
const acct_parts = acct.split('@');
// Don't follow local accounts
if (acct_parts.length === 1) return;
// Don't follow locked accounts
if (msg.account.locked === true) return;
// Don't follow other bots
if (msg.account.bot === true) return;
// Respect #nobot
if (striptags(msg.account.note).match(/#nobot/i)) {
client.mute_user(id);
console.log("MUTED #nobot: " + acct);
// Check if the bot already followed the user
// A.K.A. Did the user add a #nobot tag after I follow them ?
if (following.has(acct)) {
client.unfollow(id);
console.log("UNFOLLOW: " + acct)
}
return;
}
// Already following. (This will be... not optimal I think.)
if (following.has(acct)) return;
following.add(acct);
client.follow(id);
console.log("NOW FOLLOWS: " + acct);
}
// When a toot mention the bot
client.on("mentions", (msg) => {
if (msg.type !== "mention") return;
const status = striptags(msg.status.content);
const full_acct = '@' + client.me.acct;
// Starting with @username@domain or @username (local toots)
if (!status.startsWith(full_acct) || !status.startsWith('@' + client.me.username)) return;
const args = status.slice(client.me.acct.length + 1).trim().split(/ +/);
const commandName = args.shift().toLowerCase();
// Check if command exists
if (!commands.has(commandName)) return;
const command = commands.get(commandName);
// Check if command is disabled
if (command.disabled === true) return;
// Check if command is AdminOnly
if (command.admin_only === true && !admins.has(msg.account.acct)) return;
//Check args length
if (command.required_args > args) return;
console.log(`CMD: ${commandName} from ${msg.account.acct}`);
// Execute the command
try {
command.execute(msg, args);
}
catch (error) {
console.error(error);
}
});
process.on('SIGINT', () => {
console.info("Exiting...");
process.exit();
});