-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.js
More file actions
238 lines (212 loc) · 9.9 KB
/
Copy pathcommands.js
File metadata and controls
238 lines (212 loc) · 9.9 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
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
const { ModalBuilder, TextInputBuilder, TextInputStyle, ActionRowBuilder,
EmbedBuilder, ButtonBuilder, ButtonStyle, ChannelType } = require('discord.js');
const taskManager = require('./taskManager');
const { buildTaskListEmbed, buildDoneEmbed } = require('./embedBuilder');
const commands = {
// /tasklist — posts or refreshes the persistent task board + done board
tasklist: {
data: {
name: 'tasklist',
description: '📋 Post the live task board + done board in this channel',
},
async execute(interaction) {
await interaction.deferReply();
const guildId = interaction.guild.id;
const guildData = taskManager.getGuildTasks(guildId);
const settings = taskManager.getGuildSettings(guildId);
// Delete old main board
if (guildData.listMessageId && guildData.listChannelId) {
try {
const oldCh = await interaction.guild.channels.fetch(guildData.listChannelId);
const oldMsg = await oldCh.messages.fetch(guildData.listMessageId);
await oldMsg.delete();
} catch {}
}
// Delete old done board
if (guildData.doneMessageId && guildData.doneChannelId) {
try {
const oldCh = await interaction.guild.channels.fetch(guildData.doneChannelId);
const oldMsg = await oldCh.messages.fetch(guildData.doneMessageId);
await oldMsg.delete();
} catch {}
}
// Post done board first (so main board appears below it, i.e. more recent)
const doneEmbed = buildDoneEmbed(guildData.tasks || [], settings);
const doneMsg = await interaction.channel.send({ embeds: [doneEmbed] });
taskManager.setDoneMessage(guildId, doneMsg.id, interaction.channel.id);
// Post main board
const { embeds, components } = buildTaskListEmbed(guildData, settings);
const msg = await interaction.editReply({ embeds, components });
taskManager.setListMessage(guildId, msg.id, interaction.channel.id);
}
},
// /addtask — quick add without UI
addtask: {
data: {
name: 'addtask',
description: '➕ Quickly add a task',
options: [
{ name: 'title', description: 'Task title', type: 3, required: true },
{ name: 'description', description: 'Task description', type: 3, required: false },
{ name: 'assignees', description: 'Mention users (e.g. @User1 @User2)', type: 3, required: false },
{ name: 'deadline', description: 'Deadline (YYYY-MM-DD or YYYY-MM-DD HH:MM)', type: 3, required: false },
{ name: 'reminder', description: 'Daily reminder until deadline?', type: 5, required: false },
]
},
async execute(interaction) {
await interaction.deferReply({ ephemeral: true });
const title = interaction.options.getString('title');
const description = interaction.options.getString('description') || '';
const assigneeStr = interaction.options.getString('assignees') || '';
const deadlineStr = interaction.options.getString('deadline') || '';
const reminder = interaction.options.getBoolean('reminder') || false;
const assignees = [...assigneeStr.matchAll(/<@!?(\d+)>/g)].map(m => m[1]);
let deadline = null;
if (deadlineStr) {
const d = new Date(deadlineStr);
if (!isNaN(d)) deadline = d.toISOString();
}
const guildId = interaction.guild.id;
taskManager.addTask(guildId, { title, description, assignees, deadline, reminder });
await updateListMessage(interaction.guild);
await interaction.editReply({ content: `✅ Task **"${title}"** added!` });
}
},
// /setup — creates #tasks channel and posts live boards
setup: {
data: {
name: 'setup',
description: '🔧 Create the #tasks channel and post the live task boards',
},
async execute(interaction) {
await interaction.deferReply({ ephemeral: true });
const guildId = interaction.guild.id;
const guildData = taskManager.getGuildTasks(guildId);
const settings = taskManager.getGuildSettings(guildId);
// Fetch fresh channel list from API (cache may be empty on cold start)
const allChannels = await interaction.guild.channels.fetch();
let tasksChannel = allChannels.find(ch => ch && ch.name === 'tasks' && ch.isTextBased());
if (!tasksChannel) {
tasksChannel = await interaction.guild.channels.create({
name: 'tasks',
type: ChannelType.GuildText,
topic: '📋 Live task board — managed by TaskBot',
});
}
// Delete old boards if they exist
if (guildData.listMessageId && guildData.listChannelId) {
try {
const oldCh = await interaction.guild.channels.fetch(guildData.listChannelId);
const oldMsg = await oldCh.messages.fetch(guildData.listMessageId);
await oldMsg.delete();
} catch {}
}
if (guildData.doneMessageId && guildData.doneChannelId) {
try {
const oldCh = await interaction.guild.channels.fetch(guildData.doneChannelId);
const oldMsg = await oldCh.messages.fetch(guildData.doneMessageId);
await oldMsg.delete();
} catch {}
}
// Post done board, then main board
const doneEmbed = buildDoneEmbed(guildData.tasks || [], settings);
const doneMsg = await tasksChannel.send({ embeds: [doneEmbed] });
await doneMsg.pin().catch(() => {});
taskManager.setDoneMessage(guildId, doneMsg.id, tasksChannel.id);
const { embeds, components } = buildTaskListEmbed(guildData, settings);
const mainMsg = await tasksChannel.send({ embeds, components });
await mainMsg.pin().catch(() => {});
taskManager.setListMessage(guildId, mainMsg.id, tasksChannel.id);
await interaction.editReply({
content: `✅ TaskBot is set up in ${tasksChannel}! The task boards are live and pinned.`,
});
}
},
// /donate
donate: {
data: { name: 'donate', description: '☕ Support the bot development' },
async execute(interaction) {
const embed = new EmbedBuilder()
.setTitle('☕ Support TaskBot')
.setColor(0xFF5E5B)
.setDescription(
'TaskBot is free to use! If it helps your team stay organised, consider buying the developer a coffee.\n\n' +
'**[☕ Donate on Ko-fi](https://ko-fi.com/codebater)**\n\n' +
'Every donation helps keep the bot running and improving. Thank you! 🙏'
)
.setFooter({ text: 'No amount is too small — it all helps!' });
await interaction.reply({ embeds: [embed] });
}
},
// /settings
settings: {
data: {
name: 'settings',
description: '⚙️ Configure timezone, working hours & display options',
},
async execute(interaction) {
await showSettingsPanel(interaction);
}
}
};
async function showSettingsPanel(interaction) {
const guildId = interaction.guild.id;
const s = taskManager.getGuildSettings(guildId);
const dayNames = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
const workDaysStr = (s.workDays || [1,2,3,4,5]).map(d => dayNames[d]).join(', ');
const embed = new EmbedBuilder()
.setTitle('⚙️ TaskBot Settings')
.setColor(0xEB459E)
.addFields(
{ name: '🌍 Timezone', value: s.timezone, inline: true },
{ name: '🕘 Work Hours', value: `${s.workStart} – ${s.workEnd}`, inline: true },
{ name: '📅 Work Days', value: workDaysStr, inline: true },
{ name: '📊 Progress Bar', value: `${s.barLength ?? 16} blocks`, inline: true },
{ name: '✅ Done preview', value: `${s.donePreviewCount ?? 3} on main board`, inline: true },
{ name: '📋 Done board size', value: `${s.doneListShowCount ?? 10} tasks`, inline: true },
)
.setFooter({ text: 'Reminders only fire during working hours · members can set personal timezone' });
const row1 = new ActionRowBuilder().addComponents(
new ButtonBuilder().setCustomId('settings:timezone').setLabel('🌍 Timezone').setStyle(ButtonStyle.Primary),
new ButtonBuilder().setCustomId('settings:hours').setLabel('🕘 Work Hours').setStyle(ButtonStyle.Primary),
new ButtonBuilder().setCustomId('settings:days').setLabel('📅 Work Days').setStyle(ButtonStyle.Primary),
new ButtonBuilder().setCustomId('settings:display').setLabel('📊 Display').setStyle(ButtonStyle.Secondary),
new ButtonBuilder().setCustomId('settings:mytz').setLabel('👤 My Timezone').setStyle(ButtonStyle.Secondary),
);
const opts = { embeds: [embed], components: [row1], ephemeral: true };
if (interaction.replied || interaction.deferred) {
await interaction.editReply(opts);
} else {
await interaction.reply(opts);
}
}
async function updateListMessage(guild) {
const guildId = guild.id;
const guildData = taskManager.getGuildTasks(guildId);
const settings = taskManager.getGuildSettings(guildId);
// Update main board
if (guildData.listMessageId && guildData.listChannelId) {
try {
const channel = await guild.channels.fetch(guildData.listChannelId);
const msg = await channel.messages.fetch(guildData.listMessageId);
const { embeds, components } = buildTaskListEmbed(guildData, settings);
await msg.edit({ embeds, components });
} catch (e) {
console.error('Could not update main board:', e.message);
}
}
// Update done board
if (guildData.doneMessageId && guildData.doneChannelId) {
try {
const channel = await guild.channels.fetch(guildData.doneChannelId);
const msg = await channel.messages.fetch(guildData.doneMessageId);
const doneEmbed = buildDoneEmbed(guildData.tasks || [], settings);
await msg.edit({ embeds: [doneEmbed] });
} catch (e) {
console.error('Could not update done board:', e.message);
}
}
}
module.exports = commands;
module.exports.showSettingsPanel = showSettingsPanel;
module.exports.updateListMessage = updateListMessage;