forked from Ishidres/discord-inactive-user-kick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
119 lines (92 loc) · 3.7 KB
/
index.js
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
const Discord = require('discord.js');
const client = new Discord.Client({ fetchAllMembers: true });
const fs = require("fs");
const config = require("./config.json");
const package = require("./package.json");
var saved_users = {};
// import savedUsers.json if it exists already
if (fs.existsSync(__dirname + "/savedUsers.json"))
saved_users = JSON.parse(fs.readFileSync("./savedUsers.json", "utf8"));
client.on('ready', () => {
console.log(new Date() + ": Logged in as " + client.user.tag);
if (client.guilds.array().length > 1) {
console.log("WARNING: The bot has been added to " + client.guilds.array().length + " servers but should only be used on one server at once and will now shutdown. Please remove the bot from all servers but one and restart it!");
process.exit();
}
client.user.setPresence({ activity: { name: "by Ishidres#5758" }, status: 'online' })
});
client.on('message', message => {
// Don't kick bots
if (message.author.bot) return;
// reply when receiving !help in direct message
if (message.channel.type === "dm") {
if (!!message.content) {
if (message.content.toLowerCase().startsWith("!help"))
message.reply("This bot doesn't offer any commands and has to be set up manually."
+ "\nPlease refer to " + package.homepage + " for more information.");
}
return;
}
if (!saved_users[message.author.id])
saved_users[message.author.id] = {};
saved_users[message.author.id].lastMessage = new Date().getTime();
saved_users[message.author.id].guild = message.guild.id;
console.log(new Date() + ": Message received by " + message.author.tag);
// Save the data
fs.writeFileSync("./savedUsers.json", JSON.stringify(saved_users));
});
// checking for users to kick every 10 minutes (600 000 ms)
setInterval(function () {
let now = new Date().getTime();
let actions = 0;
let all_users = client.users.array();
// Save all users
for (y=0; y<all_users.length; y++) {
if (all_users[y].id.length < 10)
continue;
// Save users even if they haven't messaged anything yet
if (!saved_users[all_users[y].id])
saved_users[all_users[y].id] = {
lastMessage: new Date().getTime(),
guild: client.guilds.array()[0].id
}
}
// Save users
fs.writeFileSync("./savedUsers.json", JSON.stringify(saved_users));
Object.keys(saved_users).map(function (i) {
// Prevent ratelimits
if (actions >= 5)
return;
let diff = now - saved_users[i].lastMessage;
let user = client.users.get(i);
let channel = client.channels.get(config.warnMessagesChannel);
if (!user) {
user = "UserIsOffline#0000";
} else {
user = user.tag;
}
if (diff >= config.warnDelay && saved_users[i].warned !== true) {
console.log(new Date() + ": User " + i + " will be kicked in " + config.warnDelay + " ms.");
if (!!channel)
channel.send(":warning: User " + user + " (ID: " + i + ") will be kicked in " + (config.warnDelay / 60 / 60 / 1000) + " minutes.");
saved_users[i].warned = true;
actions++;
}
if (diff >= config.maxInactivity) {
console.log(new Date() + ": Kicking User " + i + " due to inactivity.");
var guild = client.guilds.get(saved_users[i].guild);
// Don't really kick users if testing is enabled
if (config.testing !== true) {
guild.fetchMember(i).then(member => {
member.kick("Kicked due to inactivity.");
if (!!channel)
channel.send("🔨 " + member.user.tag + " has been kicked due to inactivity.");
});
}
actions++;
delete saved_users[i];
fs.writeFileSync("./savedUsers.json", JSON.stringify(saved_users));
}
});
}, 60*1000);
client.login(config.token);