This repository has been archived by the owner on Apr 23, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5ac2517
commit 1212fef
Showing
8 changed files
with
83 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
}); | ||
} | ||
} | ||
} |