Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit f57dc5a

Browse files
committed
Merge branch 'discord-v3'
2 parents 64227f9 + 6f13072 commit f57dc5a

File tree

6 files changed

+340
-112
lines changed

6 files changed

+340
-112
lines changed

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ libraryDependencies ++= Seq(
5555

5656
// JDA
5757
resolvers += "jcenter-bintray" at "http://jcenter.bintray.com"
58-
libraryDependencies += "net.dv8tion" % "JDA" % "4.ALPHA.0_82"
58+
libraryDependencies += "net.dv8tion" % "JDA" % "3.8.3_463"
5959

6060
//Serial Communication
6161
libraryDependencies += "com.fazecast" % "jSerialComm" % "[2.0.0,3.0.0)"

src/main/scala/org/codeoverflow/chatoverflow/connector/actor/FileSystemActor.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ class FileSystemActor extends Actor {
3939
try {
4040
sender ! Some(Files.readAllBytes(fixPath(pathInResources).toPath))
4141
} catch {
42-
case e: Exception => e.printStackTrace()
43-
None
42+
case e: Exception => None
4443
}
4544
case SaveFile(pathInResources, content) =>
4645
try {

src/main/scala/org/codeoverflow/chatoverflow/requirement/service/discord/DiscordChatConnector.scala

Lines changed: 74 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package org.codeoverflow.chatoverflow.requirement.service.discord
22

3+
import java.io.File
34
import java.util.function.Consumer
45

56
import 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}
912
import org.codeoverflow.chatoverflow.WithLogger
1013
import 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
}

src/main/scala/org/codeoverflow/chatoverflow/requirement/service/discord/DiscordChatListener.scala

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package org.codeoverflow.chatoverflow.requirement.service.discord
22

3-
import net.dv8tion.jda.api.events.GenericEvent
4-
import net.dv8tion.jda.api.events.message.{MessageDeleteEvent, MessageReceivedEvent, MessageUpdateEvent}
5-
import net.dv8tion.jda.api.hooks.EventListener
3+
import net.dv8tion.jda.core.events.Event
4+
import net.dv8tion.jda.core.events.message.react.{MessageReactionAddEvent, MessageReactionRemoveEvent}
5+
import net.dv8tion.jda.core.events.message.{MessageDeleteEvent, MessageReceivedEvent, MessageUpdateEvent}
6+
import net.dv8tion.jda.core.hooks.EventListener
67

78
import scala.collection.mutable.ListBuffer
89

@@ -17,17 +18,27 @@ class DiscordChatListener extends EventListener {
1718

1819
private val messageDeleteEventListener = ListBuffer[MessageDeleteEvent => Unit]()
1920

21+
private val reactionAddEventListener = ListBuffer[MessageReactionAddEvent => Unit]()
22+
23+
private val reactionDelEventListener = ListBuffer[MessageReactionRemoveEvent => Unit]()
24+
2025
def addMessageReceivedListener(listener: MessageReceivedEvent => Unit): Unit = messageEventListener += listener
2126

2227
def addMessageUpdateEventListener(listener: MessageUpdateEvent => Unit): Unit = messageUpdateEventListener += listener
2328

2429
def addMessageDeleteEventListener(listener: MessageDeleteEvent => Unit): Unit = messageDeleteEventListener += listener
2530

26-
override def onEvent(event: GenericEvent): Unit = {
31+
def addReactionAddEventListener(listener: MessageReactionAddEvent => Unit): Unit = reactionAddEventListener += listener
32+
33+
def addReactionDelEventListener(listener: MessageReactionRemoveEvent => Unit): Unit = reactionDelEventListener += listener
34+
35+
override def onEvent(event: Event): Unit = {
2736
event match {
2837
case receivedEvent: MessageReceivedEvent => messageEventListener.foreach(listener => listener(receivedEvent))
2938
case updateEvent: MessageUpdateEvent => messageUpdateEventListener.foreach(listener => listener(updateEvent))
3039
case deleteEvent: MessageDeleteEvent => messageDeleteEventListener.foreach(listener => listener(deleteEvent))
40+
case reactionAddEvent: MessageReactionAddEvent => reactionAddEventListener.foreach(listener => listener(reactionAddEvent))
41+
case reactionDelEvent: MessageReactionRemoveEvent => reactionDelEventListener.foreach(listener => listener(reactionDelEvent))
3142
case _ => //Any other event, do nothing
3243
}
3344
}

0 commit comments

Comments
 (0)