use of net.kodehawa.mantarobot.modules.CommandRegistry in project MantaroBot by Mantaro.
the class QuoteCmd method quote.
@Command
public static void quote(CommandRegistry cr) {
cr.register("quote", new SimpleCommand(Category.MISC) {
@Override
protected void call(GuildMessageReceivedEvent event, String content, String[] args) {
if (content.isEmpty()) {
onHelp(event);
return;
}
String action = args[0];
String phrase = content.replace(action + " ", "");
Guild guild = event.getGuild();
ManagedDatabase db = MantaroData.db();
EmbedBuilder builder = new EmbedBuilder();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
List<Message> messageHistory;
try {
messageHistory = event.getChannel().getHistory().retrievePast(100).complete();
} catch (Exception e) {
if (e instanceof PermissionException) {
event.getChannel().sendMessage(EmoteReference.CRYING + "I don't have permission to do this :<").queue();
return;
}
event.getChannel().sendMessage(EmoteReference.ERROR + "It seems like discord is on fire, as my" + " " + "request to retrieve message history was denied" + "with the error `" + e.getClass().getSimpleName() + "`").queue();
log.warn("Shit exploded on Discord's backend. <@155867458203287552>", e);
return;
}
if (action.equals("addfrom")) {
Message message = messageHistory.stream().filter(msg -> msg.getContent().toLowerCase().contains(phrase.toLowerCase()) && !msg.getContent().startsWith(db.getGuild(guild).getData().getGuildCustomPrefix() == null ? MantaroData.config().get().getPrefix() : db.getGuild(guild).getData().getGuildCustomPrefix()) && !msg.getContent().startsWith(MantaroData.config().get().getPrefix())).findFirst().orElse(null);
if (message == null) {
event.getChannel().sendMessage(EmoteReference.ERROR + "I couldn't find a message matching the specified search" + " criteria. Please try again with a more specific query.").queue();
return;
}
TextChannel channel = guild.getTextChannelById(message.getChannel().getId());
Quote quote = Quote.of(guild.getMember(message.getAuthor()), channel, message);
db.getQuotes(guild).add(quote);
event.getChannel().sendMessage(buildQuoteEmbed(dateFormat, builder, quote)).queue();
quote.saveAsync();
return;
}
if (action.equals("random")) {
try {
Quote quote = CollectionUtils.random(db.getQuotes(event.getGuild()));
event.getChannel().sendMessage(buildQuoteEmbed(dateFormat, builder, quote)).queue();
} catch (Exception e) {
event.getChannel().sendMessage(EmoteReference.ERROR + "This server has no set quotes!").queue();
}
return;
}
if (action.equals("readfrom")) {
try {
List<Quote> quotes = db.getQuotes(guild);
for (int i2 = 0; i2 < quotes.size(); i2++) {
if (quotes.get(i2).getContent().contains(phrase)) {
Quote quote = quotes.get(i2);
event.getChannel().sendMessage(buildQuoteEmbed(dateFormat, builder, quote)).queue();
break;
}
}
} catch (Exception e) {
event.getChannel().sendMessage(EmoteReference.ERROR + "I didn't find any quotes! (no quotes match the criteria).").queue();
}
return;
}
if (action.equals("removefrom")) {
try {
List<Quote> quotes = db.getQuotes(guild);
for (int i2 = 0; i2 < quotes.size(); i2++) {
if (quotes.get(i2).getContent().contains(phrase)) {
Quote quote = quotes.get(i2);
db.getQuotes(guild).remove(i2);
quote.saveAsync();
event.getChannel().sendMessage(EmoteReference.CORRECT + "Removed quote with content: " + quote.getContent()).queue();
break;
}
}
} catch (Exception e) {
event.getChannel().sendMessage(EmoteReference.ERROR + "No quotes match the criteria.").queue();
}
}
}
@Override
public MessageEmbed help(GuildMessageReceivedEvent event) {
return helpEmbed(event, "Quote command").setDescription("**Quotes a message by search term.**").addField("Usage", "`~>quote addfrom <phrase>`- **Add a quote with the content defined by the specified number. For example, providing 1 will quote " + "the last message.**\n" + "`~>quote removefrom <phrase>` - **Remove a quote based on your text query.**\n" + "`~>quote readfrom <phrase>` - **Search for the first quote which matches your search criteria and prints " + "it.**\n" + "`~>quote random` - **Get a random quote.** \n", false).addField("Parameters", "`phrase` - A part of the quote phrase.", false).setColor(Color.DARK_GRAY).build();
}
});
}
use of net.kodehawa.mantarobot.modules.CommandRegistry in project MantaroBot by Mantaro.
the class ImageCmds method rule34.
@Command
public static void rule34(CommandRegistry cr) {
cr.register("rule34", new SimpleCommand(Category.IMAGE) {
@Override
protected void call(GuildMessageReceivedEvent event, String content, String[] args) {
if (!nsfwCheck(event, true, true))
return;
String noArgs = content.split(" ")[0];
TextChannelGround.of(event).dropItemWithChance(13, 3);
switch(noArgs) {
case "get":
try {
String whole1 = content.replace("get ", "");
String[] wholeBeheaded = whole1.split(" ");
Rule34.get(60, image -> {
try {
int number;
try {
number = Integer.parseInt(wholeBeheaded[0]);
} catch (Exception e) {
number = r.nextInt(image.size());
}
String TAGS = image.get(number).getTags().replace(" ", " ,");
EmbedBuilder builder = new EmbedBuilder();
builder.setAuthor("Found image", null, "http:" + image.get(number - 1).getFile_url()).setImage("http:" + image.get(number - 1).getFile_url()).addField("Width", String.valueOf(image.get(number - 1).getWidth()), true).addField("Height", String.valueOf(image.get(number - 1).getHeight()), true).addField("Tags", "``" + (TAGS == null ? "None" : TAGS) + "``", false).setFooter("If the image doesn't load, click the title.", null);
event.getChannel().sendMessage(builder.build()).queue();
} catch (ArrayIndexOutOfBoundsException e) {
event.getChannel().sendMessage(EmoteReference.ERROR + "**There aren't more images or no results found**! Try with a lower number.").queue();
}
});
} catch (Exception exception) {
if (exception instanceof NumberFormatException)
event.getChannel().sendMessage(EmoteReference.ERROR + "Wrong argument type. Check ~>help rule34").queue(message -> message.delete().queueAfter(10, TimeUnit.SECONDS));
}
break;
case "tags":
try {
try {
boolean isOldFormat = args[1].matches("^[0-9]*$");
if (isOldFormat) {
event.getChannel().sendMessage(EmoteReference.WARNING + "Now you don't need to specify the page number. Please use ~>rule34 tags <tag>").queue();
return;
}
String sNoArgs = content.replace("tags ", "");
String[] expectedNumber = sNoArgs.split(" ");
String tags = expectedNumber[0];
Rule34.onSearch(60, tags, images -> {
try {
try {
number1 = Integer.parseInt(expectedNumber[2]);
} catch (Exception e) {
number1 = r.nextInt(images.size() > 0 ? images.size() - 1 : images.size());
}
String TAGS = images.get(number).getTags() == null ? tags : images.get(number).getTags().replace(" ", " ,");
EmbedBuilder builder = new EmbedBuilder();
builder.setAuthor("Found image", null, "http:" + images.get(number1 - 1).getFile_url()).setImage("http:" + images.get(number1 - 1).getFile_url()).addField("Width", String.valueOf(images.get(number1 - 1).getWidth()), true).addField("Height", String.valueOf(images.get(number1 - 1).getHeight()), true).addField("Tags", "``" + (TAGS == null ? "None" : TAGS) + "``", false).setFooter("If the image doesn't load, click the title.", null);
event.getChannel().sendMessage(builder.build()).queue();
} catch (Exception e) {
e.printStackTrace();
event.getChannel().sendMessage(EmoteReference.ERROR + "**There aren't more images or no results found**! Try with a lower number.").queue();
}
});
} catch (Exception exception) {
if (exception instanceof NumberFormatException)
event.getChannel().sendMessage(EmoteReference.ERROR + "Wrong argument type. Check ~>help rule34").queue(message -> message.delete().queueAfter(10, TimeUnit.SECONDS));
}
} catch (NullPointerException e) {
event.getChannel().sendMessage(EmoteReference.ERROR + "Rule34 decided to not fetch the image. Well, you can try with another number or tag.").queue();
}
break;
default:
onHelp(event);
break;
}
}
@Override
public MessageEmbed help(GuildMessageReceivedEvent event) {
return helpEmbed(event, "rule34.xxx commmand").setColor(Color.PINK).setDescription("**Retrieves images from the rule34 (hentai) image board.**").addField("Usage", "`~>rule34 get <imagenumber>` - **Gets an image based in parameters.**\n" + "`~>rule34 tags <tag> <imagenumber>` - **Gets an image based in the specified tag and parameters.**\n", false).addField("Parameters", "`page` - **Can be any value from 1 to the rule34 maximum page. Probably around 4000.**\n" + "`imagenumber` - **(OPTIONAL) Any number from 1 to the maximum possible images to get, specified by the first instance of the command.**\n" + "`tag` - **Any valid image tag. For example animal_ears or original.**", false).build();
}
});
}
use of net.kodehawa.mantarobot.modules.CommandRegistry in project MantaroBot by Mantaro.
the class InfoCmds method ping.
@Command
public static void ping(CommandRegistry cr) {
RateLimiter rateLimiter = new RateLimiter(TimeUnit.SECONDS, 5);
cr.register("ping", new SimpleCommand(Category.INFO) {
@Override
protected void call(GuildMessageReceivedEvent event, String content, String[] args) {
if (!rateLimiter.process(event.getMember())) {
event.getChannel().sendMessage(EmoteReference.ERROR + "Yikes! Seems like you're going too fast.").queue();
return;
}
long start = System.currentTimeMillis();
event.getChannel().sendTyping().queue(v -> {
long ping = System.currentTimeMillis() - start;
event.getChannel().sendMessage(EmoteReference.MEGA + "My ping: " + ping + " ms - " + ratePing(ping) + " `Websocket:" + event.getJDA().getPing() + "ms`").queue();
TextChannelGround.of(event).dropItemWithChance(5, 5);
});
}
@Override
public MessageEmbed help(GuildMessageReceivedEvent event) {
return helpEmbed(event, "Ping Command").setDescription("**Plays Ping-Pong with Discord and prints out the result.**").build();
}
});
}
Aggregations