basic tests to understand api

This commit is contained in:
Harrison Deng 2017-08-17 14:33:59 -05:00
parent 9d24d357ff
commit 0074b27204
9 changed files with 247 additions and 50 deletions

View File

@ -6,23 +6,13 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>

View File

@ -1,5 +1,12 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
org.eclipse.jdt.core.compiler.compliance=1.5
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.5
org.eclipse.jdt.core.compiler.source=1.8

View File

@ -1,33 +1,26 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>zer01hd.zer10hdbot</groupId>
<artifactId>zer10hdbot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Zer10HDBot</name>
<description>A discord bot that has no main goal to be honest.</description>
<repositories>
<repository>
<id>javacord-repo</id>
<url>http://repo.bastian-oppermann.de</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>de.btobastian.javacord</groupId>
<artifactId>javacord</artifactId>
<version>2.0.15</version>
<!-- This will use the shaded javacord which contains all required dependencies -->
<classifier>shaded</classifier>
</dependency>
<!-- A SLF4J comaptible logging framework. I would recommend to use logback -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.3</version>
</dependency>
</dependencies>
<modelVersion>4.0.0</modelVersion>
<groupId>zer01hd.zer10hdbot</groupId>
<artifactId>zer10hdbot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Zer10HDBot</name>
<description>A discord bot that has no purpose to be honest.</description>
<repositories>
<repository>
<!-- This repo fixes issues with transitive dependencies -->
<id>jcenter</id>
<url>http://jcenter.bintray.com</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>3.2.0_242</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,81 @@
package zer01hd.zer10hdbot;
import java.util.Scanner;
import javax.security.auth.login.LoginException;
import net.dv8tion.jda.core.AccountType;
import net.dv8tion.jda.core.JDABuilder;
import net.dv8tion.jda.core.exceptions.RateLimitedException;
public class Main {
public static void main(String[] args) {
boolean alive = true;
Scanner in = new Scanner(System.in);
boolean ninja = false;
String token = null;
if (args != null) {
for (int argID = 0; argID < args.length; argID++) {
if (args[argID].equals("-ninja")) {
ninja = true;
} else if (args[argID].equals("-token")) {
argID++;
token = args[argID];
}
}
}
JDABuilder jdab;
if (!ninja) {
jdab = new JDABuilder(AccountType.BOT);
} else {
jdab = new JDABuilder(AccountType.CLIENT);
}
if (token != null) {
jdab.setToken(token);
} else {
jdab.setToken("MzQ3NjQyODI2Nzc3MTY1ODI0.DHbXfA.I-puwf_NHIn0ap7Q1BaGsb2_VIU");
}
try {
Zer10HDBot z10b = new Zer10HDBot(jdab);
while (alive) {
System.out.println("Awaiting for next command...");
String command = in.nextLine().toLowerCase();
if (command.equals("initiate")) {
System.out.println("attempting initiation.\n");
z10b.initiate();
} else if (command.equals("connect")) {
z10b.connect(jdab);
} else if (command.equals("setup listeners")) {
z10b.setupListeners();
} else if (command.equals("stop")) {
z10b.jda.shutdown();
System.out.println("Shutting things down!");
alive = false;
in.close();
} else if (command.equals("force-shutdown")) {
z10b.jda.shutdownNow();
System.out.println("Shutting things down with force!");
alive = false;
in.close();
} else if (command.equals("set prefix")) {
System.out.println("please enter the prefix you want: ");
z10b.cmdPrefix = in.nextLine();
System.out.println("command prefix set: " + z10b.cmdPrefix);
}
}
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (LoginException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RateLimitedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

View File

@ -1,5 +0,0 @@
package zer01hd.zer10hdbot;
public class Zer01HDBot {
}

View File

@ -0,0 +1,52 @@
package zer01hd.zer10hdbot;
import java.util.Arrays;
import java.util.List;
import javax.security.auth.login.LoginException;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.JDABuilder;
import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.events.Event;
import net.dv8tion.jda.core.events.ReadyEvent;
import net.dv8tion.jda.core.exceptions.RateLimitedException;
import net.dv8tion.jda.core.hooks.EventListener;
import zer01hd.zer10hdbot.listeners.MessageListener;
import zer01hd.zer10hdbot.listeners.SelfEventListener;
public class Zer10HDBot {
public JDA jda;
public String cmdPrefix = ".";
public Zer10HDBot(JDABuilder jdab) throws LoginException, IllegalArgumentException, RateLimitedException {
jdab.addEventListener(new EventListener() {
public void onEvent(Event arg0) {
if (arg0 instanceof ReadyEvent) {
initiate();
System.out.println("Build complete! Zer10Bot is now ready for action!");
setupListeners();
}
}
});
connect(jdab);
}
public void setupListeners() {
jda.addEventListener(new MessageListener(this), new SelfEventListener());
}
public void connect(JDABuilder jdab) throws LoginException, IllegalArgumentException, RateLimitedException {
System.out.println("attempting build.");
this.jda = jdab.buildAsync();
}
public void initiate() {
if (jda != null) {
List<Guild> guilds = jda.getGuilds();
System.out.println("Connected to the following servers: " + Arrays.toString(guilds.toArray()));
} else {
System.out.println("JDA didn't finish building the bot yet...");
}
}
}

View File

@ -0,0 +1,51 @@
package zer01hd.zer10hdbot.listeners;
import java.util.Random;
import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.entities.MessageChannel;
import net.dv8tion.jda.core.entities.User;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
import net.dv8tion.jda.core.hooks.ListenerAdapter;
import zer01hd.zer10hdbot.Zer10HDBot;
public class MessageListener extends ListenerAdapter {
Zer10HDBot z10b;
public MessageListener(Zer10HDBot z10b) {
this.z10b = z10b;
}
@Override
public void onMessageReceived(MessageReceivedEvent event) {
User user = event.getAuthor();
Message message = event.getMessage();
MessageChannel channel = event.getChannel();
String msg = message.getContent();
if (msg.equals(z10b.cmdPrefix)) {
channel.sendMessage("Fool! Your doing it all wrong. You need an actual command after that.").queue();
} else if (msg.equalsIgnoreCase("ping") || msg.equalsIgnoreCase(z10b.cmdPrefix+"ping")) {
channel.sendMessage("pong").queue();
} else if (msg.startsWith(z10b.cmdPrefix)) {
if (msg.substring(z10b.cmdPrefix.length()).equalsIgnoreCase("roll")) {
Random rand = new Random();
int roll = rand.nextInt(6) + 1;
channel.sendMessage("Your roll: " + roll).queue(sentMessage -> {
switch (roll) {
case 2:
channel.sendMessage("bad luck").queue();
break;
case 4:
break;
case 6:
channel.sendMessage("nice roll!").queue();
break;
}
});
}
}
super.onMessageReceived(event);
}
}

View File

@ -0,0 +1,16 @@
package zer01hd.zer10hdbot.listeners;
import net.dv8tion.jda.core.entities.TextChannel;
import net.dv8tion.jda.core.events.guild.GuildJoinEvent;
import net.dv8tion.jda.core.hooks.ListenerAdapter;
public class SelfEventListener extends ListenerAdapter {
@Override
public void onGuildJoin(GuildJoinEvent event) {
TextChannel channel = event.getGuild().getPublicChannel();
if (channel.canTalk()) {
channel.sendMessage("YHD's doppelgänger is here! Sort of. Limited functionality for now.").queue();
}
super.onGuildJoin(event);
}
}

View File

@ -0,0 +1,12 @@
package zer01hd.zer10hdbot.listeners;
import net.dv8tion.jda.client.events.group.GroupUserJoinEvent;
import net.dv8tion.jda.core.hooks.ListenerAdapter;
public class ServerEventListener extends ListenerAdapter {
@Override
public void onGroupUserJoin(GroupUserJoinEvent event) {
super.onGroupUserJoin(event);
}
}