Files
bot.js
config.json
package.json
Code
3 283
bot.js
const {
Client,
EmbedBuilder,
GatewayIntentBits,
PermissionFlagsBits,
SlashCommandBuilder
} = require('discord.js');
const { config: loadEnv } = require('dotenv');
const config = require('./config.json');
loadEnv();
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
const activeShifts = new Map();
const commands = [
new SlashCommandBuilder()
.setName('promotion')
.setDescription('Log a staff promotion')
.addUserOption(option => option
.setName('member')
.setDescription('The staff member being promoted')
.setRequired(true))
.addStringOption(option => option
.setName('rank')
.setDescription('The member’s new rank')
.setMaxLength(100)
.setRequired(true))
.addStringOption(option => option
.setName('reason')
.setDescription('Why the member is being promoted')
.setMaxLength(1000)
.setRequired(false)),
new SlashCommandBuilder()
.setName('infraction')
.setDescription('Log a staff infraction')
.addUserOption(option => option
.setName('member')
.setDescription('The staff member receiving the infraction')
.setRequired(true))
.addStringOption(option => option
.setName('type')
.setDescription('The type of infraction')
.setRequired(true)
.addChoices(
{ name: 'Warning', value: 'Warning' },
{ name: 'Strike', value: 'Strike' },
{ name: 'Suspension', value: 'Suspension' },
{ name: 'Termination', value: 'Termination' }
))
.addStringOption(option => option
.setName('reason')
.setDescription('Reason for the infraction')
.setMaxLength(1000)
.setRequired(true)),
new SlashCommandBuilder()
.setName('shift')
.setDescription('Start or end your staff shift')
.addStringOption(option => option
.setName('action')
.setDescription('Whether to start or end your shift')
.setRequired(true)
.addChoices(
{ name: 'Start shift', value: 'start' },
{ name: 'End shift', value: 'end' }
))
].map(command => command.toJSON());
function isAuthorizedStaff(interaction) {
if (interaction.memberPermissions?.has(PermissionFlagsBits.Administrator)) {
return true;
}
const allowedRoleIds = config.staffRoleIds.filter(Boolean);
if (allowedRoleIds.length === 0) {
return false;
}
const roles = interaction.member?.roles;
if (Array.isArray(roles)) {
return allowedRoleIds.some(roleId => roles.includes(roleId));
}
return allowedRoleIds.some(roleId => roles?.cache?.has(roleId));
}
function isValidChannelId(channelId) {
return /^\d{17,20}$/.test(channelId || '');
}
async function sendLog(channelId, embed) {
if (!isValidChannelId(channelId)) {
throw new Error('The log channel ID is not configured.');
}
const channel = await client.channels.fetch(channelId);
if (!channel?.isTextBased() || !channel.send) {
throw new Error('The configured log channel could not be used.');
}
await channel.send({ embeds: [embed] });
}
function addBanner(embed, url) {
if (/^https?:\/\//i.test(url || '')) {
embed.setImage(url);
}
return embed;
}
function formatDuration(milliseconds) {
const totalSeconds = Math.floor(milliseconds / 1000);
const hours = Math.floor(totalSeconds / 3600);
const minutes = Math.floor((totalSeconds % 3600) / 60);
const seconds = totalSeconds % 60;
return `${hours}h ${minutes}m ${seconds}s`;
}
async function handlePromotion(interaction) {
const member = interaction.options.getUser('member', true);
const rank = interaction.options.getString('rank', true);
const reason = interaction.options.getString('reason') || 'No reason provided.';
const embed = addBanner(new EmbedBuilder()
.setColor(0x2ecc71)
.setTitle('Staff Promotion')
.setThumbnail(member.displayAvatarURL())
.addFields(
{ name: 'Promoted Member', value: `${member} (\`${member.id}\`)` },
{ name: 'New Rank', value: rank },
{ name: 'Reason', value: reason },
{ name: 'Approved By', value: `${interaction.user}` }
)
.setTimestamp(), config.promotionBannerUrl);
await sendLog(config.promotionLogChannelId, embed);
await interaction.reply({ content: `Promotion for ${member} has been logged.`, ephemeral: true });
}
async function handleInfraction(interaction) {
const member = interaction.options.getUser('member', true);
const type = interaction.options.getString('type', true);
const reason = interaction.options.getString('reason', true);
const embed = addBanner(new EmbedBuilder()
.setColor(0xe74c3c)
.setTitle('Staff Infraction')
.setThumbnail(member.displayAvatarURL())
.addFields(
{ name: 'Staff Member', value: `${member} (\`${member.id}\`)` },
{ name: 'Infraction Type', value: type },
{ name: 'Reason', value: reason },
{ name: 'Issued By', value: `${interaction.user}` }
)
.setTimestamp(), config.infractionBannerUrl);
await sendLog(config.infractionLogChannelId, embed);
await interaction.reply({ content: `${type} for ${member} has been logged.`, ephemeral: true });
}
async function handleShift(interaction) {
const action = interaction.options.getString('action', true);
const userId = interaction.user.id;
const startedAt = activeShifts.get(userId);
if (action === 'start') {
if (startedAt) {
await interaction.reply({ content: 'You already have an active shift.', ephemeral: true });
return;
}
const startTime = new Date();
const embed = new EmbedBuilder()
.setColor(0x3498db)
.setTitle('Shift Started')
.setDescription(`${interaction.user} started a staff shift.`)
.addFields({ name: 'Started At', value: `<t:${Math.floor(startTime.getTime() / 1000)}:F>` })
.setThumbnail(interaction.user.displayAvatarURL())
.setTimestamp(startTime);
await sendLog(config.shiftLogChannelId, embed);
activeShifts.set(userId, startTime);
await interaction.reply({ content: 'Your shift has started.', ephemeral: true });
return;
}
if (!startedAt) {
await interaction.reply({ content: 'You do not have an active shift to end.', ephemeral: true });
return;
}
const endTime = new Date();
const embed = new EmbedBuilder()
.setColor(0xf39c12)
.setTitle('Shift Ended')
.setDescription(`${interaction.user} ended a staff shift.`)
.addFields(
{ name: 'Started At', value: `<t:${Math.floor(startedAt.getTime() / 1000)}:F>` },
{ name: 'Ended At', value: `<t:${Math.floor(endTime.getTime() / 1000)}:F>` },
{ name: 'Shift Duration', value: formatDuration(endTime.getTime() - startedAt.getTime()) }
)
.setThumbnail(interaction.user.displayAvatarURL())