Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Project-specific stuff

src/main/resources/configuration.json


# User-specific stuff
.idea/

Expand Down
11 changes: 11 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Contributing

I'm more than happy for people to contribute.
I'm following git-flow for the purposes of this project. If you have maintainer access please create a new branch with a name which indicates the purpose of the branch, i.e what it implements. Then proceed to create a pull request based on the develop branch, if it doesn't exist create the develop branch first.

Delete your branches once they're merged into master.

For external contributors, fork the repository and follow the same principals, create a new branch, new PR based on develop, etc etc. I'll get to it when I can.

## Hacktoberfest 2020
This project was developed during Hacktoberfest 2020. We welcome participants to contribute, as it will count towards for PR count.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
# TCD-Discord-Bot

Hacktoberfest project 2020

For the Trinity Class of '24' Discord.
https://discord.gg/9PuBCaD

In order to deploy this yourself, you have to rename `configuration.json.example` to `configuration.json` and add a token aquired from the [developers portal](https://discord.com/developers)

69 changes: 65 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,62 @@
<artifactId>tcd-bot</artifactId>
<version>0.1.0-SNAPSHOT</version>

<repositories>
<repository>
<id>jcenter</id>
<name>jcenter-bintray</name>
<url>https://jcenter.bintray.com</url>
</repository>

<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.21</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>4.2.0_184</version>
<exclusions>
<exclusion>
<groupId>club.minnced</groupId>
<artifactId>opus-java</artifactId>
</exclusion>
</exclusions>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>com.jagrosh</groupId>
<artifactId>jda-utilities</artifactId>
<version>3.0.4</version>
<scope>compile</scope>
<type>pom</type>
</dependency>
</dependencies>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
Expand All @@ -18,6 +74,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
Expand All @@ -30,17 +87,21 @@
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<minimizeJar>false</minimizeJar>
<transformers>
<transformer implementation=
"org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>io.paradaux.tcdbot.TCDBot</mainClass>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
</transformer>
</transformers>
<createDependencyReducedPom>false</createDependencyReducedPom>
<finalName>${project.artifactId}</finalName>
</configuration>
</execution>
</executions>
Expand Down
90 changes: 89 additions & 1 deletion src/main/java/io/paradaux/tcdbot/TCDBot.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,97 @@
package io.paradaux.tcdbot;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.stream.JsonReader;
import com.jagrosh.jdautilities.command.CommandClient;
import com.jagrosh.jdautilities.command.CommandClientBuilder;
import io.paradaux.tcdbot.api.ConfigurationCache;
import io.paradaux.tcdbot.api.FileUtils;
import io.paradaux.tcdbot.listeners.MessageReceivedListener;
import io.paradaux.tcdbot.listeners.ReadyListener;

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.utils.cache.CacheFlag;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.security.krb5.Config;

import javax.security.auth.login.LoginException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

public class TCDBot {

private static Logger logger;
public static Logger getLogger() { return logger; }

private static JDA client;

private static ConfigurationCache configurationCache;
public static ConfigurationCache getConfigurationCache() { return configurationCache; }

public static void main(String[] args) {
System.out.println("Hi");
TCDBot bot = new TCDBot();
}

public TCDBot() {
logger = LoggerFactory.getLogger(getClass());
logger.info("TCDBot is starting.");

if (!new File("configuration.yml").exists()) {
try {
FileUtils.ExportResource("configuration.json");
} catch (Exception exception) {
logger.error("Failed to deploy configuration.\n" + exception.toString());
return;
}

}

try {
configurationCache = readConfigurationFile();
} catch (FileNotFoundException exception) {
logger.error("Cannot find the configuration file.");
return;
}

client = login(configurationCache.getToken());
}

@Nullable
public JDA login(String token) {
JDABuilder builder = JDABuilder.createDefault(token)
.disableCache(CacheFlag.MEMBER_OVERRIDES, CacheFlag.VOICE_STATE)
.setBulkDeleteSplittingEnabled(false)
.addEventListeners(new ReadyListener(logger), new MessageReceivedListener(configurationCache));

if (token == null) {
logger.warn("You have not set the token for your discord bot for discord2mc functionality. This has been disabled.");
return null;
}

try {
return builder.build();
} catch(LoginException exception) {
logger.warn("TCDBot failed to login with the provided token.");
return null;
}
}

public ConfigurationCache readConfigurationFile() throws FileNotFoundException {
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();

BufferedReader bufferedReader = new BufferedReader(new FileReader("configuration.json"));
return gson.fromJson(bufferedReader, ConfigurationCache.class);
}

public CommandClient createCommandClient() {
CommandClientBuilder builder = new CommandClientBuilder();
return null;
}
}
47 changes: 47 additions & 0 deletions src/main/java/io/paradaux/tcdbot/api/ConfigurationCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.paradaux.tcdbot.api;

import java.util.List;

public class ConfigurationCache {

String token;
String prefix;
List<String> courseSelectionChannels;

public ConfigurationCache(String token, String prefix, List<String> courseSelectionChannels) {
this.token = token;
this.prefix = prefix;
this.courseSelectionChannels = courseSelectionChannels;
}

public ConfigurationCache() {}

public String getToken() {
return token;
}

public void setToken(String token) {
this.token = token;
}

public String getPrefix() {
return prefix;
}

public void setPrefix(String prefix) {
this.prefix = prefix;
}

public List<String> getCourseSelectionChannels() {
return courseSelectionChannels;
}

public void setCourseSelectionChannels(List<String> courseSelectionChannels) {
this.courseSelectionChannels = courseSelectionChannels;
}

@Override
public String toString() {
return "ConfigurationCache: [ token: " + token + "]";
}
}
45 changes: 45 additions & 0 deletions src/main/java/io/paradaux/tcdbot/api/FileUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.paradaux.tcdbot.api;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class FileUtils {

/**
* Export a resource embedded into a Jar file to the local file path.
*
* @param resourceName ie.: "/SmartLibrary.dll"
* @return The path to the exported resource
* @throws Exception
*/
public static String ExportResource(String resourceName) throws Exception {
InputStream stream = null;
OutputStream resStreamOut = null;
String jarFolder;
try {
stream = FileUtils.class.getResourceAsStream(resourceName);//note that each / is a directory down in the "jar tree" been the jar the root of the tree
if(stream == null) {
throw new Exception("Cannot get resource \"" + resourceName + "\" from Jar file.");
}

int readBytes;
byte[] buffer = new byte[4096];
jarFolder = new File(FileUtils.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getParentFile().getPath().replace('\\', '/');
resStreamOut = new FileOutputStream(jarFolder + resourceName);
while ((readBytes = stream.read(buffer)) > 0) {
resStreamOut.write(buffer, 0, readBytes);
}
} catch (Exception ex) {
throw ex;
} finally {
stream.close();
resStreamOut.close();
}

return jarFolder + resourceName;
}


}
18 changes: 18 additions & 0 deletions src/main/java/io/paradaux/tcdbot/commands/InviteCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.paradaux.tcdbot.commands;

import com.jagrosh.jdautilities.command.Command;
import com.jagrosh.jdautilities.command.CommandEvent;

public class InviteCommand extends Command {

public InviteCommand(String name, String[] aliases, String help) {
this.name = "invite";
this.aliases = new String[] {"inv", "i"};
this.help = "Gives an invite link.";
}

@Override
protected void execute(CommandEvent commandEvent) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.paradaux.tcdbot.listeners;

import io.paradaux.tcdbot.api.ConfigurationCache;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull;

public class MessageReceivedListener extends ListenerAdapter {

ConfigurationCache configurationCache;

public MessageReceivedListener(ConfigurationCache configurationCache) {
this.configurationCache = configurationCache;
}

@Override
public void onMessageReceived(@NotNull MessageReceivedEvent event) {
MessageChannel channel = event.getMessage().getChannel();
}
}
21 changes: 21 additions & 0 deletions src/main/java/io/paradaux/tcdbot/listeners/ReadyListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.paradaux.tcdbot.listeners;

import net.dv8tion.jda.api.events.ReadyEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.slf4j.Logger;

public class ReadyListener extends ListenerAdapter {

Logger logger;

public ReadyListener(Logger logger) {
this.logger = logger;
}

@Override
public void onReady(ReadyEvent event) {
logger.info("TCDBot has logged in successfully.");
logger.info("Serving " + event.getGuildTotalCount() + " guilds.");
}

}
5 changes: 5 additions & 0 deletions src/main/resources/configuration.json.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"token": "NzYyMzrthMrthwMjQ2OTc1r0MjEz.X3nlXg.DtBrEKhXZ8nvV4rgego6c9VtEtUKw",
"prefix": ";",
"course-selection-channel": ["730441682817908797", "732213443104604251"]
}