By using the Discord API I made a simple script which shows messages received to a certain channel and allow to send a message to that channel. But the problem is input() is holding the program and the new messages do not print until Enter is pressed.
To overcome the problem I am thinking to make Irssi like a front end in which the top part shows messages and the button part allow us to input the messages.
Backend Code
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)
Curses
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)
I made a simple curses UI, but I could not add an input window in the button as in Irssi. I want any message received by the API to print in the Top part and an input button to send a message to the server. But I am stuck in here.
getch
has a timeout which can be turned off (nodelay) or set to a small value (timeout)