Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that you want to show how it works but this command should be disabled in the actual bot. It could lead to people abusing it and trying to fill the server harddrive unecessary.


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);
}
}
}
19 changes: 11 additions & 8 deletions src/main/java/org/togetherjava/discordbot/config/TjBotConfig.java
Original file line number Diff line number Diff line change
@@ -1,30 +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, String moderationChannel, 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!");
this.moderationChannel = Objects.requireNonNull(moderationChannel, "channel can not be null!");

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

/**
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> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this klass seems to be a baseclass of Jooq implementations. I'd rename this class so that the name reflects what this class represents.


protected DSLContext dslContext;

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

}
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
);
2 changes: 2 additions & 0 deletions src/main/resources/sample-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# The command prefixes
prefixes: ["!", "?"]
botToken: "your token"
# the url for the database connection
dburl: "jdbc:sqlite:yourfile.sqlite"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enter the same db url as in the pom. Since it did not seem to work with 2 different urls.


# channel for modmail
moderationChannel: "your channel"
Expand Down