Archived
0
0
Fork 0

feat: basic channel and message rendering

This commit is contained in:
Daryl Ronningen 2021-12-29 20:59:50 -08:00
parent b6d83fea48
commit 7972a6a1d6
Signed by: Daryl Ronningen
GPG key ID: FD23F0C934A5EC6B
2 changed files with 37 additions and 5 deletions

View file

@ -1,12 +1,15 @@
process.env['NODE_CONFIG_DIR'] = process.env['HOME'] + '/.config/chord/';
process.env['NODE_CONFIG_ENV'] = 'config';
import blessed from 'blessed';
import axios from 'axios';import blessed from 'blessed';
import blessedContrib from 'blessed-contrib';
import config from 'config';
import { Client } from './lib/client';
let currentSelectedChannel = 0;
let currentSelectedGuild = 0;
const screen = blessed.screen({
smartCSR: true,
title: 'chord',
@ -176,6 +179,33 @@ client.on('guild_create', (guild: Record<string, any>) => {
client.connect();
serverListList.on('select', (_item, index) => {
channelListList.clearItems();
currentSelectedGuild = index;
for (const channel of client.guilds[index]!['channels']) {
if (channel.type === 0)
channelListList.addItem(channel.name);
}
screen.render();
});
channelListList.on('select', async (_item, index) => {
chatBoxList.clearItems();
currentSelectedChannel = index;
const getChannelMessages = await axios.get(`https://discord.com/api/v9/channels/${client.guilds[currentSelectedGuild]!['channels'][index].id}/messages`, { headers: { Authorization: config.get('token') } });
for (const message of getChannelMessages.data) {
chatBoxList.addItem(message.content);
}
screen.render();
});
screen.key('S-q', () => {
process.exit(0);
});

View file

@ -3,6 +3,7 @@ import os from 'os';
import WebSocket from 'ws';
export class Client extends EventEmitter {
public guilds: Record<string, any>[];
public ws: WebSocket | null;
private _config: object;
@ -12,6 +13,7 @@ export class Client extends EventEmitter {
public constructor(token: string, config: object) {
super();
this.guilds = [];
this.ws = null;
this._config = config;
@ -22,10 +24,6 @@ export class Client extends EventEmitter {
public async connect(): Promise<void> {
this.ws = new WebSocket('wss://gateway.discord.gg/');
this.ws.on('error', (err) => {
throw err;
});
this.ws.on('close', (_code, reason) => {
throw reason;
});
@ -35,8 +33,12 @@ export class Client extends EventEmitter {
if (objectData.op === 0) {
if (objectData.t === 'GUILD_CREATE') {
this.guilds.push(objectData.d);
this.emit('guild_create', objectData.d);
} else if (objectData.t === 'READY') {
this.guilds = objectData.d.guilds;
this.emit('ready', objectData.d);
}
} else if (objectData.op === 10) {