54 lines
2.5 KiB
JavaScript
54 lines
2.5 KiB
JavaScript
const { SlashCommandBuilder, PermissionFlagsBits } = require('discord.js');
|
|
const { readDb, writeDb } = require('../../backend/db');
|
|
|
|
module.exports = {
|
|
name: 'create-invite',
|
|
description: 'Create a Discord invite with options (channel optional, maxAge seconds, maxUses, temporary).',
|
|
enabled: true,
|
|
builder: new SlashCommandBuilder()
|
|
.setName('create-invite')
|
|
.setDescription('Create a Discord invite with options (channel optional, maxAge seconds, maxUses, temporary).')
|
|
.addChannelOption(opt => opt.setName('channel').setDescription('Channel to create invite in').setRequired(false))
|
|
.addIntegerOption(opt => opt.setName('maxage').setDescription('Duration in seconds (0 means never expire)').setRequired(false))
|
|
.addIntegerOption(opt => opt.setName('maxuses').setDescription('Number of uses allowed (0 means unlimited)').setRequired(false))
|
|
.addBooleanOption(opt => opt.setName('temporary').setDescription('Temporary membership?').setRequired(false))
|
|
.setDefaultMemberPermissions(PermissionFlagsBits.Administrator),
|
|
async execute(interaction) {
|
|
try {
|
|
const channel = interaction.options.getChannel('channel');
|
|
const maxAge = interaction.options.getInteger('maxage') || 0;
|
|
const maxUses = interaction.options.getInteger('maxuses') || 0;
|
|
const temporary = interaction.options.getBoolean('temporary') || false;
|
|
|
|
const targetChannel = channel || interaction.guild.channels.cache.find(c => c.type === 0);
|
|
if (!targetChannel) {
|
|
await interaction.reply({ content: 'No valid channel found to create an invite.', ephemeral: true });
|
|
return;
|
|
}
|
|
|
|
const invite = await targetChannel.createInvite({ maxAge, maxUses, temporary, unique: true });
|
|
|
|
const api = require('../api');
|
|
const item = {
|
|
code: invite.code,
|
|
url: invite.url,
|
|
channel_id: targetChannel.id,
|
|
created_at: new Date().toISOString(),
|
|
max_uses: invite.maxUses || maxUses || 0,
|
|
max_age: invite.maxAge || maxAge || 0,
|
|
temporary: !!invite.temporary,
|
|
};
|
|
try {
|
|
await api.addInvite(interaction.guildId, { channelId: targetChannel.id, maxAge, maxUses, temporary });
|
|
} catch (e) {
|
|
console.error('Error saving invite to backend:', e);
|
|
}
|
|
|
|
await interaction.reply({ content: `Invite created: ${invite.url}`, ephemeral: true });
|
|
} catch (error) {
|
|
console.error('Error in create-invite:', error);
|
|
await interaction.reply({ content: 'Failed to create invite.', ephemeral: true });
|
|
}
|
|
},
|
|
};
|