user blacklists

This commit is contained in:
Ashley Graves 2024-10-10 18:08:19 +02:00
parent fae08327b9
commit aca0882b21
7 changed files with 573 additions and 20 deletions

View file

@ -0,0 +1,91 @@
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 ?? "").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 })))
}
},
};