Skip to main content

An Declarative UI Kit for discord.py

Project description

This package is extension of discord.ui.

You can build ui like SwiftUI.

Features

View System

You can build UI by View class.

View.body function returns what you want to show.

Combine System

You can use Combine like Swift’s Combine.

Example

from discord.ext.ui import Component, Button, View, ObservedObject, Published, Message
from discord.ext import commands
import discord
import os

client = commands.Bot("!")


class SampleViewModel(ObservedObject):
    def __init__(self):
        super().__init__()
        self.num = Published(0)

    def countup(self):
        self.num += 1

    def countdown(self):
        self.num -= 1


class SampleView(View):
    def __init__(self):
        super().__init__()
        self.viewModel = SampleViewModel()

    async def add_reaction(self):
        await self.discord_message.add_reaction("\U0001f44d")

    async def body(self):
        return Message(
            content=f"test! {self.viewModel.num}",
            component=Component(items=[
                [
                    Button("+1")
                        .on_click(lambda x: self.viewModel.countup())
                        .style(discord.ButtonStyle.blurple),

                    Button("-1")
                        .on_click(lambda x: self.viewModel.countdown())
                        .style(discord.ButtonStyle.blurple)
                ]
            ])
        )


@client.event
async def on_message(message):
    if message.content != "!test":
        return
    await SampleView().start(client, message.channel)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

discord_ext_ui-1.0.0-py3-none-any.whl (9.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page