Using the Textual Python TUI module to recieve websocket messages

184 Views Asked by At

I'm trying to use the Textual module to make a GUI for this other python module called Discum to make a 3rd party Discord client that runs in the terminal.

I'm having difficulty hooking up the websocket gateway events to the Textual modules. I want to be able to update text and add widgets while the script is running, similar to this...

@bot.gateway.command
def discordEvent(res):
    if resp.event.message: # onMessageCreate
        msg = resp.parsed.auto()
        if (msg["channel_id"] == currentChannel):
            app.appendMessage(msg["content"]) # Adds a message widget with the content as the rich text

(this example does not work)

This is what I tried, but this does not work...

class CordApp(App):
    BINDINGS = []

    def compose(self) -> ComposeResult:
        """Create child widgets for the app."""
        # yield Header()
        label = yield Label("Logging in...", id="login-status")
        yield Footer()
        yield Container(id="message-cont")

    @bot.gateway.command
    def updateLoginStatus(rep):
        if resp.event.ready_supplemental: # onReady
            user = bot.gateway.session.user
            loginStatus = app.query_one("#login-status")
            loginStatus.update("Logged in as {}#{}".format(user['username'], user['discriminator']))

    def appendMessage(self, content):
        # msgWidget = yield 
        msgCont = self.query_one("#message-cont")
        msgCont.mount(MessageWidget(content)) # 'MessageWidget' is container that only contains a label and that argument is the label's text

app = CordApp()


### RUNTIME VARIABLES ###
currentChannel = "922797200814264361"

### DISCORD EVENTS ###
@bot.gateway.command
def helloworld(resp):
    if resp.event.ready_supplemental: # onReady
        user = bot.gateway.session.user
        app.updateLoginStatus("Logged in as {}#{}".format(user['username'], user['discriminator']))
        # print("Logged in as {}#{}".format(user['username'], user['discriminator']))
    if resp.event.message: # onMessageCreate
        msg = resp.parsed.auto()
        # print(msg['content'])
        if (msg["channel_id"] == currentChannel):
            app.appendMessage(msg["content"])

I run the Discum module in a background process so it listens to socket events while the Textual module runs in the forground, this method of running both modules syncronously could be the issue as they're running on differet references of the script- I'm not too sure...

### BEGIN INITILIZATION ###
Process(target=bot.gateway.run).start()
app.run()

I really like both of these modules, but I'm open to alternative modules if these dont really can't be used together

0

There are 0 best solutions below