Skip to content
This repository has been archived by the owner on Apr 23, 2023. It is now read-only.

Commit

Permalink
I put in the wrong mqtt before lol
Browse files Browse the repository at this point in the history
  • Loading branch information
sebasptsch committed Oct 11, 2022
1 parent 5ac2517 commit 1212fef
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 93 deletions.
2 changes: 1 addition & 1 deletion src/classes/Aliases.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Service } from 'typedi';
import type DynamicaAlias from './Alias';
import MQTT from './MQTT';
import MQTT from '../services/MQTT';

@Service()
export default class Aliases {
Expand Down
2 changes: 1 addition & 1 deletion src/classes/Events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Client from '@/services/Client';
import { ClientEvents } from 'discord.js';
import { Service } from 'typedi';
import type Event from './Event';
import MQTT from './MQTT';
import MQTT from '../services/MQTT';

@Service()
export default class Events {
Expand Down
2 changes: 1 addition & 1 deletion src/classes/Guilds.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Service } from 'typedi';
import type DynamicaGuild from './Guild';
import MQTT from './MQTT';
import MQTT from '../services/MQTT';

@Service()
export default class Guilds {
Expand Down
79 changes: 0 additions & 79 deletions src/classes/MQTT.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/classes/Primaries.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Service } from 'typedi';
import MQTT from './MQTT';
import MQTT from '../services/MQTT';
import type DynamicaPrimary from './Primary';

@Service()
Expand Down
2 changes: 1 addition & 1 deletion src/classes/Secondaries.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Service } from 'typedi';
import MQTT from './MQTT';
import MQTT from '../services/MQTT';
import type DynamicaSecondary from './Secondary';

@Service()
Expand Down
2 changes: 1 addition & 1 deletion src/events/ReadyEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Aliases from '@/classes/Aliases';
import AliasFactory from '@/classes/AliasFactory';
import GuildFactory from '@/classes/GuildFactory';
import Guilds from '@/classes/Guilds';
import MQTT from '@/classes/MQTT';
import MQTT from '@/services/MQTT';
import Primaries from '@/classes/Primaries';
import PrimaryFactory from '@/classes/PrimaryFactory';
import Secondaries from '@/classes/Secondaries';
Expand Down
85 changes: 77 additions & 8 deletions src/services/MQTT.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,79 @@
import { CacheType, ChatInputCommandInteraction } from 'discord.js';
import Logger from '@/services/Logger';
import mqtt from 'mqtt';
import { Service } from 'typedi';

const interactionDetails = (
interaction: ChatInputCommandInteraction<CacheType>
) => ({
name: interaction.commandName,
timestamp: new Date(interaction.createdTimestamp).toISOString(),
});
@Service()
export default class MQTT {
private client: mqtt.MqttClient | undefined = undefined;

export default interactionDetails;
private constructor(private logger: Logger) {
if (process.env.MQTT_URL) {
this.client = mqtt.connect(process.env.MQTT_URL, {
username: process.env.MQTT_USER,
password: process.env.MQTT_PASS,
});
this.client.on('connect', () => {
this.logger.scope('MQTT').info('Connected');
});
this.client.on('error', (error) => {
this.logger.scope('MQTT').error('Error', error);
});
}
}

public publish(topic: string, message: string | Buffer) {
return new Promise<void>((res, rej) => {
if (this.client) {
this.client.publish(topic, message, { retain: true }, (err) => {
if (err) {
rej(err);
} else {
res();
}
});
} else {
res();
}
});
}

public subscribe(topic: string) {
return new Promise<void>((res, rej) => {
if (this.client) {
this.client.subscribe(topic, (err) => {
if (err) {
rej(err);
} else {
res();
}
});
} else {
res();
}
});
}

public unsubscribe(topic: string) {
return new Promise<void>((res, rej) => {
if (this.client) {
this.client.unsubscribe(topic, (err) => {
if (err) {
rej(err);
} else {
res();
}
});
} else {
res();
}
});
}

public onMessage(callback: (topic: string, message: string) => void) {
if (this.client) {
this.client.on('message', (topic, message) => {
callback(topic, JSON.parse(message.toString()));
});
}
}
}

0 comments on commit 1212fef

Please sign in to comment.