feat: basic channel and message rendering
This commit is contained in:
parent
b6d83fea48
commit
7972a6a1d6
2 changed files with 37 additions and 5 deletions
32
src/index.ts
32
src/index.ts
|
@ -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);
|
||||
});
|
||||
|
|
|
@ -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) {
|
||||
|
|
Reference in a new issue