-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.js
156 lines (124 loc) · 4.68 KB
/
bot.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const { Client, GatewayIntentBits } = require('discord.js');
const express = require('express');
const { WebSocketServer } = require('ws');
const dotenv = require('dotenv');
dotenv.config();
const bot = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});
const registeredServers = {};
const webSocketClients = {};
bot.once('ready', () => {
console.log(`Bot connecté en tant que ${bot.user.tag}`);
});
bot.on('messageCreate', (message) => {
if (message.author.bot) return;
const guildId = message.guild.id;
if (!registeredServers[guildId]) {
registeredServers[guildId] = {};
}
const serverData = registeredServers[guildId];
if (message.content.startsWith('!register')) {
const channelId = message.channel.id;
serverData[channelId] = { messages: [] };
message.channel.send(
`Channel **${message.channel.name}** enregistré pour le serveur **${message.guild.name}**.`
);
}
if (message.content.startsWith('!tell')) {
const channelId = message.channel.id;
if (serverData[channelId]) {
const newMessage = {
username: message.author.username,
avatar: message.author.displayAvatarURL(),
content: message.content.split(' ').slice(1).join(' '),
attachments: message.attachments.map((attachment) => ({
url: attachment.url,
type: attachment.contentType,
})),
};
serverData[channelId].messages.push(newMessage);
if (webSocketClients[channelId]) {
webSocketClients[channelId].forEach((ws) => {
ws.send(JSON.stringify(newMessage));
});
}
// React to the message with a checkmark emoji
message.react('✅');
} else {
message.channel.send(
"Ce channel n'est pas enregistré. Utilisez `!register` pour commencer."
);
}
}
if (message.content.startsWith('!stell')) {
const channelId = message.channel.id;
if (serverData[channelId]) {
const newMessage = {
username: null, // Set username to null
avatar: 'https://example.com/anonymous-avatar.png', // Placeholder avatar URL
content: message.content.split(' ').slice(1).join(' '),
attachments: message.attachments.map((attachment) => ({
url: attachment.url,
type: attachment.contentType,
})),
};
serverData[channelId].messages.push(newMessage);
if (webSocketClients[channelId]) {
webSocketClients[channelId].forEach((ws) => {
ws.send(JSON.stringify(newMessage));
});
}
message.channel.send(
`Message anonyme ajouté pour le channel **${message.channel.name}**.`
);
} else {
message.channel.send(
"Ce channel n'est pas enregistré. Utilisez `!register` pour commencer."
);
}
}
if (message.content.startsWith('!help')) {
const helpMessage = `
**Commandes disponibles :**
\`!register\` - Enregistrer le channel pour recevoir des messages.
\`!tell <message>\` - Envoyer un message avec votre nom d'utilisateur.
\`!stell <message>\` - Envoyer un message anonymement.
\`!help\` - Afficher ce message d'aide.
`;
message.channel.send(helpMessage);
}
});
bot.login(process.env.BOT_TOKEN);
// Web server
const app = express();
app.set('view engine', 'ejs');
app.get('/view/:channelId', (req, res) => {
const channelId = req.params.channelId;
res.render('index', { channelId });
});
// make it serve the CSS file in the public folder
app.use(express.static('public'));
const server = app.listen(3000, () =>
console.log('Serveur web démarré sur le port 3000')
);
const wss = new WebSocketServer({ server });
wss.on('connection', (ws, req) => {
const params = new URLSearchParams(req.url.split('?')[1]);
const channelId = params.get('channelId');
if (!webSocketClients[channelId]) {
webSocketClients[channelId] = [];
}
webSocketClients[channelId].push(ws);
console.log(`Client connecté au channel ID: ${channelId}`);
ws.on('close', () => {
webSocketClients[channelId] = webSocketClients[channelId].filter(
(client) => client !== ws
);
console.log(`Client déconnecté du channel ID: ${channelId}`);
});
});