Skip to content

feat: add signal and buttonkit #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 26, 2023

Conversation

twlite
Copy link
Member

@twlite twlite commented Oct 25, 2023

This PR introduces ButtonKit (ButtonBuilder) and signals.

Example

output-commandkit.mp4

Code

import type { SlashCommandProps, CommandOptions, CommandData } from 'commandkit';
import { ButtonKit, createEffect, createSignal } from 'commandkit';

import { ButtonStyle, ActionRowBuilder, type ButtonInteraction } from 'discord.js';

export const data: CommandData = {
    name: 'count',
    description: 'Counter boi!',
};

function getButtons() {
    const dec = new ButtonKit()
        .setEmoji('➖')
        .setStyle(ButtonStyle.Primary)
        .setCustomId('decrement');

    const reset = new ButtonKit()
        .setEmoji('0️⃣')
        .setStyle(ButtonStyle.Primary)
        .setCustomId('reset');

    const inc = new ButtonKit()
        .setEmoji('➕')
        .setStyle(ButtonStyle.Primary)
        .setCustomId('increment');

    const trash = new ButtonKit()
        .setEmoji('🗑️')
        .setStyle(ButtonStyle.Danger)
        .setCustomId('trash');

    const row = new ActionRowBuilder<ButtonKit>()
        .addComponents(dec, reset, inc, trash);

    return { dec, reset, inc, trash, row };
}

export async function run({ interaction }: SlashCommandProps) {
    const [count, setCount, dispose] = createSignal(0);
    const { dec, reset, inc, trash, row } = getButtons();

    let inter: ButtonInteraction;

    const message = await interaction.reply({
        content: `Count is ${count()}`,
        components: [row],
        fetchReply: true,
    });

    createEffect(() => {
        const value = count();

        inter?.update(`Count is ${value}`);
    });

    dec.onClick((interaction) => {
        inter = interaction;
        setCount((prev) => prev - 1);
    }, { message });

    reset.onClick((interaction) => {
        inter = interaction;
        setCount(0);
    }, { message });

    inc.onClick((interaction) => {
        inter = interaction;
        setCount((prev) => prev + 1);
    }, { message });

    trash.onClick(async (interaction) => {
        const disposed = row.setComponents(
            row.components.map((button) => button.setDisabled(true).onClick(null)),
        );

        dispose();
        
        await interaction.update({
            content: 'Finished counting!',
            components: [disposed],
        });
    }, { message });
}

export const options: CommandOptions = {
    devOnly: true,
};

@netlify
Copy link

netlify bot commented Oct 25, 2023

Deploy Preview for commandkit ready!

Name Link
🔨 Latest commit ce59f5a
🔍 Latest deploy log https://app.netlify.com/sites/commandkit/deploys/6538b443e5c39b0008092f48
😎 Deploy Preview https://deploy-preview-21--commandkit.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@twlite twlite marked this pull request as ready for review October 25, 2023 05:23
@twlite twlite marked this pull request as draft October 25, 2023 05:25
@twlite twlite marked this pull request as ready for review October 25, 2023 05:28
@twlite twlite changed the title feat: add reactivity feat: add signal and buttonkit Oct 25, 2023
Copy link
Member

@notunderctrl notunderctrl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since I got exams for the week and this doesn't seem to include breaking changes, I'll approve this PR for the dev version.

@notunderctrl notunderctrl merged commit d57c462 into underctrl-io:master Oct 26, 2023
@twlite twlite deleted the reactivity-boi branch October 26, 2023 13:29
@twlite twlite restored the reactivity-boi branch October 26, 2023 13:29
@twlite twlite deleted the reactivity-boi branch October 26, 2023 13:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants