tictactoe(buttons)

Buttons does not update on interaction || Please help , if u need interactionCreate.js code i can share
 avatar
user_1137980406
javascript
2 years ago
8.7 kB
6
Indexable
Never
const { MessageButton, MessageActionRow } = require('discord.js')
module.exports = {
    name: 'tictactoe',
    aliases: ['xo'],
    description: 'Play Tic Tac Toe against someone or yourself',
      /**
     *
     * @param {Client} client
     * @param {Message} message
     * @param {String[]} args
     */
    run: async (client, message, args) => {
        let opponent = message.mentions.members.first() || message.guild.members.cache.get(args[0])
        if(!opponent) return message.channel.send("Please provide the user to challenge!")
        let fighters = [message.member.id, opponent.id].sort(() => (Math.random() > .5) ? 1 : -1)
        let Args = {
            user: 0,
            a1: {
                style: "SECONDARY",
                label: "➖",
                disabled: false
            },
            a2: {
                style: "SECONDARY",
                label: "➖",
                disabled: false
            },
            a3: {
                style: "SECONDARY",
                label: "➖",
                disabled: false
            },
            b1: {
                style: "SECONDARY",
                label: "➖",
                disabled: false
            },
            b2: {
                style: "SECONDARY",
                label: "➖",
                disabled: false
            },
            b3: {
                style: "SECONDARY",
                label: "➖",
                disabled: false
            },
            c1: {
                style: "SECONDARY",
                label: "➖",
                disabled: false
            },
            c2: {
                style: "SECONDARY",
                label: "➖",
                disabled: false
            },
            c3: {
                style: "SECONDARY",
                label: "➖",
                disabled: false
            }
        }
        
        const msg = await message.channel.send({content: `**TicTacToe** | ${message.mentions.users.first()}'s turn (⭕)`})
        tictactoe(msg)
        async function tictactoe(m) {
            Args.userid=fighters[Args.user]
            const won = {
                "⭕": false,
                "❌": false
            }
            if (Args.a1.label == "⭕" && Args.b1.label == "⭕" && Args.c1.label == "⭕") won["⭕"] = true
            if (Args.a2.label == "⭕" && Args.b2.label == "⭕" && Args.c2.label == "⭕") won["⭕"] = true
            if (Args.a3.label == "⭕" && Args.b3.label == "⭕" && Args.c3.label == "⭕") won["⭕"] = true
            if (Args.a1.label == "⭕" && Args.b2.label == "⭕" && Args.c3.label == "⭕") won["⭕"] = true
            if (Args.a3.label == "⭕" && Args.b2.label == "⭕" && Args.c1.label == "⭕") won["⭕"] = true
            if (Args.a1.label == "⭕" && Args.a2.label == "⭕" && Args.a3.label == "⭕") won["⭕"] = true
            if (Args.b1.label == "⭕" && Args.b2.label == "⭕" && Args.b3.label == "⭕") won["⭕"] = true
            if (Args.c1.label == "⭕" && Args.c2.label == "⭕" && Args.c3.label == "⭕") won["⭕"] = true
            if (won["⭕"] != false) return m.edit('⭕ won!')
            if (Args.a1.label == "❌" && Args.b1.label == "❌" && Args.c1.label == "❌") won["❌"] = true
            if (Args.a2.label == "❌" && Args.b2.label == "❌" && Args.c2.label == "❌") won["❌"] = true
            if (Args.a3.label == "❌" && Args.b3.label == "❌" && Args.c3.label == "❌") won["❌"] = true
            if (Args.a1.label == "❌" && Args.b2.label == "❌" && Args.c3.label == "❌") won["❌"] = true
            if (Args.a3.label == "❌" && Args.b2.label == "❌" && Args.c1.label == "❌") won["❌"] = true
            if (Args.a1.label == "❌" && Args.a2.label == "❌" && Args.a3.label == "❌") won["❌"] = true
            if (Args.b1.label == "❌" && Args.b2.label == "❌" && Args.b3.label == "❌") won["❌"] = true
            if (Args.c1.label == "❌" && Args.c2.label == "❌" && Args.c3.label == "❌") won["❌"] = true
            if (won["❌"] != false) return m.edit('❌ won!')
            
            const a = new MessageActionRow()
                .addComponents(
                    new MessageButton()
                    .setStyle("SECONDARY")
                    .setLabel("➖")
                    .setCustomId('a1')
                    .setDisabled(Args.a1.disabled)
                )
                .addComponents(
                    new MessageButton()
                    .setStyle("SECONDARY")
                    .setLabel("➖")
                    .setCustomId('a2')
                    .setDisabled(Args.a2.disabled)
                )
                .addComponents(
                    new MessageButton()
                    .setStyle("SECONDARY")
                    .setLabel("➖")
                    .setCustomId('a3')
                    .setDisabled(Args.a3.disabled)
                )

            const b = new MessageActionRow()
                .addComponents(
                    new MessageButton()
                    .setStyle("SECONDARY")
                    .setLabel("➖")
                    .setCustomId('b1')
                    .setDisabled(Args.b1.disabled)
                )
                .addComponents(
                    new MessageButton()
                    .setStyle("SECONDARY")
                    .setLabel("➖")
                    .setCustomId('b2')
                    .setDisabled(Args.b2.disabled)
                )
                .addComponents(
                    new MessageButton()
                    .setStyle("SECONDARY")
                    .setLabel("➖")
                    .setCustomId('b3')
                    .setDisabled(Args.b3.disabled)
                )

            const c = new MessageActionRow()
                .addComponents(
                    new MessageButton()
                    .setStyle("SECONDARY")
                    .setLabel("➖")
                    .setCustomId('c1')
                )
                .addComponents(
                    new MessageButton()
                    .setStyle("SECONDARY")
                    .setLabel("➖")
                    .setCustomId('c2')
                    .setDisabled(Args.c2.disabled)
                )
                .addComponents(
                    new MessageButton()
                    .setStyle("SECONDARY")
                    .setLabel("➖")
                    .setCustomId('c3')
                    .setDisabled(Args.c3.disabled)
                )

            m.edit({contents: [`**TicTacToe** | <@${message.author.id}>'s turn (${Args.user == 0 ? "⭕" : "❌"})`], components: [a, b, c]})
            const filter = (interaction) => interaction.clicker.user.id === Args.userid;
            const collector = message.channel.createMessageComponentCollector(filter, { max: 1, time: 30000 });

            collector.on('collect', (interaction) => {
                if (Args.user == 0) {
                    Args.user = 1
                    Args[(interaction).id] = {
                        style: "SUCCESS",
                        label: "⭕",
                        disabled: true
                    }
                } else {
                    Args.user = 0
                    Args[(interaction).id] = {
                        style: "DANGER",
                        label: "❌",
                        disabled: true
                    }
                }
                interaction.deferUpdate();
                const map = (obj, fun) =>
                    Object.entries(obj).reduce(
                        (prev, [key, value]) => ({
                            ...prev,
                            [key]: fun(key, value)
                        }),
                        {}
                    );
                const objectFilter = (obj, predicate) =>
                    Object.keys(obj)
                        .filter(key => predicate(obj[key]))
                        .reduce((res, key) => (res[key] = obj[key], res), {});
                let Brgs = objectFilter(map(Args, (_, fruit) => fruit.label == "➖"), num => num == true);
                if (Object.keys(Brgs).length == 0) return m.edit('It\'s a tie!')
                tictactoe(m)
            });
            collector.on('end', collected => {
                if (collected.size == 0) m.edit(`<@!${Args.userid}> didn\'t react in time! (30s)`)
            });
        }
    }
}