Skip to content

Commit

Permalink
✨ Added message creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ZickZenni committed Sep 5, 2024
1 parent 69993a6 commit 052a001
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 4 deletions.
16 changes: 16 additions & 0 deletions src/discord/structures/channel/BaseChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ export interface IChannelData {
total_message_sent?: number;
}

export interface CreateMessageOptions {
content?: string;
embeds?: any[];
components?: any[];
sticker_ids?: Snowflake[];
files?: any[];
}

/* eslint-disable class-methods-use-this */
export default abstract class BaseChannel {
public readonly id: Snowflake;
Expand Down Expand Up @@ -100,6 +108,14 @@ export default abstract class BaseChannel {
*/
public abstract fetchMessages(): Promise<Message[]>;

/**
* Creates a new message in the channel
* @param options Message options
*/
public abstract createMessage(
options: CreateMessageOptions,
): Promise<Message | null>;

/**
* Converts the class back into a raw data object
*/
Expand Down
40 changes: 38 additions & 2 deletions src/discord/structures/channel/MainChannel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import logger from '../../../common/log/logger';
import { Client } from '../../core/client';
import { Message } from '../Message';
import BaseChannel, { ChannelType, IChannelData } from './BaseChannel';
import BaseChannel, {
ChannelType,
CreateMessageOptions,
IChannelData,
} from './BaseChannel';

export default class MainChannel extends BaseChannel {
private readonly client: Client;
Expand Down Expand Up @@ -28,6 +33,37 @@ export default class MainChannel extends BaseChannel {
return this.messages;
}

public async createMessage(
options: CreateMessageOptions,
): Promise<Message | null> {
if (this.type !== ChannelType.GuildText) return null;

options.content ??= '';
options.components ??= [];
options.embeds ??= [];
options.files ??= [];
options.sticker_ids ??= [];

try {
const json = await this.client.restPost(
`/channels/${this.id}/messages`,
options,
);
if (json.message) return null;

const message = json as Message;
this.messages.push(message);
return message;
} catch (err) {
logger.error(
`[Discord/Channel] Failed to create message in channel '${this.id}': ${err}`,
);
return null;
}
}

/* --------------------------- */

private async fetchMessagesApi(): Promise<Message[]> {
if (this.type !== ChannelType.GuildText || this.client === null) return [];

Expand All @@ -38,7 +74,7 @@ export default class MainChannel extends BaseChannel {
if (json.message) return [];
return json as Message[];
} catch (err) {
console.error(
logger.error(
`[Discord/Channel] Failed to retrieve messages from channel '${this.id}': ${err}`,
);
return [];
Expand Down
12 changes: 11 additions & 1 deletion src/discord/structures/channel/RendererChannel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Message } from '../Message';
import BaseChannel from './BaseChannel';
import BaseChannel, { CreateMessageOptions } from './BaseChannel';

export default class RendererChannel extends BaseChannel {
public async fetchMessages(): Promise<Message[]> {
Expand All @@ -8,4 +8,14 @@ export default class RendererChannel extends BaseChannel {
this.id,
);
}

public async createMessage(
options: CreateMessageOptions,
): Promise<Message | null> {
return window.electron.ipcRenderer.invoke(
'discord:create-message',
this.id,
options,
);
}
}
11 changes: 11 additions & 0 deletions src/main/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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 { CreateMessageOptions } from '../discord/structures/channel/BaseChannel';

export default class WaveCordApp {
public readonly resourcesPath: string;
Expand Down Expand Up @@ -220,6 +221,16 @@ export default class WaveCordApp {

return channel.fetchMessages();
});

registerHandler(
'discord:create-message',
async (channelId: string, options: CreateMessageOptions) => {
const channel = this.discord.channels.cache.get(channelId);
if (channel === undefined) return null;

return channel.createMessage(options);
},
);
}

private initTray() {
Expand Down
3 changes: 2 additions & 1 deletion src/main/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export type IpcChannels =
| 'discord:fetch-guild'
| 'discord:user'
| 'discord:get-last-visited-channel'
| 'discord:set-last-visited-channel';
| 'discord:set-last-visited-channel'
| 'discord:create-message';

export function registerHandler(
channel: IpcChannels,
Expand Down

0 comments on commit 052a001

Please sign in to comment.