Files
index.js
package.json
Code
2 443
index.js
const {
ActionRowBuilder,
ButtonBuilder,
ButtonStyle,
ChannelType,
Client,
EmbedBuilder,
Events,
GatewayIntentBits,
PermissionFlagsBits,
REST,
Routes,
SlashCommandBuilder
} = require('discord.js');
const fs = require('node:fs');
const path = require('node:path');
const token = process.env.DISCORD_TOKEN;
const clientId = process.env.CLIENT_ID;
const guildId = process.env.GUILD_ID;
const antiSpamEnabled = process.env.ENABLE_ANTI_SPAM !== 'false';
const blockInvites = process.env.BLOCK_DISCORD_INVITES === 'true';
const verificationAccountAgeMs = 72 * 60 * 60 * 1000;
const swearWordsPath = path.join(__dirname, 'swear-words.json');
const swearWords = new Set(
fs.existsSync(swearWordsPath)
? JSON.parse(fs.readFileSync(swearWordsPath, 'utf8')).map(word => String(word).toLowerCase())
: []
);
if (!token || !clientId) {
throw new Error('Set DISCORD_TOKEN and CLIENT_ID before starting the bot.');
}
const commands = [
new SlashCommandBuilder()
.setName('verify-panel')
.setDescription('Post a verification panel in a channel.')
.addChannelOption(option => option
.setName('channel')
.setDescription('Channel where the panel will be posted.')
.addChannelTypes(ChannelType.GuildText)
.setRequired(true))
.addRoleOption(option => option
.setName('role')
.setDescription('Role granted after verification.')
.setRequired(true)),
new SlashCommandBuilder()
.setName('ticket-panel')
.setDescription('Post a panel for opening private support tickets.')
.addChannelOption(option => option
.setName('channel')
.setDescription('Channel where the panel will be posted.')
.addChannelTypes(ChannelType.GuildText)
.setRequired(true))
.addRoleOption(option => option
.setName('support_role')
.setDescription('Optional role that can view and respond to tickets.')),
new SlashCommandBuilder()
.setName('mod')
.setDescription('Moderate a server member.')
.addSubcommand(subcommand => subcommand
.setName('ban')
.setDescription('Ban a member.')
.addUserOption(option => option.setName('user').setDescription('Member to ban.').setRequired(true))
.addStringOption(option => option.setName('reason').setDescription('Reason for the ban.')))
.addSubcommand(subcommand => subcommand
.setName('kick')
.setDescription('Kick a member.')
.addUserOption(option => option.setName('user').setDescription('Member to kick.').setRequired(true))
.addStringOption(option => option.setName('reason').setDescription('Reason for the kick.')))
.addSubcommand(subcommand => subcommand
.setName('timeout')
.setDescription('Timeout a member.')
.addUserOption(option => option.setName('user').setDescription('Member to timeout.').setRequired(true))
.addIntegerOption(option => option
.setName('minutes')
.setDescription('Timeout duration in minutes (1–40320).')
.setMinValue(1)
.setMaxValue(40320)
.setRequired(true))
.addStringOption(option => option.setName('reason').setDescription('Reason for the timeout.')))
.addSubcommand(subcommand => subcommand
.setName('untimeout')
.setDescription('Remove a member timeout.')
.addUserOption(option => option.setName('user').setDescription('Member to untimeout.').setRequired(true))
.addStringOption(option => option.setName('reason').setDescription('Reason for removing the timeout.'))),
new SlashCommandBuilder()
.setName('security')
.setDescription('Show this bot’s security filter settings.'),
new SlashCommandBuilder()
.setName('swear-words')
.setDescription('Manage words that trigger an automatic timeout.')
.addSubcommand(subcommand => subcommand
.setName('add')
.setDescription('Add a word or phrase to the filter.')
.addStringOption(option => option.setName('word').setDescription('Word or phrase to filter.').setRequired(true)))
.addSubcommand(subcommand => subcommand
.setName('remove')
.setDescription('Remove a word or phrase from the filter.')
.addStringOption(option => option.setName('word').setDescription('Word or phrase to remove.').setRequired(true)))
.addSubcommand(subcommand => subcommand
.setName('list')
.setDescription('Show the current filtered words.'))
];
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
]
});
const recentMessages = new Map();
const automodCooldowns = new Map();
client.once(Events.ClientReady, async readyClient => {
const rest = new REST({ version: '10' }).setToken(token);
const route = guildId
? Routes.applicationGuildCommands(clientId, guildId)
: Routes.applicationCommands(clientId);
try {
await rest.put(route, { body: commands.map(command => command.toJSON()) });
console.log(`Logged in as ${readyClient.user.tag}; slash commands registered.`);
} catch (error) {
console.error('Could not register slash commands:', error);
}
});
client.on(Events.InteractionCreate, async interaction => {
try {
if (interaction.isChatInputCommand()) {
await handleCommand(interaction);
} else if (interaction.isButton()) {
await handleButton(interaction);
}
} catch (error) {
console.error('Interaction failed:', error);
const message = 'That action failed. Check the bot permissions and try again.';
if (interaction.deferred || interaction.replied) {
await interaction.followUp({ content: message, ephemeral: true }).catch(() => {});
} else {
await interaction.reply({ content: message, ephemeral: true }).catch(() => {});
}
}
});
async function handleCommand(interaction) {
if (!interaction.inGuild()) {
await interaction.reply({ content: 'This command can only be used in a server.', ephemeral: true });
return;
}
if (interaction.commandName === 'verify-panel') {
if (!interaction.memberPermissions.has(PermissionFlagsBits.ManageGuild)) {
await interaction.reply({ content: 'You need Manage Server permission to do that.', ephemeral: true });
return;
}
const channel = interaction.options.getChannel('channel', true);
const role = interaction.options.getRole('role', true);
const embed = new EmbedBuilder()
.setColor(0x5865f2)
.setTitle('Server verification')
.setDescription('Click the button below to receive the verified role. Accounts must be at least 3 days old.');
const row = new ActionRowBuilder().addComponents(
new ButtonBuilder().setCustomId(`verify:${role.id}`).setLabel('Verify').setStyle(ButtonStyle.Success)
);
await channel.send({ embeds: [embed], components: [row] });
await interaction.reply({ content: `Verification panel posted in ${channel}.`, ephemeral: true });
return;
}
if (interaction.commandName === 'ticket-panel') {
if (!interaction.memberPermissions.has(PermissionFlagsBits.ManageGuild)) {
await interaction.reply({ content: 'You need Manage Server permission to do that.', ephemeral: true });
return;
}
const channel = interaction.options.getChannel('channel', true);
const supportRole = interaction.options.getRole('support_role');
const embed = new EmbedBuilder()
.setColor(0x5865f2)
.setTitle('Support tickets')
.setDescription('Click below to open a private support ticket.');
const row = new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId(`ticket_open:${supportRole?.id ?? 'none'}`)
.setLabel('Open a ticket')
.setStyle(ButtonStyle.Primary)
);
await channel.send({ embeds: [embed], components: [row] });
await interaction.reply({ content: `Ticket panel posted in ${channel}.`, ephemeral: true });
return;
}
if (interaction.commandName === 'mod') {
await handleModeration(interaction);
return;