-
Notifications
You must be signed in to change notification settings - Fork 4
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
7f0f254
commit d264ec0
Showing
3 changed files
with
112 additions
and
49 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
src/main/scala/score/discord/canti/command/DataReplyingCommand.scala
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package score.discord.canti.command | ||
|
||
import net.dv8tion.jda.api.entities.Message | ||
import net.dv8tion.jda.api.requests.restaction.MessageAction | ||
import score.discord.canti.collections.ReplyCache | ||
import score.discord.canti.functionality.ownership.MessageOwnership | ||
import score.discord.canti.util.APIHelper | ||
import score.discord.canti.wrappers.jda.Conversions._ | ||
import score.discord.canti.wrappers.jda.ID | ||
|
||
import scala.concurrent.ExecutionContext.Implicits.global | ||
import scala.concurrent.{ExecutionContext, Future} | ||
import scala.util.chaining.scalaUtilChainingOps | ||
|
||
trait DataReplyingCommand[T] extends ReplyingCommand { | ||
override final def executeAndGetMessage(message: Message, args: String): Future[Message] = | ||
executeAndGetMessageWithData(message, args).map(_._1)(ExecutionContext.parasitic) | ||
|
||
def executeAndGetMessageWithData(message: Message, args: String): Future[(Message, T)] | ||
|
||
override def executeFuture(message: Message, args: String): Future[Message] = | ||
for { | ||
(replyUnsent, data) <- executeAndGetMessageWithData(message, args) | ||
reply <- | ||
message.reply(replyUnsent) | ||
.mentionRepliedUser(false) | ||
.pipe(tweakMessageAction(_, data)) | ||
.queueFuture() | ||
.tap(message.registerReply) | ||
} yield reply | ||
|
||
def tweakMessageAction(action: MessageAction, data: T): MessageAction | ||
|
||
override def executeForEdit(message: Message, myMessageOption: Option[ID[Message]], args: String): Unit = | ||
for (oldMessage <- myMessageOption; (myReply, data) <- executeAndGetMessageWithData(message, args)) { | ||
APIHelper.tryRequest(message.getChannel.editMessageById(oldMessage.value, myReply).pipe(tweakMessageAction(_, data)), | ||
onFail = APIHelper.failure("executing a command for edited message")) | ||
} | ||
} |
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