use of net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent in project MantaroBot by Mantaro.
the class OwnerCmd method owner.
@Subscribe
public void owner(CommandRegistry cr) {
cr.register("owner", new SimpleCommand(Category.OWNER) {
@Override
public CommandPermission permission() {
return CommandPermission.OWNER;
}
@Override
public MessageEmbed help(GuildMessageReceivedEvent event) {
return helpEmbed(event, "Owner command").setDescription("`~>owner premium add <id> <days>` - Adds premium to the specified user for x days.").build();
}
@Override
public void call(GuildMessageReceivedEvent event, String content, String[] args) {
if (args.length < 1) {
onHelp(event);
return;
}
String option = args[0];
if (option.equals("premium")) {
String sub = args[1].substring(0, args[1].indexOf(' '));
if (sub.equals("add")) {
try {
String userId;
String[] values = SPLIT_PATTERN.split(args[1], 3);
try {
Long.parseLong(values[1]);
userId = values[1];
} catch (Exception e) {
if (!event.getMessage().getMentionedUsers().isEmpty()) {
userId = event.getMessage().getMentionedUsers().get(0).getId();
return;
} else {
event.getChannel().sendMessage(EmoteReference.ERROR + "Not a valid user id").queue();
return;
}
}
DBUser db = MantaroData.db().getUser(userId);
db.incrementPremium(TimeUnit.DAYS.toMillis(Long.parseLong(values[2])));
db.saveAsync();
event.getChannel().sendMessage(EmoteReference.CORRECT + "The premium feature for user " + db.getId() + " now is until " + new Date(db.getPremiumUntil())).queue();
return;
} catch (IndexOutOfBoundsException e) {
event.getChannel().sendMessage(EmoteReference.ERROR + "You need to specify id and number of days").queue();
e.printStackTrace();
return;
}
}
if (sub.equals("guild")) {
try {
String[] values = SPLIT_PATTERN.split(args[1], 3);
DBGuild db = MantaroData.db().getGuild(values[1]);
db.incrementPremium(TimeUnit.DAYS.toMillis(Long.parseLong(values[2])));
db.saveAsync();
event.getChannel().sendMessage(EmoteReference.CORRECT + "The premium feature for guild " + db.getId() + " now is until " + new Date(db.getPremiumUntil())).queue();
return;
} catch (IndexOutOfBoundsException e) {
event.getChannel().sendMessage(EmoteReference.ERROR + "You need to specify id and number of days").queue();
e.printStackTrace();
return;
}
}
}
onHelp(event);
}
@Override
public String[] splitArgs(String content) {
return SPLIT_PATTERN.split(content, 2);
}
});
}
use of net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent in project MantaroBot by Mantaro.
the class Utils method wget.
/**
* Fetches an Object from any given URL. Uses vanilla Java methods.
* Can retrieve text, JSON Objects, XML and probably more.
*
* @param url The URL to get the object from.
* @param event guild event
* @return The object as a parsed UTF-8 string.
*/
public static String wget(String url, GuildMessageReceivedEvent event) {
String webObject = null;
try {
URL ur1 = new URL(url);
HttpURLConnection conn = (HttpURLConnection) ur1.openConnection();
conn.setRequestProperty("User-Agent", MantaroInfo.USER_AGENT);
InputStream ism = conn.getInputStream();
webObject = CharStreams.toString(new InputStreamReader(ism, StandardCharsets.UTF_8));
} catch (Exception e) {
if (e instanceof java.io.FileNotFoundException)
return null;
log.warn(getFetchDataFailureResponse(url, null), e);
Optional.ofNullable(event).ifPresent((w) -> w.getChannel().sendMessage("\u274C I got an error while retrieving data from " + url).queue());
}
return webObject;
}
use of net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent in project MantaroBot by Mantaro.
the class Utils method wgetResty.
/**
* Same than above, but using resty. Way easier tbh.
*
* @param url The URL to get the object from.
* @param event JDA message event.
* @return The object as a parsed string.
*/
public static String wgetResty(String url, GuildMessageReceivedEvent event) {
String url2 = null;
Resty resty = new Resty();
try {
resty.withHeader("User-Agent", MantaroInfo.USER_AGENT);
InputStream is = resty.text(url).stream();
url2 = CharStreams.toString(new InputStreamReader(is, StandardCharsets.UTF_8));
} catch (IOException e) {
log.warn(getFetchDataFailureResponse(url, "Resty"), e);
Optional.ofNullable(event).ifPresent((evt) -> evt.getChannel().sendMessage("\u274C Error retrieving data from URL [Resty]").queue());
}
return url2;
}
Aggregations