aboutsummaryrefslogtreecommitdiff
path: root/174bg/discord-bot/source/commands/uex.js
blob: 1a808948a4bf0c074b769d7ade5179af6b4a4206 (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { SlashCommandBuilder, MessageFlags } from "discord.js";
import { items } from "../controllers/uex.js";

export default {
    data: new SlashCommandBuilder()
        .setName("uex")
        .setDescription(
            "Commands for interacting with UEX.",
        )
        .addSubcommandGroup((subcommandGroup) =>
            subcommandGroup
                .setName("lookup")
                .setDescription("Lookup UEX information.")
                .addSubcommand((subcommand) =>
                    subcommand
                        .setName("items")
                        .setDescription("Perform UEX lookup for Star Citizen items, including ship components, weapons, and more.")
                        .addStringOption((option) =>
                            option
                                .setName("query")
                                .setDescription(
                                    "The item name or partial name to search for.",
                                )
                                .setRequired(true),
                        ),
                )
        ),
    execute: async (interaction) => {
        const subcommandGroup = interaction.options.getSubcommandGroup();
        const subcommand = interaction.options.getSubcommand();

        switch (subcommandGroup) {
            case "lookup":
                switch (subcommand) {
                    case "items":
                        await uex_lookup_items(interaction);
                        break;
                    default:
                        await interaction.reply({
                            content: "Unknown subcommand.",
                            ephemeral: true,
                        });
                        break;
                }
                break;
            default:
                await interaction.reply({
                    content: "Unknown subcommand group.",
                    ephemeral: true,
                });
                break;
        }
    },
};

async function uex_lookup_items(interaction) {
    const query = interaction.options.getString("query");

    await interaction.deferReply({ flags: MessageFlags.Ephemeral });

    const allItems = await items();

    const matchingItems = allItems.filter((item) =>
        item.name.toLowerCase().includes(query.toLowerCase()),
    );

    if (matchingItems.length === 0) {
        await interaction.editReply({
            content: `No items found matching "${query}".`,
        });
        return;
    }

    const itemList = matchingItems
        .map((item) => `- ${item.name} (ID: ${item.id})`)
        .join("\n");

    let replyContent = `Found ${matchingItems.length} item(s) matching "${query}":\n${itemList}`;

    if (replyContent.length > 2000) {
        await interaction.editReply({
            content: `Found ${matchingItems.length} item(s) matching "${query}", but the list is too long to display. Attached is a file containing the results.`,
            files: [
                {
                    attachment: Buffer.from(itemList, "utf-8"), name: `uex_items_${query}.txt`,
                },
            ],
        });
        return;
    } else {
        await interaction.editReply({
            content: replyContent,
        });
    }
}