satanic-security/interactions/commands/serverconfig.js

112 lines
6.5 KiB
JavaScript

const Discord = require("discord.js");
const allowedChannelTypes = [Discord.ChannelType.GuildAnnouncement, Discord.ChannelType.GuildText, Discord.ChannelType.GuildVoice]
const execute = async (interaction, db, client) => {
if (!interaction.isCommand()) return;
if (!interaction.guild) return interaction.reply({ content: "This command can only be used in a server.", ephemeral: true });
await interaction.deferReply({ ephemeral: true }).catch(console.error);
var conf;
await db.get("SELECT * FROM guildConfigs WHERE guildId = ?", [interaction.guild.id], async (err, row) => {
if (err) {
console.error("Database error:", err);
return interaction.editReply({ content: "An error occurred while accessing the database.", ephemeral: true });
}
if (!row) {
db.run("INSERT INTO guildConfigs (guildId, mode) VALUES (?, ?)", [interaction.guild.id, "none"], function (err) {
if (err) {
console.error("Database error:", err);
returninteraction.editReply({ content: "An error occurred while initializing server configuration.", ephemeral: true });
}
conf = { guildId: interaction.guild.id, mode: "none" };
});
}
conf = row;
const subcommand = interaction.options.getSubcommand();
console.log(`Subcommand: ${subcommand}`);
switch (subcommand) {
case "mode": // Activate or deactivate automatic bad-actor management
const newMode = interaction.options.getString("mode");
if (!["none", "warn", "kick", "ban"].includes(newMode)) {
return interaction.editReply({ content: "Invalid mode selected. Choose from: none, warn, kick, ban.", ephemeral: true });
}
db.run("UPDATE guildConfigs SET mode = ? WHERE guildId = ?", [newMode, interaction.guild.id], function (err) {
if (err) {
console.error("Database error:", err);
return interaction.editReply({ content: "An error occurred while updating the mode.", ephemeral: true });
}
conf.mode = newMode;
return interaction.editReply({ content: `Bad actor management mode set to **${newMode}**.`, ephemeral: true });
});
break;
case "logchannel": // Set the log channel for bad actor actions
const logChannel = interaction.options.getChannel("channel");
if (!logChannel || !allowedChannelTypes.includes(logChannel.type)) {
return interaction.editReply({ content: "Please select a valid text channel for logging.", ephemeral: true });
}
db.run("UPDATE guildConfigs SET logChannelId = ? WHERE guildId = ?", [logChannel.id, interaction.guild.id], function (err) {
if (err) {
console.error("Database error:", err);
return interaction.editReply({ content: "An error occurred while updating the log channel.", ephemeral: true });
}
conf.logChannelId = logChannel.id;
return interaction.editReply({ content: `Log channel set to <#${logChannel.id}>.`, ephemeral: true });
});
break;
case "view": // View current server configuration
if (!conf) {
return interaction.editReply({ content: "No configuration found for this server.", ephemeral: true });
}
const mode = conf.mode || "none";
const logChannelId = conf.logChannelId ? `<#${conf.logChannelId}>` : "Not set";
const embed = new Discord.EmbedBuilder()
.setTitle("Server Configuration")
.addFields(
{ name: "Bad Actor Management Mode", value: mode, inline: true },
{ name: "Log Channel", value: logChannelId, inline: true }
)
.setColor("#0099ff")
.setTimestamp();
return interaction.editReply({ embeds: [embed], ephemeral: true });
break;
case "fullscan": // Perform a full scan of the server for bad actors
if (!conf.logChannelId) {
return interaction.editReply({ content: "Log channel is not set. Please set it using `/serverconfig logchannel`.", ephemeral: true });
}
const logChannelFullScan = await client.channels.fetch(conf.logChannelId).catch(console.error);
if (!logChannelFullScan || !allowedChannelTypes.includes(logChannelFullScan.type)) {
return interaction.editReply({ content: "Log channel is not valid or not found.", ephemeral: true });
}
const members = await interaction.guild.members.fetch();
const badActors = [];
for (const member of members.values()) {
if (member.user.bot) continue; // Ignore bots
const actor = await new Promise((resolve, reject) => {
db.get("SELECT * FROM badActors WHERE id = ?", [member.user.id], (err, row) => {
if (err) {
console.error("Database error:", err);
return resolve(null);
}
resolve(row);
});
});
if (actor) {
badActors.push(actor);
}
}
if (badActors.length === 0) {
return interaction.editReply({ content: "No bad actors found in the server.", ephemeral: true });
}
let message = `Bad Actor Scan Results\nFound ${badActors.length} bad actors in the server.\n\n`;
badActors.forEach(actor => {
message += `ID: ${actor.id}\nUser: <@${actor.id}>\nReported by: <@${actor.reportedBy}>\nDate: <t:${Math.floor(actor.timestamp / 1000)}:F>\nComment: ${actor.comment}\n\n`;
});
logChannelFullScan.send({ content: message }).catch(console.error);
return interaction.editReply({ content: "Full scan completed. Check the log channel for results.", ephemeral: true });
break;
}
});
}
module.exports = { execute };