diff --git a/pom.xml b/pom.xml index 27679ec..483c9ec 100644 --- a/pom.xml +++ b/pom.xml @@ -16,6 +16,7 @@ 2.8.0 + jdbc:sqlite:tjbot.sqlite @@ -45,6 +46,59 @@ ${project.name} + + + org.jooq + jooq-codegen-maven + 3.12.3 + + + generate-sources + + generate + + + + + + org.sqlite.JDBC + ${db.jdbc.url} + + + + org.jooq.meta.sqlite.SQLiteDatabase + .* + + + true + + + org.togetherjava.discordbot.db.autogen + target/generated-sources/jooq + + + + + + + org.flywaydb + flyway-maven-plugin + 6.1.3 + + + generate-sources + + migrate + + + + + ${db.jdbc.url} + + filesystem:src/main/resources/db + + + @@ -55,14 +109,23 @@ -SNAPSHOT + + org.codehaus.mojo + properties-maven-plugin + 1.0.0 + maven-plugin + + com.fasterxml.jackson.core jackson-databind + com.fasterxml.jackson.dataformat jackson-dataformat-yaml + com.fasterxml.jackson.module jackson-module-parameter-names @@ -72,6 +135,7 @@ org.slf4j slf4j-api + org.apache.logging.log4j log4j-slf4j18-impl @@ -83,6 +147,36 @@ ${jbock.version} provided + + + org.jooq + jooq + 3.12.3 + + + + org.jooq + jooq-meta + 3.12.3 + + + + org.jooq + jooq-codegen + 3.12.3 + + + + org.xerial + sqlite-jdbc + 3.30.1 + + + + org.flywaydb + flyway-core + 6.1.3 + diff --git a/src/main/java/org/togetherjava/discordbot/BotMain.java b/src/main/java/org/togetherjava/discordbot/BotMain.java index 8900186..80f6c5c 100644 --- a/src/main/java/org/togetherjava/discordbot/BotMain.java +++ b/src/main/java/org/togetherjava/discordbot/BotMain.java @@ -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 { @@ -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"); } } diff --git a/src/main/java/org/togetherjava/discordbot/commands/CommandListener.java b/src/main/java/org/togetherjava/discordbot/commands/CommandListener.java index cc32ee8..1f91f09 100644 --- a/src/main/java/org/togetherjava/discordbot/commands/CommandListener.java +++ b/src/main/java/org/togetherjava/discordbot/commands/CommandListener.java @@ -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; @@ -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( diff --git a/src/main/java/org/togetherjava/discordbot/commands/commands/ExampleCommandDB.java b/src/main/java/org/togetherjava/discordbot/commands/commands/ExampleCommandDB.java new file mode 100644 index 0000000..a1f73a7 --- /dev/null +++ b/src/main/java/org/togetherjava/discordbot/commands/commands/ExampleCommandDB.java @@ -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 { + + 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); + } + } +} diff --git a/src/main/java/org/togetherjava/discordbot/config/TjBotConfig.java b/src/main/java/org/togetherjava/discordbot/config/TjBotConfig.java index ded3639..7ac5182 100644 --- a/src/main/java/org/togetherjava/discordbot/config/TjBotConfig.java +++ b/src/main/java/org/togetherjava/discordbot/config/TjBotConfig.java @@ -1,13 +1,12 @@ 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. @@ -15,14 +14,20 @@ public class TjBotConfig { private List prefixes; + @JsonProperty("botToken") private String botToken; private CommandConfig commands; + private String moderationChannel; + private String dburl; - @JsonCreator - public TjBotConfig(List 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; } /** @@ -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. * diff --git a/src/main/java/org/togetherjava/discordbot/db/repositories/ExampleRepository.java b/src/main/java/org/togetherjava/discordbot/db/repositories/ExampleRepository.java new file mode 100644 index 0000000..bdb80b8 --- /dev/null +++ b/src/main/java/org/togetherjava/discordbot/db/repositories/ExampleRepository.java @@ -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 { + + 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 getAll() { + return dao.findAll(); + } + +} diff --git a/src/main/java/org/togetherjava/discordbot/db/repository/Repository.java b/src/main/java/org/togetherjava/discordbot/db/repository/Repository.java new file mode 100644 index 0000000..6001274 --- /dev/null +++ b/src/main/java/org/togetherjava/discordbot/db/repository/Repository.java @@ -0,0 +1,16 @@ +package org.togetherjava.discordbot.db.repository; + +import java.util.List; + +public interface Repository { + + void add(T item); + + void update(T item); + + void remove(T item); + + List getAll(); + +} + diff --git a/src/main/java/org/togetherjava/discordbot/db/repository/SimpleRepository.java b/src/main/java/org/togetherjava/discordbot/db/repository/SimpleRepository.java new file mode 100644 index 0000000..57fde40 --- /dev/null +++ b/src/main/java/org/togetherjava/discordbot/db/repository/SimpleRepository.java @@ -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 implements Repository { + + protected DSLContext dslContext; + + public SimpleRepository(CommandContext context) { + dslContext = DSL.using(context.getConfig().getDburl()); + } + +} diff --git a/src/main/java/org/togetherjava/discordbot/events/modmail/ModMailListener.java b/src/main/java/org/togetherjava/discordbot/events/modmail/ModMailListener.java new file mode 100644 index 0000000..7f2af06 --- /dev/null +++ b/src/main/java/org/togetherjava/discordbot/events/modmail/ModMailListener.java @@ -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 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 mentioned = event.getMessage().getMentionedMembers(); + if (!event.getAuthor().isBot() && !mentioned.isEmpty()) { + mentioned.get(0).getUser().openPrivateChannel().queue((channel) -> + channel.sendMessage(event.getMessage()).queue()); + } + } +} diff --git a/src/main/resources/db/V1__test.sql b/src/main/resources/db/V1__test.sql new file mode 100644 index 0000000..472b10f --- /dev/null +++ b/src/main/resources/db/V1__test.sql @@ -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 +); diff --git a/src/main/resources/sample-config.yml b/src/main/resources/sample-config.yml index e4092ae..438a703 100644 --- a/src/main/resources/sample-config.yml +++ b/src/main/resources/sample-config.yml @@ -3,10 +3,16 @@ # The command prefixes prefixes: ["!", "?"] botToken: "your token" +# the url for the database connection +dburl: "jdbc:sqlite:yourfile.sqlite" + +# channel for modmail +moderationChannel: "your channel" # Command settings commands: # Configuration for the free command free: # The regex used to detect help channels for the free command - helpChannelRegex: ".*" \ No newline at end of file + helpChannelRegex: ".*" +