Skip to content

Commit

Permalink
Add basic ping-pong function
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonBailey committed Jun 12, 2024
1 parent 2e16b4c commit a32891a
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 2 deletions.
25 changes: 25 additions & 0 deletions .idea/runConfigurations/wheel_terra__bootRun_.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions terra/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ group of chat rooms. If you know Discord, then you know what a server refers to
waste my time explaining it lol. That said, I do know that the Discord API calls them "guilds" so
things will probably get a little confusing. (Especially considering [Guilds][3] are a new feature).

## Development

1. To run, `./gradlew :terra:bootRun --args='--spring.profiles.active=live'`
2. To set some default profiles,
`tasks.named<BootRun>("bootRun") { systemProperty("spring.profiles.active", "live") }`
3. To run with Spring's debug mode, `./gradlew :terra:bootRun --args='--debug'`, [see here][5]
4. Run the jar directly,
`./gradlew :terra:bootJar && java -jar terra/build/libs/terra.jar --spring.profiles.active=live`

## Notes

I am going to use Spring Boot's [Dependency Management Plugin][4] instead of trying to use the
Expand Down Expand Up @@ -53,3 +62,5 @@ Execution failed for task ':discord:generateMetadataFileForMavenJavaPublication'
[3]: https://support.discord.com/hc/en-us/articles/23187611406999-Guilds-FAQ

[4]: https://docs.spring.io/dependency-management-plugin/docs/current/reference/html/

[5]: https://stackoverflow.com/a/71402717/1858327
8 changes: 8 additions & 0 deletions terra/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import org.springframework.boot.gradle.tasks.bundling.BootJar
import org.springframework.boot.gradle.tasks.run.BootRun

plugins {
id("dev.jacksonbailey.wheel.java-application-conventions")
id("dev.jacksonbailey.wheel.shut-up-javadoc")
Expand Down Expand Up @@ -29,3 +32,8 @@ dependencies {
testImplementation("org.springframework.boot:spring-boot-test")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}

tasks.getByName<BootJar>("bootJar") {
// TODO Does this mess up publishing somehow?
this.archiveFileName.set("${archiveBaseName.get()}.${archiveExtension.get()}")
}
43 changes: 43 additions & 0 deletions terra/src/main/java/dev/jacksonbailey/wheel/terra/PingPong.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package dev.jacksonbailey.wheel.terra;

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class PingPong extends ListenerAdapter {

private static final Logger log = LoggerFactory.getLogger(PingPong.class);

private final JDA jda;

public PingPong(JDA jda) {
this.jda = jda;
// TODO This is wrong wrong wrong!
jda.addEventListener(this);
}

@Override
public void onMessageReceived(MessageReceivedEvent event) {
if (event.getAuthor().isBot()) {
return;
}
// We don't want to respond to other bot accounts, including ourself
Message message = event.getMessage();
String content = message.getContentRaw();
// getContentRaw() is an atomic getter
// getContentDisplay() is a lazy getter which modifies the content for e.g. console view (strip
// discord formatting)
if (content.equals("!ping")) {
MessageChannel channel = event.getChannel();
channel.sendMessage("Pong!")
.queue(); // Important to call .queue() on the RestAction returned by sendMessage(...)
}
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package dev.jacksonbailey.wheel.terra;

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.requests.GatewayIntent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
public class TerraConfig {

@Bean
public JDABuilder jdaBuilder(TerraConfigProps terraConfigProps) {
return JDABuilder.createDefault(terraConfigProps.token());
@Profile("live")
public JDA jda(TerraConfigProps terraConfigProps) {
return JDABuilder.createDefault(terraConfigProps.token())
.enableIntents(GatewayIntent.MESSAGE_CONTENT)
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dev.jacksonbailey.wheel.terra;

import static org.mockito.Mockito.mock;

import net.dv8tion.jda.api.JDA;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
public class TerraTestConfig {

@Bean
@Profile("!live")
public JDA jda() {
return mock(JDA.class);
}

}

0 comments on commit a32891a

Please sign in to comment.