263 lines
13 KiB
JavaScript
263 lines
13 KiB
JavaScript
// List management (create, delete, and transfer)
|
|
const colors = require("colors")
|
|
const crypto = require("crypto");
|
|
|
|
module.exports = async (interaction, db) => {
|
|
if (interaction.commandName !== 'list') return;
|
|
|
|
try {
|
|
switch (interaction.options.getSubcommand()) {
|
|
case 'create':
|
|
{
|
|
|
|
const id = crypto.randomBytes(16).toString("hex");
|
|
const name = interaction.options.getString("name");
|
|
|
|
db.run(
|
|
"INSERT INTO lists (id, name, owner_id) VALUES (?, ?, ?)",
|
|
[id, name, interaction.user.id],
|
|
async function (err) {
|
|
if (err) {
|
|
console.error(err);
|
|
return interaction.reply({
|
|
content: "❌ Failed to create ban list.",
|
|
ephemeral: true
|
|
});
|
|
}
|
|
|
|
await interaction.reply({
|
|
content: `✅ Ban list **${name}** created with ID \`${id}\`.`,
|
|
ephemeral: true
|
|
});
|
|
}
|
|
);
|
|
|
|
}
|
|
break;
|
|
case 'delete':
|
|
{
|
|
const listId = interaction.options.getString('list');
|
|
|
|
// Verify ownership
|
|
db.get("SELECT * FROM lists WHERE id = ?", [listId], async (err, list) => {
|
|
if (err) {
|
|
console.error(err);
|
|
await interaction.reply({ content: `❌ Database error occurred.`, ephemeral: true });
|
|
return;
|
|
}
|
|
if (!list) {
|
|
await interaction.reply({ content: `❌ Ban list with ID \`${listId}\` does not exist.`, ephemeral: true });
|
|
return;
|
|
}
|
|
if (list.owner_id !== interaction.user.id) {
|
|
await interaction.reply({ content: `❌ You do not own ban list **${list.name}**.`, ephemeral: true });
|
|
return;
|
|
}
|
|
|
|
// Delete the list
|
|
db.run("DELETE FROM lists WHERE id = ?", [listId], async (err) => {
|
|
if (err) {
|
|
console.error(err);
|
|
await interaction.reply({ content: `❌ Failed to delete ban list.`, ephemeral: true });
|
|
return;
|
|
}
|
|
await interaction.reply({ content: `✅ Ban list **${list.name}** has been deleted.`, ephemeral: true });
|
|
});
|
|
});
|
|
}
|
|
break;
|
|
case 'transfer':
|
|
{
|
|
const listId = interaction.options.getString('list');
|
|
const newOwner = interaction.options.getUser('to');
|
|
|
|
if (!newOwner) {
|
|
await interaction.reply({ content: '❌ Target user not found.', ephemeral: true });
|
|
return;
|
|
}
|
|
|
|
// Verify ownership
|
|
db.get("SELECT * FROM lists WHERE id = ?", [listId], async (err, list) => {
|
|
if (err) {
|
|
console.error(err);
|
|
await interaction.reply({ content: `❌ Database error occurred.`, ephemeral: true });
|
|
return;
|
|
}
|
|
if (!list) {
|
|
await interaction.reply({ content: `❌ Ban list with ID \`${listId}\` does not exist.`, ephemeral: true });
|
|
return;
|
|
}
|
|
if (list.owner_id !== interaction.user.id) {
|
|
await interaction.reply({ content: `❌ You do not own ban list **${list.name}**.`, ephemeral: true });
|
|
return;
|
|
}
|
|
|
|
// Transfer ownership
|
|
db.run("UPDATE lists SET owner_id = ? WHERE id = ?", [newOwner.id, listId], async (err) => {
|
|
if (err) {
|
|
console.error(err);
|
|
await interaction.reply({ content: `❌ Failed to transfer ownership.`, ephemeral: true });
|
|
return;
|
|
}
|
|
await interaction.reply({ content: `✅ Ban list **${list.name}** ownership has been transferred to ${newOwner.tag}.`, ephemeral: true });
|
|
});
|
|
});
|
|
}
|
|
break;
|
|
case "import":
|
|
{
|
|
const importBans = (list) => {
|
|
// Fetch bans from the guild
|
|
interaction.guild.bans.fetch().then(bans => {
|
|
let importedCount = 0;
|
|
bans.forEach(async ban => {
|
|
console.log(`Importing ban for user ${ban.user.id} into list ${list.id}`);
|
|
await db.run("INSERT OR IGNORE INTO bans (user_id, list_id, reason, banned_by) VALUES (?, ?, ?, ?)", [ban.user.id, list.id, ban.reason || '', "System"], (err) => {
|
|
if (err) {
|
|
console.error(err);
|
|
} else {
|
|
importedCount++;
|
|
}
|
|
});
|
|
});
|
|
interaction.reply({ content: `✅ Imported ${importedCount} bans from this server into ban list **${list.name}**.`, ephemeral: true });
|
|
}).catch(err => {
|
|
console.error(err);
|
|
interaction.reply({ content: '❌ Failed to fetch bans from this server.', ephemeral: true });
|
|
});
|
|
};
|
|
const listId = interaction.options.getString('list');
|
|
|
|
// Verify ownership or guild has access (either works)
|
|
db.get("SELECT * FROM lists WHERE id = ?", [listId], async (err, list) => {
|
|
if (err) {
|
|
console.error(err);
|
|
await interaction.reply({ content: `❌ Database error occurred.`, ephemeral: true });
|
|
return;
|
|
}
|
|
if (!list) {
|
|
await interaction.reply({ content: `❌ Ban list with ID \`${listId}\` does not exist.`, ephemeral: true });
|
|
return;
|
|
}
|
|
// Check ownership
|
|
if (list.owner_id !== interaction.user.id) {
|
|
// Check if guild has access in perms table (guild will exist here if it has perms to write)
|
|
db.get("SELECT * FROM list_permissions WHERE list_id = ? AND guild_id = ?", [listId, interaction.guild.id], async (err, perm) => {
|
|
if (err) {
|
|
console.error(err);
|
|
await interaction.reply({ content: `❌ Database error occurred.`, ephemeral: true });
|
|
return;
|
|
}
|
|
if (!perm) {
|
|
await interaction.reply({ content: `❌ You do not own ban list **${list.name}** and this server does not have permission to modify it.`, ephemeral: true });
|
|
return;
|
|
}
|
|
// Guild has permission, proceed
|
|
importBans(list);
|
|
});
|
|
} else {
|
|
// User owns the list, proceed
|
|
importBans(list);
|
|
}
|
|
});
|
|
|
|
}
|
|
break;
|
|
|
|
case "allowguild":
|
|
{
|
|
const listId = interaction.options.getString('list');
|
|
const guildId = interaction.options.getString('guild_id') || interaction.guild.id;
|
|
|
|
// Verify ownership
|
|
db.get("SELECT * FROM lists WHERE id = ?", [listId], async (err, list) => {
|
|
if (err) {
|
|
console.error(err);
|
|
await interaction.reply({ content: `❌ Database error occurred.`, ephemeral: true });
|
|
return;
|
|
}
|
|
if (!list) {
|
|
await interaction.reply({ content: `❌ Ban list with ID \`${listId}\` does not exist.`, ephemeral: true });
|
|
return;
|
|
}
|
|
if (list.owner_id !== interaction.user.id) {
|
|
await interaction.reply({ content: `❌ You do not own ban list **${list.name}**.`, ephemeral: true });
|
|
return;
|
|
}
|
|
|
|
// Grant
|
|
db.run("INSERT OR IGNORE INTO perms (list_id, entity_id, entity_type) VALUES (?, ?, 'guild')", [listId, guildId], async (err) => {
|
|
if (err) {
|
|
console.error(err);
|
|
await interaction.reply({ content: `❌ Failed to allow guild.`, ephemeral: true });
|
|
return;
|
|
}
|
|
await interaction.reply({ content: `✅ Guild \`${guildId}\` has been allowed to use ban list **${list.name}**.`, ephemeral: true });
|
|
});
|
|
});
|
|
}
|
|
break;
|
|
|
|
case "revokeguild":
|
|
{
|
|
const listId = interaction.options.getString('list');
|
|
const guildId = interaction.options.getString('guild_id') || interaction.guild.id;
|
|
// Verify ownership
|
|
db.get("SELECT * FROM lists WHERE id = ?", [listId], async (err, list) => {
|
|
if (err) {
|
|
console.error(err);
|
|
await interaction.reply({ content: `❌ Database error occurred.`, ephemeral: true });
|
|
return;
|
|
}
|
|
if (!list) {
|
|
await interaction.reply({ content: `❌ Ban list with ID \`${listId}\` does not exist.`, ephemeral: true });
|
|
return;
|
|
}
|
|
if (list.owner_id !== interaction.user.id) {
|
|
await interaction.reply({ content: `❌ You do not own ban list **${list.name}**.`, ephemeral: true });
|
|
return;
|
|
}
|
|
|
|
// Revoke
|
|
db.run("DELETE FROM perms WHERE list_id = ? AND guild_id = ?", [listId, guildId], async (err) => {
|
|
if (err) {
|
|
console.error(err);
|
|
await interaction.reply({ content: `❌ Failed to revoke guild.`, ephemeral: true });
|
|
return;
|
|
}
|
|
await interaction.reply({ content: `✅ Guild \`${guildId}\` has been revoked access to ban list **${list.name}**.`, ephemeral: true });
|
|
});
|
|
});
|
|
}
|
|
break;
|
|
case "view":
|
|
{
|
|
// Fetch all lists owned by the user
|
|
db.all("SELECT * FROM lists WHERE owner_id = ?", [interaction.user.id], async (err, lists) => {
|
|
if (err) {
|
|
console.error(err);
|
|
await interaction.reply({ content: `❌ Database error occurred.`, ephemeral: true });
|
|
return;
|
|
}
|
|
if (lists.length === 0) {
|
|
await interaction.reply({ content: '❌ You do not own any ban lists.', ephemeral: true });
|
|
return;
|
|
}
|
|
|
|
let response = '📋 **Your Ban Lists:**\n';
|
|
lists.forEach(list => {
|
|
response += `• **${list.name}** (ID: \`${list.id}\`)\n`;
|
|
});
|
|
|
|
await interaction.reply({ content: response, ephemeral: true });
|
|
});
|
|
}
|
|
break;
|
|
default:
|
|
await interaction.reply({ content: '❌ Unknown subcommand.', ephemeral: true });
|
|
}
|
|
} catch (error) {
|
|
console.error(`${colors.red('[ERROR]')} Error handling list command:`, error);
|
|
await interaction.reply({ content: '❌ An error occurred while processing your request.', ephemeral: true });
|
|
}
|
|
}; |