Files
bot.js
config.json
package.json
README.md
Code
4 357
bot.js
const { Client, GatewayIntentBits, SlashCommandBuilder, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, PermissionFlagsBits } = require('discord.js');
require('dotenv').config();
const config = require('./config.json');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers
]
});
const activeLoaRequests = new Map();
const activeLoas = new Map();
client.once('ready', async () => {
console.log(`Bot is ready! Logged in as ${client.user.tag}`);
const commands = [
new SlashCommandBuilder()
.setName('loa')
.setDescription('Leave of Absence management')
.addSubcommand(subcommand =>
subcommand
.setName('submit')
.setDescription('Submit a LOA request')
.addStringOption(option =>
option.setName('starting_date')
.setDescription('Starting date (MM/DD/YYYY)')
.setRequired(true))
.addStringOption(option =>
option.setName('end_date')
.setDescription('End date (MM/DD/YYYY)')
.setRequired(true))
.addStringOption(option =>
option.setName('reason')
.setDescription('Reason for LOA')
.setRequired(true))),
new SlashCommandBuilder()
.setName('staffinfo')
.setDescription('Display staff information')
.addStringOption(option =>
option.setName('name')
.setDescription('Staff member name')
.setRequired(true))
.addStringOption(option =>
option.setName('nickname')
.setDescription('Staff member nickname')
.setRequired(true))
.addStringOption(option =>
option.setName('position')
.setDescription('Staff position')
.setRequired(true))
];
try {
await client.application.commands.set(commands);
console.log('Slash commands registered successfully');
} catch (error) {
console.error('Error registering slash commands:', error);
}
setInterval(checkExpiredLoas, 60000);
});
client.on('interactionCreate', async interaction => {
if (interaction.isChatInputCommand()) {
await handleSlashCommand(interaction);
} else if (interaction.isButton()) {
await handleButton(interaction);
}
});
async function handleSlashCommand(interaction) {
if (interaction.commandName === 'loa') {
if (interaction.options.getSubcommand() === 'submit') {
await handleLoaSubmit(interaction);
}
} else if (interaction.commandName === 'staffinfo') {
await handleStaffInfo(interaction);
}
}
async function handleLoaSubmit(interaction) {
const user = interaction.user;
if (activeLoas.has(user.id)) {
await interaction.reply({ content: 'You already have an active LOA. Please wait until it expires before submitting a new one.', ephemeral: true });
return;
}
if (activeLoaRequests.has(user.id)) {
await interaction.reply({ content: 'You already have a pending LOA request. Please wait for it to be reviewed.', ephemeral: true });
return;
}
const startingDate = interaction.options.getString('starting_date');
const endDate = interaction.options.getString('end_date');
const reason = interaction.options.getString('reason');
const embed = new EmbedBuilder()
.setColor('#FFA500')
.setTitle('๐๏ธ LOA Request')
.addFields(
{ name: 'User', value: `${user.tag} (${user.id})`, inline: false },
{ name: 'Starting Date', value: startingDate, inline: true },
{ name: 'End Date', value: endDate, inline: true },
{ name: 'Reason', value: reason, inline: false }
)
.setTimestamp()
.setFooter({ text: `Requested by ${user.tag}`, iconURL: user.displayAvatarURL() });
const approveButton = new ButtonBuilder()
.setCustomId(`loa_approve_${user.id}`)
.setLabel('Approve')
.setStyle(ButtonStyle.Success);
const denyButton = new ButtonBuilder()
.setCustomId(`loa_deny_${user.id}`)
.setLabel('Deny')
.setStyle(ButtonStyle.Danger);
const row = new ActionRowBuilder()
.addComponents(approveButton, denyButton);
try {
const loaChannel = await client.channels.fetch(config.loaRequestChannelId);
const message = await loaChannel.send({ embeds: [embed], components: [row] });
activeLoaRequests.set(user.id, {
messageId: message.id,
userId: user.id,
startingDate: startingDate,
endDate: endDate,
reason: reason
});
await interaction.reply({ content: 'Your LOA request has been submitted!', ephemeral: true });
} catch (error) {
console.error('Error submitting LOA request:', error);
await interaction.reply({ content: 'Failed to submit LOA request. Please check the bot configuration.', ephemeral: true });
}
}
async function handleStaffInfo(interaction) {
const name = interaction.options.getString('name');
const nickname = interaction.options.getString('nickname');
const position = interaction.options.getString('position');
const embed = new EmbedBuilder()
.setColor('#3498DB')
.setTitle('๐ค Staff Information')
.addFields(
{ name: 'Name', value: name, inline: true },
{ name: 'Nickname', value: nickname, inline: true },
{ name: 'Position', value: position, inline: true }
)
.setTimestamp();
await interaction.reply({ embeds: [embed] });
}
async function handleButton(interaction) {
const customId = interaction.customId;
if (customId.startsWith('loa_approve_')) {
await handleLoaApprove(interaction);
} else if (customId.startsWith('loa_deny_')) {
await handleLoaDeny(interaction);
}
}
async function handleLoaApprove(interaction) {
const member = interaction.member;
if (!member.roles.cache.has(config.adminRoleId)) {
await interaction.reply({ content: 'You do not have permission to approve LOA requests. Only admins can approve or deny LOAs.', ephemeral: true });
return;
}
const userId = interaction.customId.split('_')[2];
const loaRequest = activeLoaRequests.get(userId);
if (!loaRequest) {
await interaction.reply({ content: 'LOA request not found.', ephemeral: true });
return;
}
try {
const guild = interaction.guild;
const targetMember = await guild.members.fetch(userId);
const loaRole = await guild.roles.fetch(config.loaRoleId);
if (loaRole) {
await targetMember.roles.add(loaRole);
}
const userEmbed = new EmbedBuilder()
.setColor('#00FF00')
.setTitle('โ
LOA Request Approved')