Got rid of Export default's because they're bad bad
This commit is contained in:
parent
d7ad22b115
commit
623645090d
@ -1,21 +1,22 @@
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
import Hyperswarm, { PeerDiscovery } from 'hyperswarm';
|
import Hyperswarm, { PeerDiscovery } from 'hyperswarm';
|
||||||
import TextMessage from "./message/TextMessage";
|
import {TextMessage} from "./message/TextMessage";
|
||||||
import FileMessage from "./message/FileMessage";
|
import {FileMessage} from "./message/FileMessage";
|
||||||
import AudioMessage from "./message/AudioMessage";
|
import {AudioMessage} from "./message/AudioMessage";
|
||||||
import Message from "./message/Message";
|
import {Message} from "./message/Message";
|
||||||
import Corestore from 'corestore';
|
import Corestore from 'corestore';
|
||||||
import Hyperdrive from 'hyperdrive';
|
import Hyperdrive from 'hyperdrive';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import ServeDrive from 'serve-drive';
|
import ServeDrive from 'serve-drive';
|
||||||
import IconMessage from "./message/IconMessage";
|
import {IconMessage} from "./message/IconMessage";
|
||||||
import TypedEventEmitter from "./util/TypedEventEmitter";
|
import {TypedEventEmitter} from "./util/TypedEventEmitter";
|
||||||
|
import {LinkUpEvents} from "./LinkUpEvents";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class is the core component of the bot system. It handles connections to the Hyperswarm network, manages message sending and receiving, and emits events for various actions.
|
* This class is the core component of the bot system. It handles connections to the Hyperswarm network, manages message sending and receiving, and emits events for various actions.
|
||||||
*/
|
*/
|
||||||
class Client extends TypedEventEmitter<LinkUpEvents> {
|
export class Client extends TypedEventEmitter<LinkUpEvents> {
|
||||||
public botName: string = "";
|
public botName: string = "";
|
||||||
public servePort: number | null = 0;
|
public servePort: number | null = 0;
|
||||||
public storagePath: string | undefined;
|
public storagePath: string | undefined;
|
||||||
@ -325,26 +326,4 @@ class Client extends TypedEventEmitter<LinkUpEvents> {
|
|||||||
await this.swarm?.destroy();
|
await this.swarm?.destroy();
|
||||||
console.log(`Bot ${this.botName} disconnected.`);
|
console.log(`Bot ${this.botName} disconnected.`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export type LinkUpEvents = {
|
|
||||||
/**
|
|
||||||
* Triggered when a new message is received.
|
|
||||||
*
|
|
||||||
* @event Client#onMessage
|
|
||||||
* @property {TextMessage} TextMessage - Class with all of the information about received text message
|
|
||||||
* @example
|
|
||||||
* const bot = new Client("MyBot");
|
|
||||||
* bot.on('onMessage', (message) => {
|
|
||||||
* console.log(`Message from ${message.peerName}: ${message.message}`);
|
|
||||||
* });
|
|
||||||
*/
|
|
||||||
'onMessage': [textMessage: TextMessage]
|
|
||||||
'onFile': [fileMessage: FileMessage]
|
|
||||||
'onAudio': [audioMessage: AudioMessage]
|
|
||||||
'onIcon': [iconMessage: IconMessage]
|
|
||||||
'onError': [error: any]
|
|
||||||
'onBotJoinRoom': [currentTopic: string]
|
|
||||||
}
|
|
||||||
|
|
||||||
export default Client;
|
|
24
src/LinkUpEvents.ts
Normal file
24
src/LinkUpEvents.ts
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
import {TextMessage} from "./message/TextMessage";
|
||||||
|
import {FileMessage} from "./message/FileMessage";
|
||||||
|
import {AudioMessage} from "./message/AudioMessage";
|
||||||
|
import {IconMessage} from "./message/IconMessage";
|
||||||
|
|
||||||
|
export type LinkUpEvents = {
|
||||||
|
/**
|
||||||
|
* Triggered when a new message is received.
|
||||||
|
*
|
||||||
|
* @event Client#onMessage
|
||||||
|
* @property {TextMessage} TextMessage - Class with all of the information about received text message
|
||||||
|
* @example
|
||||||
|
* const bot = new Client("MyBot");
|
||||||
|
* bot.on('onMessage', (message) => {
|
||||||
|
* console.log(`Message from ${message.peerName}: ${message.message}`);
|
||||||
|
* });
|
||||||
|
*/
|
||||||
|
'onMessage': [textMessage: TextMessage]
|
||||||
|
'onFile': [fileMessage: FileMessage]
|
||||||
|
'onAudio': [audioMessage: AudioMessage]
|
||||||
|
'onIcon': [iconMessage: IconMessage]
|
||||||
|
'onError': [error: any]
|
||||||
|
'onBotJoinRoom': [currentTopic: string]
|
||||||
|
}
|
@ -2,11 +2,11 @@
|
|||||||
* @module Message
|
* @module Message
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import Message from "./Message";
|
import {Message} from "./Message";
|
||||||
import b4a from "b4a";
|
import b4a from "b4a";
|
||||||
import Client from "../Client";
|
import {Client} from "../Client";
|
||||||
|
|
||||||
class AudioMessage extends Message {
|
export class AudioMessage extends Message {
|
||||||
audioUrl: string;
|
audioUrl: string;
|
||||||
audioType: string;
|
audioType: string;
|
||||||
audioData: string;
|
audioData: string;
|
||||||
@ -59,5 +59,3 @@ class AudioMessage extends Message {
|
|||||||
return new AudioMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), audioUrl, audioType, audioData);
|
return new AudioMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), audioUrl, audioType, audioData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default AudioMessage;
|
|
||||||
|
@ -2,11 +2,11 @@
|
|||||||
* @module Message
|
* @module Message
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import Message from "./Message";
|
import {Message} from "./Message";
|
||||||
import b4a from "b4a";
|
import b4a from "b4a";
|
||||||
import Client from "../Client";
|
import {Client} from "../Client";
|
||||||
|
|
||||||
class FileMessage extends Message {
|
export class FileMessage extends Message {
|
||||||
fileName: string;
|
fileName: string;
|
||||||
fileUrl: string;
|
fileUrl: string;
|
||||||
fileType: string;
|
fileType: string;
|
||||||
@ -63,6 +63,4 @@ class FileMessage extends Message {
|
|||||||
const fileData = b4a.toString(fileBuffer, 'base64'); // Convert file buffer to base64
|
const fileData = b4a.toString(fileBuffer, 'base64'); // Convert file buffer to base64
|
||||||
return new FileMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), fileName, fileUrl, fileType, fileData);
|
return new FileMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), fileName, fileUrl, fileType, fileData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default FileMessage;
|
|
@ -2,11 +2,11 @@
|
|||||||
* @module Message
|
* @module Message
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import Message from "./Message";
|
import {Message} from "./Message";
|
||||||
import b4a from "b4a";
|
import b4a from "b4a";
|
||||||
import Client from "../Client";
|
import {Client} from "../Client";
|
||||||
|
|
||||||
class IconMessage extends Message {
|
export class IconMessage extends Message {
|
||||||
/**
|
/**
|
||||||
* @description Creates a new icon message.
|
* @description Creates a new icon message.
|
||||||
* @since 1.0
|
* @since 1.0
|
||||||
@ -31,6 +31,4 @@ class IconMessage extends Message {
|
|||||||
static new(bot: Client, avatarBuffer: Buffer): IconMessage {
|
static new(bot: Client, avatarBuffer: Buffer): IconMessage {
|
||||||
return new IconMessage(bot.botName, b4a.toString(avatarBuffer, 'base64'), Date.now());
|
return new IconMessage(bot.botName, b4a.toString(avatarBuffer, 'base64'), Date.now());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default IconMessage;
|
|
@ -4,7 +4,7 @@
|
|||||||
* @author MiTask
|
* @author MiTask
|
||||||
* @module Message
|
* @module Message
|
||||||
*/
|
*/
|
||||||
class Message {
|
export class Message {
|
||||||
type: string;
|
type: string;
|
||||||
peerName: string;
|
peerName: string;
|
||||||
peerAvatar: string;
|
peerAvatar: string;
|
||||||
@ -54,6 +54,4 @@ class Message {
|
|||||||
...this.toJson()
|
...this.toJson()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Message;
|
|
@ -2,10 +2,10 @@
|
|||||||
* @module Message
|
* @module Message
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import Message from "./Message";
|
import {Message} from "./Message";
|
||||||
import Client from "../Client";
|
import {Client} from "../Client";
|
||||||
|
|
||||||
class TextMessage extends Message {
|
export class TextMessage extends Message {
|
||||||
message: string;
|
message: string;
|
||||||
/**
|
/**
|
||||||
* @description Creates a new text message.
|
* @description Creates a new text message.
|
||||||
@ -46,6 +46,4 @@ class TextMessage extends Message {
|
|||||||
static new(bot: Client, message: string): TextMessage {
|
static new(bot: Client, message: string): TextMessage {
|
||||||
return new TextMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), message);
|
return new TextMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default TextMessage;
|
|
@ -4,7 +4,7 @@ import {EventEmitter} from "stream";
|
|||||||
* This class is used for TypeSafe events.
|
* This class is used for TypeSafe events.
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
export default class TypedEventEmitter<TEvents extends Record<string, any>> {
|
export class TypedEventEmitter<TEvents extends Record<string, any>> {
|
||||||
private emitter = new EventEmitter()
|
private emitter = new EventEmitter()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user