Skip to content
Merged
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
Empty file added input.nbs
Empty file.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.diamondfire.helpbot.bot;

import com.diamondfire.helpbot.HelpBot;
import com.diamondfire.helpbot.bot.command.CommandHandler;
import com.diamondfire.helpbot.bot.command.impl.codeblock.*;
import com.diamondfire.helpbot.bot.command.impl.other.*;
Expand Down Expand Up @@ -109,7 +108,9 @@ public static void initialize() throws LoginException {
new ExcuseStaffCommand(),
new ExcusedStaffCommand(),
new SupportBannedPlayersCommand(),
new DiscussionMuteCommand()
new DiscussionMuteCommand(),
new NbsCommand(),
new DailySessionsCommand()
);

JDABuilder builder = JDABuilder.createDefault(config.getToken())
Expand Down Expand Up @@ -140,4 +141,4 @@ public static Config getConfig() {
public static TaskRegistry getScheduler() {
return loop;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@

public abstract class AbstractOffsetArgument extends AbstractSimpleValueArgument<Date> {

private final boolean reverse;

public AbstractOffsetArgument() {
this(false);
}

public AbstractOffsetArgument(boolean reverse) {
this.reverse = reverse;
}

@Override
protected Date parse(@NotNull String argument) throws ArgumentException {
Calendar calendar = Calendar.getInstance();
Expand All @@ -23,8 +33,17 @@ protected Date parse(@NotNull String argument) throws ArgumentException {

if (!Character.isDigit(currentChar)) {
if (durationMap.containsKey(currentChar)) {
calendar.add(durationMap.get(currentChar), Integer.parseInt(argument.substring(offset, i)));
modified = true;
try {
int key = Integer.parseInt(argument.substring(offset, i));
if (reverse) {
key *= -1;
}

calendar.add(durationMap.get(currentChar), key);
modified = true;
} catch (NumberFormatException e) {
throw new MalformedArgumentException("Malformed duration! Provided token " + currentChar + " had an invalid duration.");
}
} else {
List<String> units = new ArrayList<>();
for (Character character : durationMap.keySet()) {
Expand All @@ -38,7 +57,12 @@ protected Date parse(@NotNull String argument) throws ArgumentException {
}

if (!modified) {
throw new MalformedArgumentException("Malformed duration!");
List<String> units = new ArrayList<>();
for (Character character : durationMap.keySet()) {
units.add(character.toString());
}

throw new MalformedArgumentException("Malformed duration!" + " \nPossible time units are: " + String.join(",", units));
}

return calendar.getTime();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@

public class DateOffsetArgument extends AbstractOffsetArgument {

public DateOffsetArgument() {
super();
}

@Override
protected Duration[] getDurations() {
return new Duration[]{
new Duration(Calendar.DAY_OF_MONTH, 'd'),
new Duration(Calendar.DAY_OF_WEEK, 'w'),
new Duration(Calendar.MONTH, 'm'),
new Duration(Calendar.YEAR, 'y'),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@

public class TimeOffsetArgument extends AbstractOffsetArgument {

public TimeOffsetArgument() {
super();
}

public TimeOffsetArgument(boolean reverse) {
super(reverse);
}

@Override
protected Duration[] getDurations() {
return new Duration[]{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package com.diamondfire.helpbot.bot.command.impl.other;

import com.diamondfire.helpbot.bot.command.argument.ArgumentSet;
import com.diamondfire.helpbot.bot.command.help.CommandCategory;
import com.diamondfire.helpbot.bot.command.help.HelpContext;
import com.diamondfire.helpbot.bot.command.impl.Command;
import com.diamondfire.helpbot.bot.command.permissions.Permission;
import com.diamondfire.helpbot.bot.command.reply.PresetBuilder;
import com.diamondfire.helpbot.bot.command.reply.feature.informative.InformativeReply;
import com.diamondfire.helpbot.bot.command.reply.feature.informative.InformativeReplyType;
import com.diamondfire.helpbot.bot.events.CommandEvent;
import com.diamondfire.helpbot.util.nbs.*;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.TextChannel;

import java.awt.*;
import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;

import java.util.Scanner;
import java.util.concurrent.CompletableFuture;

public class NbsCommand extends Command {

@Override
public String getName() {
return "nbs";
}

@Override
public HelpContext getHelpContext() {
return new HelpContext()
.description("Generates a Codeutils song function.")
.category(CommandCategory.OTHER);
}

@Override
protected ArgumentSet compileArguments() { return new ArgumentSet(); }

@Override
public Permission getPermission() { return Permission.USER; }

@Override
public void run(CommandEvent event) {
TextChannel channel = event.getChannel();
PresetBuilder preset = new PresetBuilder();
preset.withPreset(new InformativeReply(InformativeReplyType.ERROR,"You need to attach an nbs file!"));
if(event.getMessage().getAttachments().isEmpty()) {
event.reply(preset);
return;
}
Message.Attachment attachment = event.getMessage().getAttachments().get(0);
if(!attachment.getFileExtension().equals("nbs")) {
event.reply(preset);
return;
}
EmbedBuilder errorEmbed = new EmbedBuilder()
.setColor(Color.RED)
.setTitle("An error occurred!");
try {
File file = new File("input.nbs");
System.out.println(event.getMessage().getAttachments().get(0).getFileExtension());
CompletableFuture.runAsync(() -> {
CompletableFuture<File> task = attachment.downloadToFile(file);
while(!task.isDone()) { }
try {
String code = new NBSToTemplate(NBSDecoder.parse(file)).convert();
byte[] b64 = CompressionUtil.toBase64(CompressionUtil.toGZIP(code.getBytes(StandardCharsets.UTF_8)));
String jsonInputString = String.format("{\"template\": \"%s\",\"temp\": true}",new String(b64));
JsonObject json = new Gson().fromJson(generateLink(jsonInputString),JsonObject.class);

EmbedBuilder embed = new EmbedBuilder()
.setColor(new Color(70,199,82))
.setTitle("Function Generated!")
.setThumbnail("https://static.wikia.nocookie.net/minecraft/images/9/9b/Note_Block.png/revision/latest?cb=20190921170620")
.addField("Link: __Expires in 2 mins__","[Function Link](https://derpystuff.gitlab.io/code/l?link=" + json.get("link").getAsString() + ")",false)
.addField("Info:","Click the link shown above and click the button in the bottom left corner to copy the give command for the template. You will need [this function](https://derpystuff.gitlab.io/code/l?link=7cf5d91c35bbde31c28567d8d8945c40) to play songs.",false);

channel.sendMessage(embed.build()).queue();
} catch(OutdatedNBSException e) {
e.printStackTrace();
channel.sendMessage(errorEmbed.build()).queue();
} catch(IOException e) {
e.printStackTrace();
channel.sendMessage(errorEmbed.build()).queue();
}
});
} catch(Exception e) {
e.printStackTrace();
channel.sendMessage(errorEmbed.build()).queue();
}

}
private static String generateLink(String templateData) throws IOException {
URL url = new URL("https://twv.vercel.app/v2/create");
URLConnection con = url.openConnection();
HttpURLConnection http = (HttpURLConnection) con;
http.setRequestMethod("POST");
http.setDoOutput(true);
byte[] out = templateData.getBytes(StandardCharsets.UTF_8);
int length = out.length;

http.setFixedLengthStreamingMode(length);
http.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
http.setRequestProperty("Accept","application/json; charset=UTF-8");
http.connect();
try(OutputStream os = http.getOutputStream()) {
os.write(out);
}
Scanner s = new Scanner(http.getInputStream()).useDelimiter("\\A");
String result = s.hasNext() ? s.next() : "";
s.close();
http.disconnect();

return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.diamondfire.helpbot.bot.command.impl.other;

import com.diamondfire.helpbot.bot.command.argument.ArgumentSet;
import com.diamondfire.helpbot.bot.command.argument.impl.parsing.types.MessageArgument;
import com.diamondfire.helpbot.bot.command.help.*;
import com.diamondfire.helpbot.bot.command.impl.Command;
import com.diamondfire.helpbot.bot.command.permissions.Permission;
import com.diamondfire.helpbot.bot.command.reply.PresetBuilder;
import com.diamondfire.helpbot.bot.command.reply.feature.informative.*;
import com.diamondfire.helpbot.bot.events.CommandEvent;
import com.diamondfire.helpbot.util.Util;
import net.dv8tion.jda.api.EmbedBuilder;
Expand All @@ -14,49 +17,58 @@
import static com.diamondfire.helpbot.bot.HelpBotInstance.getConfig;

public class PollCommand extends Command {


@Override
public String getName() {
return "poll";
}

@Override
public HelpContext getHelpContext() {
return new HelpContext()
.description("Creates a poll.")
.category(CommandCategory.OTHER);
.category(CommandCategory.OTHER).
addArgument(new HelpContextArgument()
.name("text|input"));
}

@Override
public ArgumentSet compileArguments() {
return new ArgumentSet();
return new ArgumentSet().
addArgument("input",
new MessageArgument());
}

@Override
public Permission getPermission() {
return Permission.DEVELOPER;
}

@Override
public void run(CommandEvent event) {

String input = event.getArgument("input");
//sends arguments on a ?poll command
if (event.getMessage().getContentRaw().equals(getConfig().getPrefix() + "poll")) {

EmbedBuilder error = new EmbedBuilder();
error.setTitle("Poll Command");
error.setDescription("The arguments for the poll command are:\n" + getConfig().getPrefix() + "poll [Question]\\|[Option 1]\\|[Option 2]\\|");
error.setColor(Color.RED);

event.getChannel().sendMessage(error.build()).queue();

if (input.isEmpty()) {
PresetBuilder preset = new PresetBuilder();
preset.withPreset(new InformativeReply(InformativeReplyType.ERROR, "Poll Command", "The arguments for the poll command are:\n" + getConfig().getPrefix() + "poll [Question]\\|[Option 1]\\|[Option 2]\\|"));
event.reply(preset);
return;
}

ArrayList<String> pollOptions = new ArrayList<>();
pollOptions.addAll(Arrays.asList(event.getMessage().getContentRaw().split("\\|"))); //poll options + poll question (contains ?poll)
String pollQuestion = String.valueOf(pollOptions.get(0).subSequence(6,pollOptions.get(0).length())); //poll question (does not contain ?poll)
pollOptions.addAll(Arrays.asList(input.split("\\|"))); //poll options + poll question

//checks if there are no arguments
if (pollOptions.isEmpty()) {
PresetBuilder preset = new PresetBuilder();
preset.withPreset(new InformativeReply(InformativeReplyType.ERROR, "The arguments are incorrect. Correct arguments are:\\n" + getConfig().getPrefix() + "poll [Question]\\\\|[Option 1]\\\\|[Option 2]\\\\|"));
event.reply(preset);
return;
}


String pollQuestion = pollOptions.get(0); //poll question
pollOptions.remove(0); //remove the poll question so only the options remain

ArrayList<String> fullPollOptions = new ArrayList<>();
Expand All @@ -81,39 +93,24 @@ public void run(CommandEvent event) {

i += 2;
}

EmbedBuilder builder = new EmbedBuilder();
builder.setTitle(pollQuestion); //sets title to poll question
builder.setDescription(String.join(" ", fullPollOptions)); //adds poll options
builder.setColor(new Color(33, 40, 97));

int len = pollOptions.toArray().length; //get amount of poll options

//checks if there are no arguments
if (len == 0) {

EmbedBuilder error = new EmbedBuilder();
error.setTitle("Error!");
error.setDescription("The arguments are incorrect. Correct arguments are:\n" + getConfig().getPrefix() + "poll [Question]\\|[Option 1]\\|[Option 2]\\|");
error.setColor(Color.RED);

event.getChannel().sendMessage(error.build()).queue();

return;
}

event.getChannel().sendMessage(builder.build()).queue((message) -> { //send embed message

//add reactions
Deque<String> nums = Util.getUnicodeNumbers();
for (String option : pollOptions) {
message.addReaction(nums.pop()).queue();
}

});

//delete original message
event.getMessage().delete().queue();

}
}
Loading