import { getRepository } from "typeorm";
import { DeadChatReply } from "@shared/db/entity/DeadChatReply";
import { Message } from "discord.js";
import { Event, BotEventData, Plugin } from "src/model/plugin";

const triggers = [
    "dead server",
    "dead chat",
    "ded chat",
    "ded server"
];

@Plugin
export class DeadChat {
    @Event("message")
    async onMessage(data: BotEventData, msg: Message): Promise<void> {
        if (data.actionsDone)
            return;
        
        const lowerContent = msg.cleanContent.trim().toLowerCase();

        if (!triggers.some(s => lowerContent.includes(s)))
            return;

        const repo = getRepository(DeadChatReply);

        const reply = await repo.query(`select message
                                        from dead_chat_reply
                                        order by random()
                                        limit 1`) as DeadChatReply[];

        if (reply.length == 0)
            return;

        msg.channel.send(reply[0].message);
        data.actionsDone = true;
    }
}