All files / src bot.ts

32.39% Statements 23/71
100% Branches 0/0
100% Functions 0/0
32.39% Lines 23/71

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 721x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x       1x 1x 1x 1x 1x 1x     1x 1x 1x                                                                                     1x 1x  
import { Client, GatewayIntentBits } from 'discord.js';
 
import dataSource from "./data-source";
import { Prompt } from "./entity/prompt";
import { generate } from './generate';
 
const defaultNegative = "text, watermark, advertisement, low quality, low resolution, low calorie, low effort";
const defaultWorkflow = "sc";
 
// Connect to database
dataSource.initialize().then(() => {
    console.log("Database: connected");
}).catch((err) => {
    console.log("Database: an error occurred. " + err);
    throw new Error("database connection failed");
});
 
// Create a new Discord client instance
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
 
// When the client is ready, run this code (only once)
client.once('ready', () => {
    console.log('Ready.');
});
 
// Respond to commands
client.on('interactionCreate', async (interaction) => {
    if (!interaction.isChatInputCommand()) return;

    const { commandName } = interaction;

    if (commandName === 'txt2img') {
        const prompt = new Prompt();
        prompt.createdAt = new Date();
        prompt.updatedAt = new Date();
        prompt.mode = "txt2img";

        let extra = '';

        prompt.prompt = interaction.options.getString("prompt");
        if (prompt.prompt == null) return;

        prompt.negativePrompt = interaction.options.getString("negative_prompt");
        if (prompt.negativePrompt == null || prompt.negativePrompt === "")
            prompt.negativePrompt = defaultNegative;
        else
            extra = `~~${prompt.negativePrompt}~~\n`;

        prompt.workflow = interaction.options.getString("workflow");
        if (prompt.workflow == null || prompt.workflow === "")
            prompt.workflow = defaultWorkflow;

        prompt.batchSize = interaction.options.getInteger("batch_size");
        if (prompt.batchSize == null || prompt.batchSize < 1)
            prompt.batchSize = 1;

        await interaction.deferReply();

        await generate(prompt).then(async (files) => {
            const reply = `__**${prompt.prompt}**__\n${extra}${files.length}x ${prompt.workflow} in ${prompt.runtime}s`;
            await interaction.editReply({ content: reply, files });
        })
        .catch(async (err) => {
            console.log(err);
            const reply = `Error occurred while generating prompt: ${prompt.prompt}\n\`\`\`${err}\`\`\``;
            await interaction.editReply({ content: reply });
        });
    }
});
 
export { client };