Skip to content

Commit

Permalink
Add clancompare integration and interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
SoAJeff committed Mar 21, 2023
1 parent 2e36c32 commit 1110d49
Show file tree
Hide file tree
Showing 11 changed files with 293 additions and 1 deletion.
10 changes: 10 additions & 0 deletions soa-discord/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<name>Maven Repository Switchboard</name>
<url>https://repo1.maven.org/maven2</url>
</repository>
<repository>
<id>github-clancompare</id>
<name>Github ClanCompare repo</name>
<url>https://maven.pkg.github.com/SoAJeff/ClanCompare</url>
</repository>
<repository>
<!-- This repo fixes issues with transitive dependencies -->
<id>jcenter</id>
Expand Down Expand Up @@ -55,6 +60,11 @@
<artifactId>discord4j-core</artifactId>
<version>3.2.3</version>
</dependency>
<dependency>
<groupId>com.github.soajeff</groupId>
<artifactId>clancompare</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,18 +195,20 @@ private ConfigValidatorResult validateConfig() {
}

if (JdbiWrapper.getInstance().getJdbi()
.withHandle(handle -> handle.createQuery("show tables").mapTo(String.class).list()).size() != 4) {
.withHandle(handle -> handle.createQuery("show tables").mapTo(String.class).list()).size() != 5) {
//Disable any event that uses the db
if (DiscordCfgFactory.getConfig().getUserTrackingEvent() != null) {
SoaLogging.getLogger(this).warn("Database structure does not seem right, disabling db events");
DiscordCfgFactory.getConfig().getUserTrackingEvent().setEnabled(false);
DiscordCfgFactory.getConfig().getClanCompareEvent().setEnabled(false);
}
}
} else {
//Disable any event that uses the db
if (DiscordCfgFactory.getConfig().getUserTrackingEvent() != null) {
SoaLogging.getLogger(this).warn("Database configuration not present, disabling any db events");
DiscordCfgFactory.getConfig().getUserTrackingEvent().setEnabled(false);
DiscordCfgFactory.getConfig().getClanCompareEvent().setEnabled(false);
}
}

Expand Down
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;
}
}
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;
}
}
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());
}
}
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;
}
}
4 changes: 4 additions & 0 deletions soa-discord/src/main/resources/commands/clancompare.json
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."
}
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
}
]
}
4 changes: 4 additions & 0 deletions soa-discord/src/main/resources/logcfg.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@
<AppenderRef ref="Console"/>
<AppenderRef ref="MyFile"/>
</Logger>
<Logger name="com.github.soajeff.clancompare" level="debug" additivity="false">
<AppenderRef ref="Console"/>
<AppenderRef ref="MyFile"/>
</Logger>
</Loggers>
</Configuration>
13 changes: 13 additions & 0 deletions soa-discord/src/main/xsd/discordConfiguration.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<xsd:element name="admin-event" type="AdminEvent" minOccurs="0" maxOccurs="1"/>
<xsd:element name="user-tracking-event" type="UserTrackingEvent" minOccurs="0" maxOccurs="1"/>
<xsd:element name="rs-news-task" type="RsListingEvent" minOccurs="0" maxOccurs="1"/>
<xsd:element name="clan-compare-event" type="ClanCompareEvent" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
Expand Down Expand Up @@ -98,4 +99,16 @@
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="ClanCompareEvent">
<xsd:complexContent>
<xsd:extension base="BaseEvent">
<xsd:sequence>
<xsd:element name="forums-api-url" type="xsd:string" minOccurs="1" maxOccurs="1"/>
<xsd:element name="forums-api-key" type="xsd:string" minOccurs="1" maxOccurs="1"/>
<xsd:element name="rs-clan-url" type="xsd:string" minOccurs="1" maxOccurs="1"/>
<xsd:element name="competitions-url" type="xsd:string" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:schema>
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);
}

}

0 comments on commit 1110d49

Please sign in to comment.