Skip to content

Commit

Permalink
✨ Added live messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ZickZenni committed Sep 5, 2024
1 parent c211aae commit 301be74
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 25 deletions.
16 changes: 15 additions & 1 deletion src/discord/core/client.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { TypedEmitter } from 'tiny-typed-emitter';
import { WebSocket } from 'ws';
import { Gateway } from '../ws/gateway';
import { GatewayReadyDispatchData, GatewaySocketEvent } from '../ws/types';
import {
GatewayDispatchEvents,
GatewayReadyDispatchData,
GatewaySocketEvent,
} from '../ws/types';
import { debug, setDebugging } from './logger';
import { GuildManager } from '../managers/GuildManager';

Check warning on line 10 in src/discord/core/client.ts

View workflow job for this annotation

GitHub Actions / test (macos-latest)

Dependency cycle detected

Check warning on line 10 in src/discord/core/client.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest)

Dependency cycle detected

Check warning on line 10 in src/discord/core/client.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

Dependency cycle detected
import { ChannelManager } from '../managers/ChannelManager';

Check warning on line 11 in src/discord/core/client.ts

View workflow job for this annotation

GitHub Actions / test (macos-latest)

Dependency cycle detected

Check warning on line 11 in src/discord/core/client.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest)

Dependency cycle detected

Check warning on line 11 in src/discord/core/client.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

Dependency cycle detected
import MainGuild from '../structures/guild/MainGuild';
import MainChannel from '../structures/channel/MainChannel';

Check warning on line 13 in src/discord/core/client.ts

View workflow job for this annotation

GitHub Actions / test (macos-latest)

Dependency cycle detected

Check warning on line 13 in src/discord/core/client.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest)

Dependency cycle detected

Check warning on line 13 in src/discord/core/client.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

Dependency cycle detected
import MainUser from '../structures/user/MainUser';
import { Message } from '../structures/Message';

export interface ClientEvents {
ready: () => void;
Expand Down Expand Up @@ -70,6 +75,15 @@ export class Client extends TypedEmitter<ClientEvents> {
);
this.emit('ready');
}

if (event.event === GatewayDispatchEvents.MessageCreate) {
const message = event.data as Message;
const channel = this.channels.cache.get(message.channel_id);

if (channel !== undefined) {
channel.messages.push(message);
}
}
this.emit('dispatch', event);
});
this.guilds = new GuildManager(this);
Expand Down
1 change: 1 addition & 0 deletions src/discord/structures/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export interface Message {
timestamp: string;
id: Snowflake;
author: IUserData;
channel_id: Snowflake;
}
10 changes: 8 additions & 2 deletions src/main/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import fs from 'fs';
import path from 'path';
import logger, { Logger } from '../common/log/logger';
import { Client } from '../discord/core/client';
import { GatewaySocketEvent } from '../discord/ws/types';
import { registerHandler, registerListener } from './ipc';
import { GatewayDispatchEvents, GatewaySocketEvent } from '../discord/ws/types';
import { registerHandler, registerListener, sendToRenderer } from './ipc';
import { CreateMessageOptions } from '../discord/structures/channel/BaseChannel';
import { Message } from '../discord/structures/Message';

export default class WaveCordApp {
public readonly resourcesPath: string;
Expand Down Expand Up @@ -56,6 +57,11 @@ export default class WaveCordApp {
});
this.discord.on('dispatch', (event: GatewaySocketEvent) => {
logger.info('Received event: ', event.event);

if (event.event === GatewayDispatchEvents.MessageCreate) {
const message = event.data as Message;
sendToRenderer(this.window!, 'discord:gateway:message-create', message);
}
});

this.discord.login(this.token);
Expand Down
13 changes: 11 additions & 2 deletions src/main/ipc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ipcMain } from 'electron';
import { BrowserWindow, ipcMain } from 'electron';

export type IpcChannels =
| 'logger:info'
Expand All @@ -18,7 +18,8 @@ export type IpcChannels =
| 'discord:user'
| 'discord:get-last-visited-channel'
| 'discord:set-last-visited-channel'
| 'discord:create-message';
| 'discord:create-message'
| 'discord:gateway:message-create';

export function registerHandler(
channel: IpcChannels,
Expand All @@ -37,3 +38,11 @@ export function registerListener(
func(...args);
});
}

export function sendToRenderer(
window: BrowserWindow,
channel: IpcChannels,
...args: any[]
) {
window.webContents.send(channel, ...args);
}
10 changes: 5 additions & 5 deletions src/main/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ import { IpcChannels } from './ipc';

const electronHandler = {
ipcRenderer: {
sendMessage(channel: IpcChannels, ...args: unknown[]) {
sendMessage(channel: IpcChannels, ...args: any[]) {
ipcRenderer.send(channel, ...args);
},
on(channel: IpcChannels, func: (...args: unknown[]) => void) {
const subscription = (_event: IpcRendererEvent, ...args: unknown[]) =>
on(channel: IpcChannels, func: (...args: any[]) => void) {
const subscription = (_event: IpcRendererEvent, ...args: any[]) =>
func(...args);
ipcRenderer.on(channel, subscription);

return () => {
ipcRenderer.removeListener(channel, subscription);
};
},
once(channel: IpcChannels, func: (...args: unknown[]) => void) {
once(channel: IpcChannels, func: (...args: any[]) => void) {
ipcRenderer.once(channel, (_event, ...args) => func(...args));
},
invoke(channel: IpcChannels, ...args: unknown[]): Promise<any> {
invoke(channel: IpcChannels, ...args: any[]): Promise<any> {
return ipcRenderer.invoke(channel, ...args);
},
},
Expand Down
52 changes: 37 additions & 15 deletions src/renderer/pages/Guild/Channel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,49 @@ export default function ChannelPage() {
const [channel, setChannel] = useState<RendererChannel | null>(null);
const [messages, setMessages] = useState<Message[]>([]);

// Live messages
useEffect(() => {
window.electron.ipcRenderer.on(
'discord:gateway:message-create',
(msg: Message) => {
if (msg.channel_id !== channelId) return;

setMessages([msg, ...messages]);
},
);
}, [channel, channelId, messages]);

// Channel loading / Page loading
useEffect(() => {
const loadChannel = () => {
window.electron.ipcRenderer
.invoke('discord:load-channel', channelId)
.then(async (data: IChannelData) => {
if (data.id !== channelId) return false;

const chn = new RendererChannel(data);
const msgs = await chn.fetchMessages();
setChannel(chn);
setMessages(msgs);
return true;
})
.catch((err) => console.error(err));
};

if (channel !== null) {
if (channel.id !== channelId) {
loadChannel();
}
return;
}

const messageList = document.getElementById('channel_page_message_list');
if (messageList) {
messageList.scrollTop = messageList.scrollHeight;
}

window.electron.ipcRenderer
.invoke('discord:load-channel', channelId)
.then(async (data: IChannelData) => {
const chn = new RendererChannel(data);
const msgs = await chn.fetchMessages();
setChannel(chn);
setMessages(msgs);
return true;
})
.catch((err) => console.error(err));

return () => {
setMessages([]);
};
}, [channelId]);
loadChannel();
}, [channel, channelId]);

if (channelId.length === 0 || channel === null) return null;

Expand Down

0 comments on commit 301be74

Please sign in to comment.