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_DIR'] = process.env['HOME'] + '/.config/chord/';
|
||||||
process.env['NODE_CONFIG_ENV'] = 'config';
|
process.env['NODE_CONFIG_ENV'] = 'config';
|
||||||
|
|
||||||
import blessed from 'blessed';
|
import axios from 'axios';import blessed from 'blessed';
|
||||||
import blessedContrib from 'blessed-contrib';
|
import blessedContrib from 'blessed-contrib';
|
||||||
import config from 'config';
|
import config from 'config';
|
||||||
|
|
||||||
import { Client } from './lib/client';
|
import { Client } from './lib/client';
|
||||||
|
|
||||||
|
let currentSelectedChannel = 0;
|
||||||
|
let currentSelectedGuild = 0;
|
||||||
|
|
||||||
const screen = blessed.screen({
|
const screen = blessed.screen({
|
||||||
smartCSR: true,
|
smartCSR: true,
|
||||||
title: 'chord',
|
title: 'chord',
|
||||||
|
@ -176,6 +179,33 @@ client.on('guild_create', (guild: Record<string, any>) => {
|
||||||
|
|
||||||
client.connect();
|
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', () => {
|
screen.key('S-q', () => {
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
});
|
});
|
||||||
|
|
|
@ -3,6 +3,7 @@ import os from 'os';
|
||||||
import WebSocket from 'ws';
|
import WebSocket from 'ws';
|
||||||
|
|
||||||
export class Client extends EventEmitter {
|
export class Client extends EventEmitter {
|
||||||
|
public guilds: Record<string, any>[];
|
||||||
public ws: WebSocket | null;
|
public ws: WebSocket | null;
|
||||||
|
|
||||||
private _config: object;
|
private _config: object;
|
||||||
|
@ -12,6 +13,7 @@ export class Client extends EventEmitter {
|
||||||
public constructor(token: string, config: object) {
|
public constructor(token: string, config: object) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
|
this.guilds = [];
|
||||||
this.ws = null;
|
this.ws = null;
|
||||||
|
|
||||||
this._config = config;
|
this._config = config;
|
||||||
|
@ -22,10 +24,6 @@ export class Client extends EventEmitter {
|
||||||
public async connect(): Promise<void> {
|
public async connect(): Promise<void> {
|
||||||
this.ws = new WebSocket('wss://gateway.discord.gg/');
|
this.ws = new WebSocket('wss://gateway.discord.gg/');
|
||||||
|
|
||||||
this.ws.on('error', (err) => {
|
|
||||||
throw err;
|
|
||||||
});
|
|
||||||
|
|
||||||
this.ws.on('close', (_code, reason) => {
|
this.ws.on('close', (_code, reason) => {
|
||||||
throw reason;
|
throw reason;
|
||||||
});
|
});
|
||||||
|
@ -35,8 +33,12 @@ export class Client extends EventEmitter {
|
||||||
|
|
||||||
if (objectData.op === 0) {
|
if (objectData.op === 0) {
|
||||||
if (objectData.t === 'GUILD_CREATE') {
|
if (objectData.t === 'GUILD_CREATE') {
|
||||||
|
this.guilds.push(objectData.d);
|
||||||
|
|
||||||
this.emit('guild_create', objectData.d);
|
this.emit('guild_create', objectData.d);
|
||||||
} else if (objectData.t === 'READY') {
|
} else if (objectData.t === 'READY') {
|
||||||
|
this.guilds = objectData.d.guilds;
|
||||||
|
|
||||||
this.emit('ready', objectData.d);
|
this.emit('ready', objectData.d);
|
||||||
}
|
}
|
||||||
} else if (objectData.op === 10) {
|
} else if (objectData.op === 10) {
|
||||||
|
|
Reference in a new issue