-
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.
Add clancompare integration and interactions
- Loading branch information
Showing
11 changed files
with
293 additions
and
1 deletion.
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
90 changes: 90 additions & 0 deletions
90
soa-discord/src/main/java/com/soa/rs/discordbot/v3/commands/ClanCompareInteraction.java
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,90 @@ | ||
package com.soa.rs.discordbot.v3.commands; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.github.soajeff.clancompare.ClanCompareProcessor; | ||
import com.soa.rs.discordbot.v3.api.annotation.Interaction; | ||
import com.soa.rs.discordbot.v3.api.command.AbstractCommand; | ||
import com.soa.rs.discordbot.v3.cfg.DiscordCfgFactory; | ||
import com.soa.rs.discordbot.v3.jdbi.SettingsUtility; | ||
|
||
import discord4j.core.event.domain.interaction.ChatInputInteractionEvent; | ||
import discord4j.core.event.domain.interaction.ModalSubmitInteractionEvent; | ||
import discord4j.core.event.domain.message.MessageCreateEvent; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
|
||
@Interaction(trigger = "clancompare") | ||
public class ClanCompareInteraction extends AbstractCommand { | ||
|
||
private String clanFetchUrl; | ||
private String clanFetchApiKey; | ||
private String rsClanFetchUrl; | ||
private String altFetchUrl; | ||
private SettingsUtility settingsUtility; | ||
|
||
@Override | ||
public void initialize() { | ||
if (DiscordCfgFactory.getConfig().getClanCompareEvent() != null && DiscordCfgFactory.getConfig().getClanCompareEvent().isEnabled()) { | ||
setEnabled(DiscordCfgFactory.getConfig().getClanCompareEvent().isEnabled()); | ||
this.clanFetchUrl = DiscordCfgFactory.getConfig().getClanCompareEvent().getForumsApiUrl(); | ||
this.clanFetchApiKey = DiscordCfgFactory.getConfig().getClanCompareEvent().getForumsApiKey(); | ||
this.rsClanFetchUrl = DiscordCfgFactory.getConfig().getClanCompareEvent().getRsClanUrl(); | ||
this.altFetchUrl = DiscordCfgFactory.getConfig().getClanCompareEvent().getCompetitionsUrl(); | ||
this.settingsUtility = new SettingsUtility(); | ||
} else { | ||
setEnabled(false); | ||
} | ||
} | ||
|
||
@Override | ||
public Mono<Void> execute(MessageCreateEvent event) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Mono<Void> execute(ChatInputInteractionEvent event) { | ||
return event.deferReply().withEphemeral(true).then(Mono.fromCallable(() -> settingsUtility.getValueForKey("clancompare.altId")) | ||
.map(Integer::parseInt) | ||
.flatMap(i -> Mono.fromCallable(() -> processClanCompare(i))) | ||
.flatMapMany(Flux::fromIterable) | ||
.flatMapSequential(s -> event.createFollowup(s).withEphemeral(true)).then()) | ||
.then(); | ||
} | ||
|
||
@Override | ||
public Mono<Void> execute(ModalSubmitInteractionEvent event) { | ||
return null; | ||
} | ||
|
||
public List<String> processClanCompare(int altId) | ||
{ | ||
ClanCompareProcessor processor = new ClanCompareProcessor(clanFetchUrl, clanFetchApiKey, rsClanFetchUrl, altFetchUrl, altId); | ||
List<String> processResult = processor.process(); | ||
|
||
List<String> response = new ArrayList<>(); | ||
|
||
if(processResult.size() == 0) | ||
{ | ||
response.add("Clan Compare found no deltas between the forums and the in-game clan."); | ||
return response; | ||
} | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append("Clan Compare Results:"); | ||
sb.append("\n"); | ||
for(String s : processResult) | ||
{ | ||
sb.append("* "); | ||
sb.append(s); | ||
sb.append("\n"); | ||
if(sb.length() > 1500) | ||
{ | ||
response.add(sb.toString().trim()); | ||
sb.setLength(0); | ||
} | ||
} | ||
response.add(sb.toString().trim()); | ||
return response; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...rc/main/java/com/soa/rs/discordbot/v3/commands/ClanCompareUpdateAltCompIdInteraction.java
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,60 @@ | ||
package com.soa.rs.discordbot.v3.commands; | ||
|
||
import com.soa.rs.discordbot.v3.api.annotation.Interaction; | ||
import com.soa.rs.discordbot.v3.api.command.AbstractCommand; | ||
import com.soa.rs.discordbot.v3.cfg.DiscordCfgFactory; | ||
import com.soa.rs.discordbot.v3.jdbi.SettingsUtility; | ||
|
||
import discord4j.core.event.domain.interaction.ChatInputInteractionEvent; | ||
import discord4j.core.event.domain.interaction.ModalSubmitInteractionEvent; | ||
import discord4j.core.event.domain.message.MessageCreateEvent; | ||
import discord4j.core.object.command.ApplicationCommandInteractionOption; | ||
import discord4j.core.object.command.ApplicationCommandInteractionOptionValue; | ||
import discord4j.core.spec.InteractionFollowupCreateMono; | ||
import reactor.core.publisher.Mono; | ||
|
||
@Interaction(trigger = "clancompareupdatealtcompid") | ||
public class ClanCompareUpdateAltCompIdInteraction extends AbstractCommand { | ||
|
||
private SettingsUtility settingsUtility; | ||
private final String CLANCOMPARE_SETTING = "clancompare.altId" ; | ||
@Override | ||
public void initialize() { | ||
if (DiscordCfgFactory.getConfig().getClanCompareEvent() != null && DiscordCfgFactory.getConfig().getClanCompareEvent().isEnabled()) { | ||
setEnabled(DiscordCfgFactory.getConfig().getClanCompareEvent().isEnabled()); | ||
this.settingsUtility = new SettingsUtility(); | ||
} | ||
else { | ||
setEnabled(false); | ||
} | ||
} | ||
|
||
@Override | ||
public Mono<Void> execute(MessageCreateEvent event) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Mono<Void> execute(ChatInputInteractionEvent event) { | ||
long compid = event.getOption("compid").flatMap(ApplicationCommandInteractionOption::getValue) | ||
.map(ApplicationCommandInteractionOptionValue::asLong).get(); | ||
String compidString = Long.toString(compid); | ||
return event.deferReply().withEphemeral(true).then(Mono.fromCallable(()->settingsUtility.updateValueForKey(CLANCOMPARE_SETTING, compidString)).flatMap( | ||
i->sendUpdateMessage(i, event))).then(); | ||
} | ||
|
||
private InteractionFollowupCreateMono sendUpdateMessage(Integer i, ChatInputInteractionEvent event) { | ||
if(i == 1) | ||
{ | ||
return event.createFollowup("Value updated.").withEphemeral(true); | ||
} | ||
else { | ||
return event.createFollowup("Failed to update value.").withEphemeral(true); | ||
} | ||
} | ||
|
||
@Override | ||
public Mono<Void> execute(ModalSubmitInteractionEvent event) { | ||
return null; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
soa-discord/src/main/java/com/soa/rs/discordbot/v3/jdbi/SettingsUtility.java
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,21 @@ | ||
package com.soa.rs.discordbot.v3.jdbi; | ||
|
||
public class SettingsUtility { | ||
|
||
public void createSettingsTable() { | ||
JdbiWrapper.getInstance().getJdbi().useHandle(handle -> handle.execute( | ||
"create table settings (settingkey varchar(255) not null, settingvalue varchar(255), primary key(settingkey))")); | ||
} | ||
|
||
public String getValueForKey(String key) { | ||
return JdbiWrapper.getInstance().getJdbi().withHandle( | ||
handle -> handle.createQuery("select settingvalue from settings where settingkey = :settingkey").bind("settingkey", key) | ||
.mapTo(String.class).first()); | ||
} | ||
|
||
public int updateValueForKey(String key, String value) { | ||
return JdbiWrapper.getInstance().getJdbi().withHandle( | ||
handle -> handle.createUpdate("update settings set settingvalue = :settingvalue where settingkey = :settingkey") | ||
.bind("settingvalue", value).bind("settingkey", key).execute()); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
soa-discord/src/main/java/com/soa/rs/discordbot/v3/jdbi/entities/Setting.java
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,23 @@ | ||
package com.soa.rs.discordbot.v3.jdbi.entities; | ||
|
||
public class Setting { | ||
|
||
private String key; | ||
private String value; | ||
|
||
public String getKey() { | ||
return key; | ||
} | ||
|
||
public void setKey(String key) { | ||
this.key = key; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
|
||
public void setValue(String value) { | ||
this.value = value; | ||
} | ||
} |
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,4 @@ | ||
{ | ||
"name": "clancompare", | ||
"description": "Runs a comparison between the SoA forums memberlist and the RS clan memberlist." | ||
} |
12 changes: 12 additions & 0 deletions
12
soa-discord/src/main/resources/commands/clancompareupdatealtcompid.json
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,12 @@ | ||
{ | ||
"name": "clancompareupdatealtcompid", | ||
"description": "Update the ID of the monthly alt xp competition used by the clancompare command", | ||
"options": [ | ||
{ | ||
"name": "compid", | ||
"description": "Competition ID of the xp comp that is currently tracking clan alts.", | ||
"type": 4, | ||
"required": true | ||
} | ||
] | ||
} |
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
53 changes: 53 additions & 0 deletions
53
soa-discord/src/test/java/com/soa/rs/discordbot/v3/jdbi/SettingsUtilityTest.java
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,53 @@ | ||
package com.soa.rs.discordbot.v3.jdbi; | ||
|
||
import org.jdbi.v3.core.Jdbi; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
public class SettingsUtilityTest { | ||
|
||
private SettingsUtility settingsUtility = new SettingsUtility(); | ||
|
||
@BeforeClass | ||
public static void setUp() { | ||
Jdbi jdbi = Jdbi.create("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;"); | ||
JdbiWrapper.getInstance().setJdbi(jdbi); | ||
} | ||
|
||
@Before | ||
public void createDb() { | ||
settingsUtility.createSettingsTable(); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
JdbiWrapper.getInstance().getJdbi().useHandle(handle -> handle.execute("drop table settings")); | ||
} | ||
|
||
@Test | ||
public void testGetValueForKey() { | ||
JdbiWrapper.getInstance().getJdbi().useHandle( | ||
handle -> handle.createUpdate("insert into settings values ('settingkey1', 'settingvalue1')") | ||
.execute()); | ||
|
||
String value = settingsUtility.getValueForKey("settingkey1"); | ||
Assert.assertEquals("settingvalue1", value); | ||
} | ||
|
||
@Test | ||
public void testUpdateValueForKey() | ||
{ | ||
JdbiWrapper.getInstance().getJdbi().useHandle( | ||
handle -> handle.createUpdate("insert into settings values ('settingkey1', 'settingvalue1')") | ||
.execute()); | ||
|
||
settingsUtility.updateValueForKey("settingkey1", "settingvalue2"); | ||
|
||
String value = settingsUtility.getValueForKey("settingkey1"); | ||
Assert.assertEquals("settingvalue2", value); | ||
} | ||
|
||
} |