aboutsummaryrefslogtreecommitdiff
path: root/bot/src/commands/quote.js
blob: eaac975107bb10f90170926ca4cf25f8c9361ce0 (plain) (blame)
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
import { SlashCommandBuilder, EmbedBuilder } from 'discord.js';
import { quote as quoteDB } from '../models/quote.js';
export default {
    data: new SlashCommandBuilder()
        .setName('quote')
        .setDescription('It gives you a quote.')
        .addNumberOption(option =>
            option
                .setName('id')
                .setDescription('Enter the quote ID to get a specific quote.')),
    async execute(interaction) {
        let quoteID = interaction.options.getNumber('id');

        if (!quoteID) {
            const quoteList = await quoteDB.findAll({ attributes: ['id'] });
            const random = crypto.getRandomValues(new Uint32Array(1));
            quoteID = quoteList[random[0] % quoteList.length].id;
        }

        const quote = await quoteDB.findOne({ where: { id: quoteID } });

        if (quote) {
            let userSubmitter = await interaction.client.users.fetch(quote.submitter);
            const quoteEmbed = new EmbedBuilder()
                .setAuthor({ name: quote.author, iconURL: quote.authorImage })
                .setDescription(quote.quote)
                .setColor('#1fd619')
                .setFooter({ text: `- ${quote.year}\nSubmitted by ${userSubmitter.username}` });

            return await interaction.reply({ embeds: [quoteEmbed] });
        } else {
            return await interaction.reply('Cannot find quote, specify the correct quote id.');
        }
    }
};