Skip to content

Commit

Permalink
Together-Java#9 Database setup
Browse files Browse the repository at this point in the history
  • Loading branch information
acathers committed Jan 21, 2020
1 parent 179d379 commit e31571b
Show file tree
Hide file tree
Showing 11 changed files with 287 additions and 10 deletions.
94 changes: 94 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

<properties>
<jbock.version>2.8.0</jbock.version>
<db.jdbc.url>jdbc:sqlite:tjbot.sqlite</db.jdbc.url>
</properties>

<build>
Expand Down Expand Up @@ -45,6 +46,59 @@
<finalName>${project.name}</finalName>
</configuration>
</plugin>

<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<version>3.12.3</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<jdbc>
<driver>org.sqlite.JDBC</driver>
<url>${db.jdbc.url}</url>
</jdbc>
<generator>
<database>
<name>org.jooq.meta.sqlite.SQLiteDatabase</name>
<includes>.*</includes>
</database>
<generate>
<daos>true</daos>
</generate>
<target>
<packageName>org.togetherjava.discordbot.db.autogen</packageName>
<directory>target/generated-sources/jooq</directory>
</target>
</generator>
</configuration>
</plugin>

<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>6.1.3</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>migrate</goal>
</goals>
</execution>
</executions>
<configuration>
<url>${db.jdbc.url}</url>
<locations>
<location>filesystem:src/main/resources/db</location>
</locations>
</configuration>
</plugin>
</plugins>
</build>

Expand All @@ -55,14 +109,23 @@
<version>-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<type>maven-plugin</type>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
Expand All @@ -72,6 +135,7 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j18-impl</artifactId>
Expand All @@ -83,6 +147,36 @@
<version>${jbock.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq</artifactId>
<version>3.12.3</version>
</dependency>

<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq-meta</artifactId>
<version>3.12.3</version>
</dependency>

<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen</artifactId>
<version>3.12.3</version>
</dependency>

<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.30.1</version>
</dependency>

<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>6.1.3</version>
</dependency>
</dependencies>

<repositories>
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/togetherjava/discordbot/BotMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.togetherjava.discordbot.cli.CliArgumentSpecification_Parser;
import org.togetherjava.discordbot.commands.CommandListener;
import org.togetherjava.discordbot.config.TjBotConfig;
import org.togetherjava.discordbot.events.modmail.ModMailListener;
import org.togetherjava.discordbot.util.Messages;

public class BotMain {
Expand All @@ -30,10 +31,11 @@ public static void main(String[] args) throws Exception {
logger.info("Starting JDA");
JDA jda = new JDABuilder(AccountType.BOT)
.setToken(config.getAndDeleteBotToken())
.addEventListeners()
.build()
.awaitReady();
jda.addEventListener(new ModMailListener(jda, config));
jda.addEventListener(new CommandListener(config, messages, jda));

logger.info("Started JDA");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import de.ialistannen.commandprocrastination.parsing.ParseException;
import javax.annotation.Nonnull;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.ChannelType;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.togetherjava.discordbot.commands.CommandContext.JdaRequestContext;
Expand Down Expand Up @@ -42,9 +43,10 @@ public CommandListener(TjBotConfig config, Messages messages, JDA jda) {

@Override
public void onMessageReceived(@Nonnull MessageReceivedEvent event) {
if (event.getAuthor().isBot()) {
if (event.getAuthor().isBot() || event.isFromType(ChannelType.PRIVATE)) {
return;
}

String content = event.getMessage().getContentRaw();
try {
JdaRequestContext context = new JdaRequestContext(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.togetherjava.discordbot.commands.commands;

import de.ialistannen.commandprocrastination.autodiscovery.ActiveCommand;
import de.ialistannen.commandprocrastination.command.tree.CommandNode;
import de.ialistannen.commandprocrastination.parsing.ParseException;
import org.togetherjava.discordbot.commands.CommandContext;
import org.togetherjava.discordbot.db.autogen.tables.pojos.Test;
import org.togetherjava.discordbot.db.repositories.ExampleRepository;

import static de.ialistannen.commandprocrastination.parsing.defaults.StringParsers.greedyPhrase;

@ActiveCommand(name = "example", parentClass = BasePrefixCommand.class)
@SuppressWarnings("unused")
public class ExampleCommandDB extends CommandNode<CommandContext> {

private ExampleRepository repository;

@SuppressWarnings("unused")
public ExampleCommandDB(CommandContext context) {
super("example");
repository = new ExampleRepository(context);
setCommand(this::execute);
}

private void execute(CommandContext context) throws ParseException {
repository.add(new Test(null, context.getRequestContext().getUser().getName(), context.shift(greedyPhrase())));
for(Test value: repository.getAll()){
context.getRequestContext().getChannel().sendMessage(value.getText()).queue();
}

for(Test value: repository.getAll()){
value.setText(value.getText() + "UPDATE TEST");
repository.update(value);
}
}
}
26 changes: 19 additions & 7 deletions src/main/java/org/togetherjava/discordbot/config/TjBotConfig.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
package org.togetherjava.discordbot.config;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Objects;

/**
* The pojo for the main config file for this bot.
*/
public class TjBotConfig {

private List<String> prefixes;
@JsonProperty("botToken")
private String botToken;
private CommandConfig commands;
private String moderationChannel;
private String dburl;

@JsonCreator
public TjBotConfig(List<String> prefixes, String botToken, CommandConfig commands) {
this.prefixes = Objects.requireNonNull(prefixes, "prefixes can not be null!");
this.botToken = Objects.requireNonNull(botToken, "botToken can not be null!");
this.commands = Objects.requireNonNull(commands, "commands can not be null!");

/**
* Returns the database url
*
* @return the database url
*/
public String getDburl() {
return dburl;
}

/**
Expand All @@ -43,6 +48,13 @@ public CommandConfig getCommands() {
return commands;
}

/**
* Returns the moderation channel for modmail.
*
* @return the moderation channel for modmail.
*/
public String getModerationChannel() { return moderationChannel; }

/**
* Returns the token and deletes it from the config.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.togetherjava.discordbot.db.repositories;

import org.togetherjava.discordbot.commands.CommandContext;
import org.togetherjava.discordbot.db.autogen.tables.daos.TestDao;
import org.togetherjava.discordbot.db.autogen.tables.pojos.Test;
import org.togetherjava.discordbot.db.repository.SimpleRepository;
import java.util.List;

/**
* can use the dao or the DSLContext to write queries, the dao having most things pre written
*/
public class ExampleRepository extends SimpleRepository<Test> {

private TestDao dao;

public ExampleRepository(CommandContext context){
super(context);
dao = new TestDao(dslContext.configuration());
}

@Override
public void add(Test item) {
dao.insert(item);
}

@Override
public void update(Test item) {
dao.update(item);
}

@Override
public void remove(Test item) {
dao.delete(item);
}

@Override
public List<Test> getAll() {
return dao.findAll();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.togetherjava.discordbot.db.repository;

import java.util.List;

public interface Repository<T> {

void add(T item);

void update(T item);

void remove(T item);

List<T> getAll();

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.togetherjava.discordbot.db.repository;

import org.jooq.*;
import org.jooq.impl.DSL;
import org.togetherjava.discordbot.commands.CommandContext;

public abstract class SimpleRepository<T> implements Repository<T> {

protected DSLContext dslContext;

public SimpleRepository(CommandContext context) {
dslContext = DSL.using(context.getConfig().getDburl());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.togetherjava.discordbot.events.modmail;

import lombok.NonNull;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.events.message.priv.PrivateMessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.togetherjava.discordbot.config.TjBotConfig;
import javax.annotation.Nonnull;
import java.util.List;



public class ModMailListener extends ListenerAdapter {
private TextChannel moderation;

public ModMailListener(JDA jda, TjBotConfig config) {
List<TextChannel> channels = jda.getTextChannelsByName(config.getModerationChannel(), false);
if(channels.isEmpty()){
throw new IllegalArgumentException("Moderation channel is invalid, does not exist");
}
moderation = channels.get(0);
}

@Override
public void onPrivateMessageReceived(@Nonnull PrivateMessageReceivedEvent event) {
if (!event.getAuthor().isBot()) {
String message = "<@" + event.getAuthor().getId() + ">: " + event.getMessage().getContentRaw();
moderation.sendMessage(message).queue();
}
}

@Override
public void onGuildMessageReceived(@NonNull GuildMessageReceivedEvent event) {
if(!event.getMessage().getTextChannel().equals(moderation)){
return;
}

List<Member> mentioned = event.getMessage().getMentionedMembers();
if (!event.getAuthor().isBot() && !mentioned.isEmpty()) {
mentioned.get(0).getUser().openPrivateChannel().queue((channel) ->
channel.sendMessage(event.getMessage()).queue());
}
}
}
6 changes: 6 additions & 0 deletions src/main/resources/db/V1__test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE IF NOT EXISTS TEST
(
ID INTEGER AUTO_INCREMENT PRIMARY KEY,
MEMBER varchar(30) NOT NULL,
TEXT varchar(30) NOT NULL
);
Loading

0 comments on commit e31571b

Please sign in to comment.