Skip to content

Commit

Permalink
Add redis command and pub/sub event (#75)
Browse files Browse the repository at this point in the history
* Add redis command and pub/sub event

* Fix minor redis event issues

* Allow multiple subbed channels, command arg list input

* Avoid list object hacking
  • Loading branch information
mergu committed Nov 28, 2021
1 parent 210ea8e commit 3d3bfea
Show file tree
Hide file tree
Showing 6 changed files with 590 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Expand Up @@ -62,6 +62,11 @@
<version>9.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.7.0</version>
</dependency>
</dependencies>

<ciManagement>
Expand Down
Expand Up @@ -50,6 +50,7 @@ public static void registerCoreEvents() {
registerScriptEvent(new ConsoleOutputScriptEvent());
registerScriptEvent(new CustomScriptEvent());
registerScriptEvent(new DeltaTimeScriptEvent());
registerScriptEvent(new RedisPubSubMessageScriptEvent());
registerScriptEvent(new PreScriptReloadScriptEvent());
registerScriptEvent(new ReloadScriptsScriptEvent());
registerScriptEvent(new ScriptGeneratesErrorScriptEvent());
Expand Down
@@ -0,0 +1,78 @@
package com.denizenscript.denizencore.events.core;

import com.denizenscript.denizencore.DenizenCore;
import com.denizenscript.denizencore.events.ScriptEvent;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;

public class RedisPubSubMessageScriptEvent extends ScriptEvent {

// <--[event]
// @Events
// redis pubsub message
//
// @Switch channel:<channel> to only fire on events advanced-matching the given channel.
//
// @Group Core
//
// @Triggers when a subscribed redis connection receives a published message, see <@link command Redis>.
//
// @Context
// <context.redis_id> returns the connection id that saw this message.
// <context.pattern> returns the redis pattern that matched the channel.
// <context.channel> returns the actual channel matched.
// <context.message> returns the published message.
//
// -->

public static RedisPubSubMessageScriptEvent instance;

public String redisID;
public String pattern;
public String channel;
public String message;

public RedisPubSubMessageScriptEvent() {
instance = this;
registerCouldMatcher("redis pubsub message");
registerSwitches("channel");
}

@Override
public ScriptEntryData getScriptEntryData() {
return DenizenCore.implementation.getEmptyScriptEntryData();
}

@Override
public boolean matches(ScriptPath path) {
if (!runGenericSwitchCheck(path, "channel", channel)) {
return false;
}
return super.matches(path);
}

@Override
public String getName() {
return "RedisPubSubMessage";
}

@Override
public ObjectTag getContext(String name) {
switch (name) {
case "redis_id": return new ElementTag(redisID);
case "pattern": return new ElementTag(pattern);
case "channel": return new ElementTag(channel);
case "message": return new ElementTag(message);
}
return super.getContext(name);
}

public void handle(String redisID, String pattern, String channel, String message) {
this.redisID = redisID;
this.pattern = pattern;
this.channel = channel;
this.message = message;
fire();
}
}
Expand Up @@ -98,6 +98,7 @@ public void registerCoreCommands() {
registerCommand(NoteCommand.class);
registerCommand(ReloadCommand.class);
registerCommand(SQLCommand.class);
registerCommand(RedisCommand.class);
registerCommand(WebGetCommand.class);
// file
registerCommand(FileCopyCommand.class);
Expand Down

0 comments on commit 3d3bfea

Please sign in to comment.