11package org .codeoverflow .chatoverflow .requirement .service .discord
22
3+ import java .io .File
34import java .util .function .Consumer
45
56import javax .security .auth .login .LoginException
6- import net .dv8tion .jda .api .entities .{MessageChannel , TextChannel }
7- import net .dv8tion .jda .api .events .message .{MessageDeleteEvent , MessageReceivedEvent , MessageUpdateEvent }
8- import net .dv8tion .jda .api .{JDA , JDABuilder }
7+ import net .dv8tion .jda .core .entities .{Message , MessageEmbed , TextChannel }
8+ import net .dv8tion .jda .core .events .message .react .{MessageReactionAddEvent , MessageReactionRemoveEvent }
9+ import net .dv8tion .jda .core .events .message .{MessageDeleteEvent , MessageReceivedEvent , MessageUpdateEvent }
10+ import net .dv8tion .jda .core .requests .RestAction
11+ import net .dv8tion .jda .core .{JDA , JDABuilder , MessageBuilder }
912import org .codeoverflow .chatoverflow .WithLogger
1013import org .codeoverflow .chatoverflow .connector .Connector
14+ import org .codeoverflow .chatoverflow .connector .actor .FileSystemActor
15+ import org .codeoverflow .chatoverflow .connector .actor .FileSystemActor .LoadBinaryFile
1116
1217/**
1318 * The discord connector connects to the discord REST API
@@ -19,23 +24,28 @@ class DiscordChatConnector(override val sourceIdentifier: String) extends Connec
1924
2025 private var jda : Option [JDA ] = None
2126
27+ private val fileSystemActor = createActor[FileSystemActor ]
28+
2229 override protected var requiredCredentialKeys : List [String ] = List (" authToken" )
2330 override protected var optionalCredentialKeys : List [String ] = List ()
2431
2532 private val defaultFailureHandler : Consumer [_ >: Throwable ] =
2633 throwable => logger warn s " Rest action for connector $sourceIdentifier failed: ${throwable.getMessage}"
2734
28- def addMessageReceivedListener (listener : MessageReceivedEvent => Unit ): Unit = {
35+ def addMessageReceivedListener (listener : MessageReceivedEvent => Unit ): Unit =
2936 discordChatListener.addMessageReceivedListener(listener)
30- }
3137
32- def addMessageUpdateListener (listener : MessageUpdateEvent => Unit ): Unit = {
38+ def addMessageUpdateListener (listener : MessageUpdateEvent => Unit ): Unit =
3339 discordChatListener.addMessageUpdateEventListener(listener)
34- }
3540
36- def addMessageDeleteListener (listener : MessageDeleteEvent => Unit ): Unit = {
41+ def addMessageDeleteListener (listener : MessageDeleteEvent => Unit ): Unit =
3742 discordChatListener.addMessageDeleteEventListener(listener)
38- }
43+
44+ def addReactionAddEventListener (listener : MessageReactionAddEvent => Unit ): Unit =
45+ discordChatListener.addReactionAddEventListener(listener)
46+
47+ def addReactionDelEventListener (listener : MessageReactionRemoveEvent => Unit ): Unit =
48+ discordChatListener.addReactionDelEventListener(listener)
3949
4050 /**
4151 * Connects to discord
@@ -78,6 +88,24 @@ class DiscordChatConnector(override val sourceIdentifier: String) extends Connec
7888 }
7989 }
8090
91+ /**
92+ * Retrieves an already send message from a text channel by it's id
93+ *
94+ * @param channelId the id of a text channel
95+ * @param messageId the id of the message
96+ * @return a rest action that will return the message
97+ */
98+ def retrieveMessage (channelId : String , messageId : String ): RestAction [Message ] = {
99+ Option (validJDA.getTextChannelById(channelId)) match {
100+ case Some (channel) => channel.getMessageById(messageId)
101+ case None =>
102+ Option (validJDA.getPrivateChannelById(channelId)) match {
103+ case Some (channel) => channel.getMessageById(messageId)
104+ case None => throw new IllegalArgumentException (s " Channel with id $channelId not found " )
105+ }
106+ }
107+ }
108+
81109 /**
82110 * Retrieves a text channel
83111 *
@@ -98,4 +126,41 @@ class DiscordChatConnector(override val sourceIdentifier: String) extends Connec
98126 case None => throw new IllegalArgumentException (s " Channel with id $channelId not found " )
99127 }
100128 }
129+
130+ /**
131+ * Sends a embed to a text channel
132+ *
133+ * @param channelId the id of the text channel
134+ * @param embed the embed to send
135+ */
136+ def sendChatMessage (channelId : String , embed : MessageEmbed ): Unit = {
137+ Option (validJDA.getTextChannelById(channelId)) match {
138+ case Some (channel) => channel.sendMessage(embed).queue(null , defaultFailureHandler)
139+ case None => throw new IllegalArgumentException (s " Channel with id $channelId not found " )
140+ }
141+ }
142+
143+ /**
144+ * Sends a message with an attachment to a text channel
145+ *
146+ * @param channelId the id of the text channel
147+ * @param file path of a file inside the data folder that should be send
148+ * @param message optional containing the actual message (or none)
149+ */
150+ def sendFile (channelId : String , file : String , message : Option [String ] = None ): Unit = {
151+ val fileName = file.substring(file.lastIndexOf(File .separator) + 1 )
152+ val fileIn = fileSystemActor.?? [Option [Array [Byte ]]](3 )(LoadBinaryFile (file))
153+ if (fileIn.isDefined && fileIn.get.isDefined) {
154+ Option (validJDA.getTextChannelById(channelId)) match {
155+ case Some (channel) =>
156+ message match {
157+ case Some (m) => channel.sendFile(fileIn.get.get, fileName, new MessageBuilder (m).build()).queue(null , defaultFailureHandler)
158+ case None => channel.sendFile(fileIn.get.get, fileName).queue(null , defaultFailureHandler)
159+ }
160+ case None => throw new IllegalArgumentException (s " Channel with id $channelId not found " )
161+ }
162+ } else {
163+ logger warn s " Could not load file ' $file' "
164+ }
165+ }
101166}
0 commit comments