Files
SpigotWatch/commands/setchannel.js
2026-02-25 18:51:08 +01:00

77 lines
2.3 KiB
JavaScript

import { EmbedBuilder, PermissionsBitField, SlashCommandBuilder } from "discord.js";
import fs from "fs/promises";
import { t, getLang } from "../util/i18n.js";
export default {
name: "setchannel",
description: "Ändert den Update-Kanal für eine beobachtete Ressource",
aliases: [],
guild: ["all"],
nsfw: false,
user_permissions: [PermissionsBitField.Flags.Administrator],
bot_permissions: [],
args_required: 2,
args_usage: "[ressourcen_id] [#kanal] Beispiel: vn!setchannel 72678 #updates",
cooldown: 5,
data: new SlashCommandBuilder()
.setName("setchannel")
.setDescription("Changes the update channel for a watched resource")
.addStringOption((opt) =>
opt.setName("ressourcen_id").setDescription("Spiget Ressourcen-ID").setRequired(true)
)
.addChannelOption((opt) =>
opt.setName("kanal").setDescription("Neuer Update-Kanal").setRequired(true)
),
async execute(client, ctx, args) {
const guildID = ctx.guild.id;
const filePath = `./serverdata/${guildID}.json`;
let saveData;
try {
const raw = await fs.readFile(filePath, "utf8");
saveData = JSON.parse(raw);
} catch {
return ctx.reply("Dieser Server hat keine beobachteten Ressourcen.");
}
const lang = getLang(saveData);
const resourceID = ctx.isSlash
? ctx.interaction.options.getString("ressourcen_id")
: args[0];
const channel = ctx.isSlash
? ctx.interaction.options.getChannel("kanal")
: ctx.mentions?.channels?.first();
if (!channel) {
return ctx.reply(t(lang, "add.invalidChannel"));
}
const watched = saveData.watchedResources.find(
(r) => String(r.resourceID) === String(resourceID)
);
if (!watched) {
return ctx.reply(t(lang, "setchannel.notWatched", { id: resourceID }));
}
watched.channelID = channel.id;
try {
await fs.writeFile(filePath, JSON.stringify(saveData, null, 2));
} catch (e) {
client.logger.error(e);
return ctx.reply(t(lang, "error.saveFailed"));
}
const name = watched.resourceName ?? resourceID;
const embed = new EmbedBuilder()
.setColor(ctx.guild.members.me.displayHexColor)
.setDescription(t(lang, "setchannel.success", { name, channelID: channel.id }));
return ctx.reply({ embeds: [embed] });
},
};