-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patholdbot.js
280 lines (227 loc) · 8.97 KB
/
oldbot.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import { Client, GatewayIntentBits, ActionRowBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder } from 'discord.js';
import { config } from 'dotenv';
import { joinVoiceChannel, VoiceConnectionStatus, createAudioPlayer, createAudioResource } from '@discordjs/voice';
import path from 'path';
import fs from 'fs';
config();
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildVoiceStates,
],
});
const focusTime = 25 * 60;
const shortBreak = 5 * 60;
const longBreak = 15 * 60;
const activeSessions = new Map();
let timers = {};
client.once('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
// Section 1:
// Command to start interaction
client.on('messageCreate', async (message) => {
// checks if the message's author is a bot and returns early - to prevent infinite loops?
if (message.author.bot) return;
// checks messages to find hi pomo
if (message.content.toLowerCase() === 'hi pomo') {
const userVoiceChannel = message.member.voice.channel;
if (!userVoiceChannel) {
return message.reply('Please join a voice channel before starting a Pomodoro session!');
}
// if there is hi pomo, it will show these button compononents for user interaction
const row = new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId('focus_short')
.setLabel(`Short Break (${shortBreak / 60} mins)`)
.setStyle(ButtonStyle.Primary),
new ButtonBuilder()
.setCustomId('focus_long')
.setLabel(`Long Break (${longBreak / 60} mins)`)
.setStyle(ButtonStyle.Primary)
);
const embed = new EmbedBuilder()
.setColor('#cc95ab')
.setTitle('Hi School of Coder 👋')
.setDescription('Choose your focus session below:')
.setFooter({ text: 'Pomodoro Body Doubling Sessions 👥' });
// sends the components to show to user
message.channel.send({
embeds: [embed],
components: [row],
});
}
});
//
// Step 2: Handle button interactions
client.on('interactionCreate', async (interaction) => {
if (!interaction.isButton()) return;
const voiceChannel = interaction.member.voice.channel;
// Check if the member is in a voice channel
if (!voiceChannel) {
return interaction.reply({
content: 'You need to join a voice channel first!',
ephemeral: true,
});
}
// Check if the user already has an active session
if (activeSessions.has(interaction.user.id)) {
return interaction.reply({
content: 'You already have an active session. Please finish or cancel your current session.',
ephemeral: true,
});
}
// Mark the user as having an active session
activeSessions.set(interaction.user.id, { type: 'focus' });
// Countdown function that updates the message every second
const countdown = (focusMessage, initialTime) => {
let remainingTime = initialTime;
const interval = setInterval(() => {
if (remainingTime <= 0) {
clearInterval(interval);
// Play a sound when time's up (you can add your own sound)
playSound(voiceChannel);
} else {
// Update the embed with the remaining time (in minutes:seconds format)
const minutes = Math.floor(remainingTime / 60);
const seconds = remainingTime % 60;
const embed = new EmbedBuilder()
.setColor('#cc95ab')
.setTitle('Focus Session Countdown')
.setDescription(`Time left: ${minutes}:${seconds < 10 ? '0' : ''}${seconds}`)
.setFooter({ text: 'Stay focused! 👀' });
focusMessage.edit({ embeds: [embed] });
remainingTime--;
}
}, 1000);
return interval; // Return the interval so we can clear it on cancel
};
// Create the cancel button
const cancelButton = new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId('cancel')
.setLabel('Cancel Session')
.setStyle(ButtonStyle.Danger)
);
// Helper function to play a sound when the session ends
const playSound = (channel) => {
const soundPath = path.join(__dirname, 'soft-chimes.mp3');
if (!fs.existsSync(soundPath)) return;
const resource = createAudioResource(soundPath);
const player = createAudioPlayer();
player.play(resource);
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
});
connection.subscribe(player);
player.on('idle', () => connection.destroy());
};
// Handle focus short button
if (interaction.customId === 'focus_short') {
await interaction.reply({
content: `Starting focus session: 25 minutes work and 5 minutes break.`,
components: [cancelButton],
});
// Mute user in the voice channel during focus session
await interaction.member.voice.setMute(true);
// Join the voice channel
const connection = joinVoiceChannel({
channelId: voiceChannel.id,
guildId: voiceChannel.guild.id,
adapterCreator: voiceChannel.guild.voiceAdapterCreator,
});
connection.on(VoiceConnectionStatus.Ready, () => {
console.log('The bot has successfully connected to the voice channel!');
});
// Create a message for the countdown and send it
const focusMessage = await interaction.followUp({
content: `Focus session is in progress...`,
embeds: [
new EmbedBuilder()
.setColor('#cc95ab')
.setTitle('Focus Session Countdown')
.setDescription(`Time left: ${Math.floor(focusTime / 60)}:00`)
.setFooter({ text: 'Stay focused! ⏳' })
]
});
// Start focus timer (work time)
timers[interaction.user.id] = countdown(focusMessage, focusTime);
// Wait for the focus timer to end
await new Promise((resolve) => setTimeout(resolve, focusTime * 1000));
await interaction.followUp(`Focus session is over. Now, enjoy your 5 minutes break.`);
// Start break timer (short break)
timers[interaction.user.id] = countdown(focusMessage, shortBreak);
await new Promise((resolve) => setTimeout(resolve, shortBreak * 1000));
await interaction.followUp('Break is over! Ready to start the next session?');
// Unmute the user after the break
await interaction.member.voice.setMute(false);
// Remove the user from active sessions
activeSessions.delete(interaction.user.id);
// Disconnect from the voice channel after the session
connection.destroy();
}
// Handle focus long button
else if (interaction.customId === 'focus_long') {
await interaction.reply({
content: `Starting focus session: 25 minutes work and 15 minutes break.`,
components: [cancelButton],
});
// Mute user in the voice channel during focus session
await interaction.member.voice.setMute(true);
// Join the voice channel
const connection = joinVoiceChannel({
channelId: voiceChannel.id,
guildId: voiceChannel.guild.id,
adapterCreator: voiceChannel.guild.voiceAdapterCreator,
});
connection.on(VoiceConnectionStatus.Ready, () => {
console.log('The bot has successfully connected to the voice channel!');
});
// Create a message for the countdown and send it
const focusMessage = await interaction.followUp({
content: `Focus session is in progress...`,
embeds: [
new EmbedBuilder()
.setColor('#cc95ab')
.setTitle('Focus Session Countdown')
.setDescription(`Time left: ${Math.floor(focusTime / 60)}:00`)
.setFooter({ text: 'Stay focused! 👀' })
]
});
// Start focus timer (work time)
timers[interaction.user.id] = countdown(focusMessage, focusTime);
// Wait for the focus timer to end
await new Promise((resolve) => setTimeout(resolve, focusTime * 1000));
await interaction.followUp(`Focus session is over. Now, enjoy your 15 minutes break.`);
// Start break timer (long break)
timers[interaction.user.id] = countdown(focusMessage, longBreak);
await new Promise((resolve) => setTimeout(resolve, longBreak * 1000));
await interaction.followUp('Break is over! Ready to start the next session?');
// Unmute the user after the break
await interaction.member.voice.setMute(false);
// Remove the user from active sessions
activeSessions.delete(interaction.user.id);
// Disconnect from the voice channel after the session
connection.destroy();
}
// Handle cancel button
if (interaction.customId === 'cancel') {
activeSessions.delete(interaction.user.id); // Remove user from active sessions
// Clear the timer
if (timers[interaction.user.id]) {
clearInterval(timers[interaction.user.id]); // Clear the timer
}
// Unmute the user if they are muted
await interaction.member.voice.setMute(false);
// Send a cancel message to the user
await interaction.reply({
content: 'Session canceled.',
ephemeral: true,
});
}
});
client.login(process.env.BOT_TOKEN);