使Irssi像使用curses或其他可用工具在Python中成为文本前端

问题描述 投票:0回答:1

通过使用Discord API,我编写了一个简单的脚本,该脚本显示了接收到某个频道的消息,并允许将消息发送到该频道。但是问题是input()保留了该程序,并且直到按下Enter时才显示新消息。

为了克服这个问题,我正在考虑使Irssi像前端一样,其中顶部显示消息,而按钮部分允许我们输入消息。

Irsii text frontend

后端代码

from discord.ext import commands


channel_id = discord_channel_id
bot_token = bot_token

class MyClient(discord.Client):

    async def on_ready(self):
        global channel, msg
        channel = discord.Client.get_channel(self, id=channel_id) 
        print(f'You are Connected on {channel}')
        while True:
            # Send message
            msg = input('[You] >> ')
            await channel.send(msg)

    async def on_message(self, message):           
            # Don't respond to ourselves
            if message.author == self.user:
                return

            if message.content:
                global received
                received = message.content

client = MyClient()


client.run(bot_token)

诅咒

import time
import curses

def main(stdscr):
    status = 'connecting'
    server = 'connecting'
    channel = 'connecting'
    online = 'connecting'
    key = 0

    stdscr.clear()
    stdscr.refresh()

    # Disable cursor blinking
    curses.curs_set(0)

    while (key != ord('~')):

        # Initialization
        stdscr.clear()
        height, width = stdscr.getmaxyx()
        # Status Bar
        stdscr.addstr(f"Status: {status}    Server: {server}    Channel: {channel}   Online: {online}", curses.A_REVERSE)
        stdscr.chgat(-1, curses.A_REVERSE)


        # Update the screen
        stdscr.refresh()

        key = stdscr.getch()


curses.wrapper(main)

我制作了一个简单的curses UI,但是无法像Irssi一样在按钮中添加输入窗口。我希望API收到的任何消息都打印在顶部,并希望有一个输入按钮将消息发送到服务器。但是我被困在这里。

python python-3.x discord curses irssi
1个回答
0
投票

getch的超时可以关闭(nodelay)或设置为较小的值(timeout

© www.soinside.com 2019 - 2024. All rights reserved.