Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add modname command #47

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "src/main/resources/mcdrama"]
path = src/main/resources/mcdrama
url = https://github.com/Gaelan/MCDrama.git
[submodule "src/main/resources/modname"]
path = src/main/resources/modname
url = https://github.com/4hrue2kd83f/MCDrama.git
92 changes: 92 additions & 0 deletions src/main/java/com/tterrag/k9/commands/CommandModname.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.tterrag.k9.commands;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;

import org.apache.commons.io.IOUtils;
import org.jruby.RubyIO;
import org.jruby.embed.ScriptingContainer;

import com.google.common.base.Charsets;
import com.tterrag.k9.K9;
import com.tterrag.k9.commands.api.Command;
import com.tterrag.k9.commands.api.CommandBase;
import com.tterrag.k9.commands.api.CommandContext;
import com.tterrag.k9.util.EmbedCreator;
import com.tterrag.k9.util.annotation.Nullable;

import reactor.core.publisher.Mono;

@Command
public class CommandModname extends CommandBase {

private final ScriptingContainer sc = new ScriptingContainer();
private final @Nullable Object modname;

public CommandModname() {
super("modname", false);
InputStream script = K9.class.getResourceAsStream("/modname/modname.rb");
if (script != null) {
modname = sc.runScriptlet(new InputStreamReader(script), "modname.rb");
} else {
modname = null;
}
InputStream dramaversion = K9.class.getResourceAsStream("/modname/.dramaversion");
String version = null;
if (dramaversion != null) {
try {
version = IOUtils.readLines(dramaversion, Charsets.UTF_8).get(0);
} catch (IOException e) {
e.printStackTrace();
}
}
if (version == null) {
Process proc;
try {
proc = Runtime.getRuntime().exec("git submodule --quiet foreach git rev-parse --short HEAD");
proc.waitFor();
version = IOUtils.readLines(proc.getInputStream(), Charsets.UTF_8).get(0);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
if (version != null) {
sc.callMethod(modname, "set_current_version", version);
}
}

@Override
public Mono<?> process(CommandContext ctx) {
if (modname != null) {
sc.callMethod(modname, "set_file_fetcher", new Object() {
@SuppressWarnings("unused")
public RubyIO open(String path) {
return new RubyIO(sc.getProvider().getRuntime(), K9.class.getResourceAsStream("/modname/" + path));
}
});
BigInteger seed = (BigInteger) sc.callMethod(sc.get("Random"), "new_seed");
sc.callMethod(sc.get("Random"), "srand", seed);
String drama = ((String) sc.callMethod(modname, "draminate")).replaceAll("(\\r\\n|\\r|\\n)", "");

return ctx.getMember()
.map(m -> m.getDisplayName())
.switchIfEmpty(Mono.justOrEmpty(ctx.getAuthor().map(a -> a.getUsername())))
.map(name ->
EmbedCreator.builder()
.title("Mod Name Generator")
.url("https://mod-name-generator.herokuapp.com/" + "d67ee8" + "/" + seed.toString(36))
.description(drama)
.build())
.flatMap(ctx::reply);
} else {
return ctx.error("Sorry, the modname command is not set up properly. Contact your bot admin!");
}
}

@Override
public String getDescription(CommandContext ctx) {
return "Generates a random mod name.";
}
}
1 change: 1 addition & 0 deletions src/main/resources/modname
Submodule modname added at fc89d0