mirror of
https://git.arson.gg/lilith/discord-bot.git
synced 2025-12-05 03:34:49 +01:00
91 lines
3.1 KiB
JavaScript
91 lines
3.1 KiB
JavaScript
const { InteractionContextType, ApplicationIntegrationType, SlashCommandBuilder, EmbedBuilder } = require("discord.js");
|
|
const { format } = require("node:util");
|
|
const { knex } = require("../../db.js");
|
|
|
|
const data = new SlashCommandBuilder()
|
|
.setName("blacklist")
|
|
.setDescription("Manage your booru tag blacklist")
|
|
.addSubcommand((builder) =>
|
|
builder //
|
|
.setName("add")
|
|
.setDescription("Add a tag to your blacklist")
|
|
.addStringOption(builder =>
|
|
builder //
|
|
.setName("tag")
|
|
.setRequired(true)
|
|
.setDescription("Tag to blacklist")
|
|
))
|
|
.addSubcommand((builder) =>
|
|
builder //
|
|
.setName("remove")
|
|
.setDescription("Remove a tag from your blacklist")
|
|
.addStringOption(builder =>
|
|
builder //
|
|
.setName("tag")
|
|
.setRequired(true)
|
|
.setDescription("Tag to unblacklist")
|
|
.setAutocomplete(true)
|
|
))
|
|
.setContexts([
|
|
InteractionContextType.Guild,
|
|
InteractionContextType.BotDM,
|
|
InteractionContextType.PrivateChannel
|
|
])
|
|
.setIntegrationTypes([
|
|
ApplicationIntegrationType.GuildInstall,
|
|
ApplicationIntegrationType.UserInstall
|
|
]);
|
|
|
|
module.exports = {
|
|
data,
|
|
async execute(interaction) {
|
|
await interaction.deferReply({ ephemeral: true });
|
|
const command = interaction.options.getSubcommand(true);
|
|
const tag = interaction.options.getString("tag").replaceAll(" ", "_");
|
|
const blacklist = ((await knex.select("blacklist").from("blacklists").where("user", interaction.user.id).first()).blacklist ?? "").split(" ");
|
|
|
|
const data = {
|
|
user: interaction.user.id,
|
|
blacklist: [...blacklist].join(" ").trim()
|
|
}
|
|
|
|
switch (command) {
|
|
case "add":
|
|
if (blacklist.includes(tag)) {
|
|
await interaction.followUp("This tag is already blacklisted.");
|
|
return;
|
|
}
|
|
|
|
data.blacklist += " " + tag;
|
|
await interaction.followUp("Successfully blacklisted!");
|
|
break;
|
|
case "remove":
|
|
data.blacklist = data.blacklist.split(" ").filter(i => i != tag).join(" ").trim();
|
|
await interaction.followUp("Successfully removed!");
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
await knex.raw(format('%s ON CONFLICT (user) DO UPDATE SET %s',
|
|
knex("blacklists").insert(data).toString().toString(),
|
|
knex("blacklists").update(data).whereRaw(`'blacklists'.user = '${data.user}'`).toString().replace(/^update\s.*\sset\s/i, '')
|
|
));
|
|
},
|
|
async autocomplete(interaction) {
|
|
const value = interaction.options.getFocused() ?? "";
|
|
const command = interaction.options.getSubcommand(true);
|
|
if (command == "remove") {
|
|
const blacklist = ((await knex.select("blacklist").from("blacklists").where("user", interaction.user.id).first()) ?? { blacklist: '' }).blacklist.trim().split(" ");
|
|
const choices = [];
|
|
for (const tag of blacklist) {
|
|
if (value == "" || tag.startsWith(value.trim()))
|
|
choices.push(tag);
|
|
}
|
|
|
|
console.log(choices);
|
|
|
|
await interaction.respond(choices.map(choice => ({ name: choice, value: choice })))
|
|
}
|
|
},
|
|
};
|