通过使用 Discord API,我制作了一个简单的脚本,显示接收到某个频道的消息并允许向该频道发送消息。Enter但问题是 input() 正在保存程序,并且在按下之前不会打印新消息。
为了克服这个问题,我正在考虑让 Irssi 成为一个前端,其中顶部显示消息,按钮部分允许我们输入消息。
后端代码
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 接收到的任何消息都打印在顶部,并希望有一个输入按钮来向服务器发送消息。但我被困在这里。