import { SlashCommandBuilder, MessageFlags } from "discord.js"
import type { ChatInputCommandInteraction } from "discord.js"
import { t } from "@/utils/i18n"

export const data = new SlashCommandBuilder()
	.setName("spam")
	.setDescription("Spam a user with a message")
	.setDescriptionLocalizations({ fr: "Spammer un utilisateur avec un message" })
	.addUserOption(option => option
		.setName("user")
		.setDescription("Target user")
		.setNameLocalizations({ fr: "utilisateur" })
		.setDescriptionLocalizations({ fr: "Utilisateur cible" })
		.setRequired(true)
	)
	.addStringOption(option => option
		.setName("message")
		.setDescription("Message to spam")
		.setDescriptionLocalizations({ fr: "Message à spammer" })
		.setRequired(true)
	)
	.addIntegerOption(option => option
		.setName("count")
		.setDescription("Number of times to spam")
		.setNameLocalizations({ fr: "nombre" })
		.setDescriptionLocalizations({ fr: "Nombre de fois à spammer" })
		.setRequired(true)
		.setMinValue(1)
		.setMaxValue(100)
	)

export async function execute(interaction: ChatInputCommandInteraction) {
	const user = interaction.options.getUser("user", true)
	const string = interaction.options.getString("message", true)
	const integer = interaction.options.getInteger("count", true)

	await interaction.reply({ content: t(interaction.locale, "salonpostam.spam.started"), flags: MessageFlags.Ephemeral })
	let i = 0
	function myLoop() {
		setTimeout(() => {
			user.send(string).catch(console.error)
			i++
			if (i < integer) myLoop()
		}, 1000)
	}
	myLoop()
}
