diff --git a/src/index.ts b/src/index.ts index a7fa233..c8a22bd 100644 --- a/src/index.ts +++ b/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) => { 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); }); diff --git a/src/lib/client.ts b/src/lib/client.ts index aaf9d29..901e9da 100644 --- a/src/lib/client.ts +++ b/src/lib/client.ts @@ -3,6 +3,7 @@ import os from 'os'; import WebSocket from 'ws'; export class Client extends EventEmitter { + public guilds: Record[]; 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 { 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) {