refactor/better handle 25+ alarms in alarm command

This commit is contained in:
SowinskiBraeden committed 2024-04-27 15:56:35 -07:00
1 parent 8b9ff28395
commit 52636e9131
2 files changed
+196 -131

No files matched your search

+195 -130
View File
@@ -260,156 +260,270 @@ module.exports = {
if (GuildDB.alarms.length == 0) return interaction.send({ embeds: [new EmbedBuilder().setColor(client.config.Colors.Default).setDescription('**Notice:** No Existing Alarms to Delete.')] }); if (GuildDB.alarms.length == 0) return interaction.send({ embeds: [new EmbedBuilder().setColor(client.config.Colors.Default).setDescription('**Notice:** No Existing Alarms to Delete.')] });
let alarms = new StringSelectMenuBuilder() let alarmComponents = [];
.setCustomId(`DeleteAlarmSelect-${interaction.member.user.id}`)
.setPlaceholder(`Select an Alarm to Delete.`)
for (let i = 0; i < GuildDB.alarms.length; i++) { const max = 25;
alarms.addOptions({ let currentIndex = 0;
let id = 1;
let currentAlarmComponents = new StringSelectMenuBuilder()
.setCustomId(`DeleteAlarmSelect-${id}`)
.setPlaceholder(`Select an Alarm to delete.`);
for (let i = 0; i <= GuildDB.alarms.length; i++) {
if (currentIndex == max || i == GuildDB.alarms.length) {
alarmComponents.push(new ActionRowBuilder().addComponents(currentAlarmComponents));
currentAlarmComponents = new StringSelectMenuBuilder()
.setCustomId(`DeleteAlarmSelect-${id}`)
.setPlaceholder(`Select an Alarm to delete.`);
currentIndex = 0;
id++;
}
currentAlarmComponents.addOptions({
label: GuildDB.alarms[i].name, label: GuildDB.alarms[i].name,
description: `Delete this Alarm`, description: `Delete this alarm`,
value: GuildDB.alarms[i].name value: GuildDB.alarms[i].name,
}); })
}
const opt = new ActionRowBuilder().addComponents(alarms);
return interaction.send({ components: [opt], flags: (1 << 6) }); currentIndex++;
}
return interaction.send({ components: alarmComponents, flags: (1 << 6) });
} else if (args[0].name == 'add-player' || args[0].name == 'remove-player') { } else if (args[0].name == 'add-player' || args[0].name == 'remove-player') {
let add = args[0].name == 'add-player'; const add = args[0].name == 'add-player';
if (GuildDB.alarms.length == 0) return interaction.send({ embeds: [new EmbedBuilder().setColor(client.config.Colors.Default).setDescription(`**Notice:** No Existing Alarms to ${add?'Add':'Remove'} Player ${add?'to':'from'}.`)] }); if (GuildDB.alarms.length == 0) return interaction.send({ embeds: [new EmbedBuilder().setColor(client.config.Colors.Default).setDescription(`**Notice:** No Existing Alarms to ${add?'Add':'Remove'} Player ${add?'to':'from'}.`)] });
let alarms = new StringSelectMenuBuilder() let alarmComponents = [];
.setCustomId(`ManageAlarmIgnored-${add?'add':'remove'}-${args[0].options[0].value}-${interaction.member.user.id}`)
.setPlaceholder(`Select an Alarm to ${add?'Add':'Remove'} Player ${add?'to':'from'}.`)
for (let i = 0; i < GuildDB.alarms.length; i++) { const max = 25;
alarms.addOptions({ let currentIndex = 0;
let id = 1;
let currentAlarmComponents = new StringSelectMenuBuilder()
.setCustomId(`ManageAlarmIgnored-${add?'add':'remove'}-${args[0].options[0].value}-${id}`)
.setPlaceholder(`Select an Alarm to ${add?'add':'remove'} player ${add?'to':'from'}.`);
for (let i = 0; i <= GuildDB.alarms.length; i++) {
if (currentIndex == max || i == GuildDB.alarms.length) {
alarmComponents.push(new ActionRowBuilder().addComponents(currentAlarmComponents));
currentAlarmComponents = new StringSelectMenuBuilder()
.setCustomId(`ManageAlarmIgnored-${add?'add':'remove'}-${args[0].options[0].value}-${id}`)
.setPlaceholder(`Select an Alarm to ${add?'add':'remove'} player ${add?'to':'from'}.`);
currentIndex = 0;
id++;
}
currentAlarmComponents.addOptions({
label: GuildDB.alarms[i].name, label: GuildDB.alarms[i].name,
description: `${add?'Add':'Remove'} Player ${add?'to':'from'} this Alarm`, description: `${add?'Add':'Remove'} player ${add?'to':'from'} this Alarm`,
value: GuildDB.alarms[i].name value: GuildDB.alarms[i].name
}); });
}
const opt = new ActionRowBuilder().addComponents(alarms);
return interaction.send({ components: [opt] }); currentIndex++;
}
return interaction.send({ components: alarmComponents, flags: (1 << 6) });
} else if (args[0].name == 'set-rule' || args[0].name == 'remove-rule') { } else if (args[0].name == 'set-rule' || args[0].name == 'remove-rule') {
if (GuildDB.alarms.length == 0) return interaction.send({ embeds: [new EmbedBuilder().setColor(client.config.Colors.Default).setDescription('**Notice:** No Existing Alarms to configure.')] }); if (GuildDB.alarms.length == 0) return interaction.send({ embeds: [new EmbedBuilder().setColor(client.config.Colors.Default).setDescription('**Notice:** No Existing Alarms to configure.')] });
let id = `ManageRule-${args[0].name=='set-rule'?'add':'remove'}${args[0].name=='set-rule'?`-${args[0].options[0].value}`:''}-${interaction.member.user.id}` let alarmComponents = [];
let alarms = new StringSelectMenuBuilder() const max = 25;
.setCustomId(id) let currentIndex = 0;
.setPlaceholder(`Select an Alarm to configure.`) let id = 1;
let currentAlarmComponents = new StringSelectMenuBuilder()
.setCustomId(`ManageRule-${args[0].name=='set-rule'?'add':'remove'}${args[0].name=='set-rule'?`-${args[0].options[0].value}`:''}-${id}`)
.setPlaceholder(`Select an Alarm to configure.`);
for (let i = 0; i < GuildDB.alarms.length; i++) { for (let i = 0; i <= GuildDB.alarms.length; i++) {
alarms.addOptions({ if (currentIndex == max || i == GuildDB.alarms.length) {
alarmComponents.push(new ActionRowBuilder().addComponents(currentAlarmComponents));
currentAlarmComponents = new StringSelectMenuBuilder()
.setCustomId(`ManageRule-${args[0].name=='set-rule'?'add':'remove'}${args[0].name=='set-rule'?`-${args[0].options[0].value}`:''}-${id}`)
.setPlaceholder(`Select an Alarm to configure.`);
currentIndex = 0;
id++;
}
currentAlarmComponents.addOptions({
label: GuildDB.alarms[i].name, label: GuildDB.alarms[i].name,
description: `Configure this Alarm`, description: `Configure this Alarm`,
value: GuildDB.alarms[i].name value: GuildDB.alarms[i].name
}); });
}
const opt = new ActionRowBuilder().addComponents(alarms);
return interaction.send({ components: [opt], flags: (1 << 6) }); currentIndex++;
}
return interaction.send({ components: alarmComponents, flags: (1 << 6) });
} else if (args[0].name == 'enable' || args[0].name == 'disable') { } else if (args[0].name == 'enable' || args[0].name == 'disable') {
if (GuildDB.alarms.length == 0) return interaction.send({ embeds: [new EmbedBuilder().setColor(client.config.Colors.Default).setDescription('**Notice:** No Existing Alarms to configure.')] }); const disable = args[0].name == 'disable';
const message = disable ? 'disable' : 'enable';
let disable = args[0].name == 'disable' ? true : false; if (GuildDB.alarms.length == 0) return interaction.send({
embeds: [
new EmbedBuilder()
.setColor(client.config.Colors.Default)
.setDescription(`**Notice:**\n> No Existing Alarms to ${message}.`)
]
});
let alarms = new StringSelectMenuBuilder() if (!GuildDB.alarms.some(alarm => alarm.disabled == disable)) return interaction.send({
.setCustomId(`EnableOrDisableAlarm-${disable ? 'disable' : 'enable'}-${interaction.member.user.id}`) embeds: [
.setPlaceholder(`Select an Alarm to ${disable ? 'disable' : 'enable'}.`); new EmbedBuilder()
.setColor(client.config.Colors.Default)
.setDescription(`**Notice:**\n> There are no alarms to ${message}.`)
]
});
for (let i = 0; i < GuildDB.alarms.length; i++) { let alarmComponents = [];
const max = 25;
let currentIndex = 0;
let id = 1;
let currentAlarmComponents = new StringSelectMenuBuilder()
.setCustomId(`EnableOrDisableAlarm-${message}-${id}`)
.setPlaceholder(`Select an Alarm to ${message}.`);
for (let i = 0; i <= GuildDB.alarms.length; i++) {
if (currentIndex == max || i == GuildDB.alarms.length) {
alarmComponents.push(new ActionRowBuilder().addComponents(currentAlarmComponents));
currentAlarmComponents = new StringSelectMenuBuilder()
.setCustomId(`EnableOrDisableAlarm-${message}-${id}`)
.setPlaceholder(`Select an Alarm to ${message}.`);
currentIndex = 0;
id++;
}
// Handle selecting an active alarm to disable
if (disable && !GuildDB.alarms[i].disabled) { if (disable && !GuildDB.alarms[i].disabled) {
alarms.addOptions({ currentAlarmComponents.addOptions({
label: GuildDB.alarms[i].name, label: GuildDB.alarms[i].name,
description: `Disable this alarm`, description: `Disable this alarm`,
value: GuildDB.alarms[i].name, value: GuildDB.alarms[i].name,
}) })
} else if (!disable && GuildDB.alarms[i].disabled) { }
alarms.addOptions({
// Handle selecting a disabled alarm to enable
if (!disable && GuildDB.alarms[i].disabled) {
currentAlarmComponents.addOptions({
label: GuildDB.alarms[i].name, label: GuildDB.alarms[i].name,
description: `Enable this alarm`, description: `Enable this alarm`,
value: GuildDB.alarms[i].name, value: GuildDB.alarms[i].name,
}) })
} }
currentIndex++;
} }
const opt = new ActionRowBuilder().addComponents(alarms); return interaction.send({ components: alarmComponents, flags: (1 << 6) });
return interaction.send({ components: [opt], flags: (1 << 6) });
} else if (args[0].name == 'rename') { } else if (args[0].name == 'rename') {
if (GuildDB.alarms.length == 0) return interaction.send({ embeds: [new EmbedBuilder().setColor(client.config.Colors.Default).setDescription('**Notice:** No Existing Alarms to configure.')] }); if (GuildDB.alarms.length == 0) return interaction.send({ embeds: [new EmbedBuilder().setColor(client.config.Colors.Default).setDescription('**Notice:**\n> No Existing Alarms to configure.')] });
let disable = args[0].name == 'disable' ? true : false; let alarmComponents = [];
let alarms = new StringSelectMenuBuilder() const max = 25;
.setCustomId(`RenameAlarm-${args[0].options[0].value}-${interaction.member.user.id}`) let currentIndex = 0;
let id = 1;
let currentAlarmComponents = new StringSelectMenuBuilder()
.setCustomId(`RenameAlarm-${args[0].options[0].value}-${id}`)
.setPlaceholder(`Select an Alarm to rename.`); .setPlaceholder(`Select an Alarm to rename.`);
for (let i = 0; i < GuildDB.alarms.length; i++) { for (let i = 0; i <= GuildDB.alarms.length; i++) {
alarms.addOptions({ if (currentIndex == max || i == GuildDB.alarms.length) {
alarmComponents.push(new ActionRowBuilder().addComponents(currentAlarmComponents));
currentAlarmComponents = new StringSelectMenuBuilder()
.setCustomId(`RenameAlarm-${args[0].options[0].value}-${id}`)
.setPlaceholder(`Select an Alarm to rename.`);
currentIndex = 0;
id++;
}
currentAlarmComponents.addOptions({
label: GuildDB.alarms[i].name, label: GuildDB.alarms[i].name,
description: `Rename this alarm`, description: `Rename this alarm`,
value: GuildDB.alarms[i].name, value: GuildDB.alarms[i].name,
}) });
currentIndex++;
} }
const opt = new ActionRowBuilder().addComponents(alarms); return interaction.send({ components: alarmComponents, flags: (1 << 6) });
return interaction.send({ components: [opt], flags: (1 << 6) });
} else if (args[0].name == 'move-origin') { } else if (args[0].name == 'move-origin') {
if (GuildDB.alarms.length == 0) return interaction.send({ embeds: [new EmbedBuilder().setColor(client.config.Colors.Default).setDescription('**Notice:** No Existing Alarms to configure.')] }); if (GuildDB.alarms.length == 0) return interaction.send({ embeds: [new EmbedBuilder().setColor(client.config.Colors.Default).setDescription('**Notice:** No Existing Alarms to configure.')] });
let alarms = new StringSelectMenuBuilder() let alarmComponents = [];
.setCustomId(`MoveOrigin-${args[0].options[0].value}-${args[0].options[1].value}-${interaction.member.user.id}`)
.setPlaceholder(`Select an Alarm to move.`);
for (let i = 0; i < GuildDB.alarms.length; i++) { const max = 25;
alarms.addOptions({ let currentIndex = 0;
let id = 1;
let currentAlarmComponents = new StringSelectMenuBuilder()
.setCustomId(`MoveOrigin-${args[0].options[0].value}-${args[0].options[1].value}-${id}`)
.setPlaceholder(`Select an Alarm to rename.`);
for (let i = 0; i <= GuildDB.alarms.length; i++) {
if (currentIndex == max || i == GuildDB.alarms.length) {
alarmComponents.push(new ActionRowBuilder().addComponents(currentAlarmComponents));
currentAlarmComponents = new StringSelectMenuBuilder()
.setCustomId(`MoveOrigin-${args[0].options[0].value}-${args[0].options[1].value}-${id}`)
.setPlaceholder(`Select an Alarm to rename.`);
currentIndex = 0;
id++;
}
currentAlarmComponents.addOptions({
label: GuildDB.alarms[i].name, label: GuildDB.alarms[i].name,
description: `Move the Origin of this Alarm`, description: `Move the Origin of this Alarm`,
value: GuildDB.alarms[i].name, value: GuildDB.alarms[i].name,
}) });
currentIndex++;
} }
const opt = new ActionRowBuilder().addComponents(alarms); return interaction.send({ components: alarmComponents, flags: (1 << 6) });
return interaction.send({ components: [opt], flags: (1 << 6) });
} else if (args[0].name == 'mute') { } else if (args[0].name == 'mute') {
if (GuildDB.alarms.length == 0) return interaction.send({ embeds: [new EmbedBuilder().setColor(client.config.Colors.Default).setDescription('**Notice:** No Existing Alarms to configure.')] }); if (GuildDB.alarms.length == 0) return interaction.send({ embeds: [new EmbedBuilder().setColor(client.config.Colors.Default).setDescription('**Notice:** No Existing Alarms to configure.')] });
let alarms = new StringSelectMenuBuilder() let alarmComponents = [];
.setCustomId(`MuteAlarm-${args[0].options[0].value ? 1 : 0}-${interaction.member.user.id}`)
const max = 25;
let currentIndex = 0;
let id = 1;
let currentAlarmComponents = new StringSelectMenuBuilder()
.setCustomId(`MuteAlarm-${args[0].options[0].value ? 1 : 0}-${id}`)
.setPlaceholder(`Select an Alarm to mute.`); .setPlaceholder(`Select an Alarm to mute.`);
for (let i = 0; i < GuildDB.alarms.length; i++) { for (let i = 0; i <= GuildDB.alarms.length; i++) {
alarms.addOptions({ if (currentIndex == max || i == GuildDB.alarms.length) {
alarmComponents.push(new ActionRowBuilder().addComponents(currentAlarmComponents));
currentAlarmComponents = new StringSelectMenuBuilder()
.setCustomId(`MuteAlarm-${args[0].options[0].value ? 1 : 0}-${id}`)
.setPlaceholder(`Select an Alarm to rename.`);
currentIndex = 0;
id++;
}
currentAlarmComponents.addOptions({
label: GuildDB.alarms[i].name, label: GuildDB.alarms[i].name,
description: `Mute the role pings of this Alarm`, description: `Mute role pings of this Alarm`,
value: GuildDB.alarms[i].name, value: GuildDB.alarms[i].name,
}) });
currentIndex++;
} }
const opt = new ActionRowBuilder().addComponents(alarms); return interaction.send({ components: alarmComponents, flags: (1 << 6) });
return interaction.send({ components: [opt], flags: (1 << 6) });
} }
}, },
}, },
@@ -427,11 +541,11 @@ module.exports = {
const opt = new ActionRowBuilder() const opt = new ActionRowBuilder()
.addComponents( .addComponents(
new ButtonBuilder() new ButtonBuilder()
.setCustomId(`DeleteAlarm-yes-${alarm.name}-${interaction.member.user.id}`) .setCustomId(`DeleteAlarm-yes-${alarm.name}`)
.setLabel("Yes") .setLabel("Yes")
.setStyle(ButtonStyle.Danger), .setStyle(ButtonStyle.Danger),
new ButtonBuilder() new ButtonBuilder()
.setCustomId(`DeleteAlarm-no-${alarm.name}-${interaction.member.user.id}`) .setCustomId(`DeleteAlarm-no-${alarm.name}`)
.setLabel("No") .setLabel("No")
.setStyle(ButtonStyle.Success) .setStyle(ButtonStyle.Success)
) )
@@ -469,13 +583,6 @@ module.exports = {
}, },
ManageAlarmIgnored: { ManageAlarmIgnored: {
run: async(client, interaction, GuildDB) => { run: async(client, interaction, GuildDB) => {
if (!interaction.customId.endsWith(interaction.member.user.id)) {
return interaction.reply({
content: "This menu is not for you",
flags: (1 << 6)
})
}
let alarm = GuildDB.alarms.find(alarm => alarm.name == interaction.values[0]); let alarm = GuildDB.alarms.find(alarm => alarm.name == interaction.values[0]);
let alarmIndex = GuildDB.alarms.indexOf(alarm); let alarmIndex = GuildDB.alarms.indexOf(alarm);
@@ -508,13 +615,6 @@ module.exports = {
}, },
ManageRule: { ManageRule: {
run: async(client, interaction, GuildDB) => { run: async(client, interaction, GuildDB) => {
if (!interaction.customId.endsWith(interaction.member.user.id)) {
return interaction.reply({
content: "This menu is not for you",
flags: (1 << 6)
})
}
let alarm = GuildDB.alarms.find(alarm => alarm.name == interaction.values[0]); let alarm = GuildDB.alarms.find(alarm => alarm.name == interaction.values[0]);
let alarmIndex = GuildDB.alarms.indexOf(alarm); let alarmIndex = GuildDB.alarms.indexOf(alarm);
@@ -559,13 +659,6 @@ module.exports = {
}, },
DeleteAlarmRule: { DeleteAlarmRule: {
run: async(client, interaction, GuildDB) => { run: async(client, interaction, GuildDB) => {
if (!interaction.customId.endsWith(interaction.member.user.id)) {
return interaction.reply({
content: "This menu is not for you",
flags: (1 << 6)
})
}
let alarm = GuildDB.alarms.find(alarm => alarm.name == interaction.customId.split('-')[1]); let alarm = GuildDB.alarms.find(alarm => alarm.name == interaction.customId.split('-')[1]);
let alarmIndex = GuildDB.alarms.indexOf(alarm); let alarmIndex = GuildDB.alarms.indexOf(alarm);
@@ -592,16 +685,9 @@ module.exports = {
}, },
EnableOrDisableAlarm: { EnableOrDisableAlarm: {
run: async(client, interaction, GuildDB) => { run: async(client, interaction, GuildDB) => {
if (!interaction.customId.endsWith(interaction.member.user.id)) {
return interaction.reply({
content: "This menu is not for you",
flags: (1 << 6)
})
}
let alarm = GuildDB.alarms.find(alarm => alarm.name == interaction.values[0]); let alarm = GuildDB.alarms.find(alarm => alarm.name == interaction.values[0]);
let alarmIndex = GuildDB.alarms.indexOf(alarm); let alarmIndex = GuildDB.alarms.indexOf(alarm);
let disable = interaction.customId.split('-')[1] == 'disable' ? true : false; let disable = interaction.customId.split('-')[1] == 'disable';
alarm.disabled = disable; alarm.disabled = disable;
GuildDB.alarms[alarmIndex] = alarm GuildDB.alarms[alarmIndex] = alarm
@@ -615,7 +701,7 @@ module.exports = {
let successEmbed = new EmbedBuilder() let successEmbed = new EmbedBuilder()
.setColor(client.config.Colors.Green) .setColor(client.config.Colors.Green)
.setDescription(`**Success:** Successfully ${disable ? 'disabled' : 'enabled'} the Alarm **${interaction.values[0]}**`); .setDescription(`**Success:**\n> Successfully ${disable ? 'disabled' : 'enabled'} the Alarm **${interaction.values[0]}**`);
return interaction.update({ embeds: [successEmbed], components: [] }); return interaction.update({ embeds: [successEmbed], components: [] });
} }
@@ -623,13 +709,6 @@ module.exports = {
MoveOrigin: { MoveOrigin: {
run: async(client, interaction, GuildDB) => { run: async(client, interaction, GuildDB) => {
if (!interaction.customId.endsWith(interaction.member.user.id)) {
return interaction.reply({
content: "This menu is not for you",
flags: (1 << 6)
})
}
let alarm = GuildDB.alarms.find(alarm => alarm.name == interaction.values[0]); let alarm = GuildDB.alarms.find(alarm => alarm.name == interaction.values[0]);
let alarmIndex = GuildDB.alarms.indexOf(alarm); let alarmIndex = GuildDB.alarms.indexOf(alarm);
let origin = [parseFloat(interaction.customId.split('-')[1]), parseFloat(interaction.customId.split('-')[2])]; let origin = [parseFloat(interaction.customId.split('-')[1]), parseFloat(interaction.customId.split('-')[2])];
@@ -654,13 +733,6 @@ module.exports = {
RenameAlarm: { RenameAlarm: {
run: async(client, interaction, GuildDB) => { run: async(client, interaction, GuildDB) => {
if (!interaction.customId.endsWith(interaction.member.user.id)) {
return interaction.reply({
content: "This menu is not for you",
flags: (1 << 6)
})
}
let alarm = GuildDB.alarms.find(alarm => alarm.name == interaction.values[0]); let alarm = GuildDB.alarms.find(alarm => alarm.name == interaction.values[0]);
let alarmIndex = GuildDB.alarms.indexOf(alarm); let alarmIndex = GuildDB.alarms.indexOf(alarm);
let oldName = alarm.name; let oldName = alarm.name;
@@ -685,13 +757,6 @@ module.exports = {
MuteAlarm: { MuteAlarm: {
run: async(client, interaction, GuildDB) => { run: async(client, interaction, GuildDB) => {
if (!interaction.customId.endsWith(interaction.member.user.id)) {
return interaction.reply({
content: "This menu is not for you",
flags: (1 << 6)
})
}
let alarm = GuildDB.alarms.find(alarm => alarm.name == interaction.values[0]); let alarm = GuildDB.alarms.find(alarm => alarm.name == interaction.values[0]);
let alarmIndex = GuildDB.alarms.indexOf(alarm); let alarmIndex = GuildDB.alarms.indexOf(alarm);
let mute = parseInt(interaction.customId.split('-')[1]); let mute = parseInt(interaction.customId.split('-')[1]);
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "dayzr-bot", "name": "dayzr-bot",
"version": "13.1.7", "version": "13.1.8",
"description": "A General Purpose Discord Bot for DayZ Nitrado Servers.", "description": "A General Purpose Discord Bot for DayZ Nitrado Servers.",
"main": "index.js", "main": "index.js",
"nodemonConfig": { "nodemonConfig": {