import { Command, sendMessage } from "lib/commandUtils";
import parseCommandArgs from "lib/parseCommandArgs";
import { redis } from "lib/redis";
import { streamerUsers } from "main";
import User from "user";

export default new Command({
	name: "addbot",
	aliases: ["addbot"],
	usertype: "streamer",
	disableable: false,
	execution: async (msg) => {
		const args = parseCommandArgs(msg.messageText);
		if (!args[0]) {
			await sendMessage("Please specify a target", msg.messageId);
			return;
		}
		const target = await User.initUsername(args[0].toLowerCase());
		if (!target) {
			await sendMessage(`Chatter ${args[0]} doesn't exist`, msg.messageId);
			return;
		}
		if (streamerUsers.includes(target.id)) {
			await sendMessage(
				`Cannot change bot status of qweribot managed user`,
				msg.messageId,
			);
			return;
		}
		const data = await redis.set(`user:${target.id}:bot`, "1");
		await target.clearVulnerable();
		if (data === "OK")
			await sendMessage(`${target.displayName} is now a bot`, msg.messageId);
		else
			await sendMessage(
				`${target.displayName} is already a bot`,
				msg.messageId,
			);
	},
});
