Repository navigation
Expand file tree
/
Copy pathdiscord.js
More file actions
111 lines (93 loc) · 2.74 KB
/
Copy pathdiscord.js
File metadata and controls
111 lines (93 loc) · 2.74 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
const { Client, GatewayIntentBits } = require("discord.js");
const perspective = require('./perspective.js');
const { token, KICK_THRESHOLD } = require('./config.json');
const emojiMap = {
'FLIRTATION': '💋',
'TOXICITY': '🧨',
'INSULT': '👊',
'INCOHERENT': '🤪',
'SPAM': '🐟',
};
const User = {};
async function kick(user, guild) {
const member = await guild.members.fetch(user.id).catch(() => null);
if (!member) return;
try {
await member.kick('Was being a jerk');
} catch (err) {
console.log(`Could not kick user ${user.username}: ${err}`);
}
}
async function evaluateMessage(message) {
let scores;
try {
scores = await perspective.analyzeText(message.content);
} catch (err) {
console.log(err);
return false;
}
const userid = message.author.id;
if (!User[userid]) {
User[userid] = {};
}
for (const attribute in emojiMap) {
if (scores[attribute]) {
try {
await message.react(emojiMap[attribute]);
} catch (err) {
console.log(`Failed to react: ${err.message}`);
}
User[userid][attribute] =
User[userid][attribute] ? User[userid][attribute] + 1 : 1;
}
}
return (User[userid]['TOXICITY'] || 0) > KICK_THRESHOLD;
}
function getKarma() {
const scores = [];
for (const user in User) {
if (!Object.keys(User[user]).length) continue;
let score = `<@${user}> - `;
for (const attr in User[user]) {
score += `${emojiMap[attr]}: ${User[user][attr]}\t`;
}
scores.push(score);
}
return scores.length ? scores.join('\n') : '';
}
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers
]
});
client.on('ready', () => {
console.log('✅ I am ready!');
});
client.on('messageCreate', async (message) => {
console.log(message);
if (!message.guild || message.author.bot) return;
const userid = message.author.id;
if (!User[userid]) {
User[userid] = {};
}
if (message.content.startsWith('!karma')) {
const karma = getKarma();
await message.channel.send(karma || 'No karma yet!');
return;
}
let shouldKick = false;
try {
shouldKick = await evaluateMessage(message);
} catch (err) {
console.log(err);
}
if (shouldKick) {
await kick(message.author, message.guild);
delete User[userid];
await message.channel.send(`🚫 ${message.author.username} was kicked for repeated violations.`);
}
});
client.login(token);