Search in sources :

Example 66 with GuildMessageReceivedEvent

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);
        }
    });
}
Also used : MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) DBGuild(net.kodehawa.mantarobot.db.entities.DBGuild) SimpleCommand(net.kodehawa.mantarobot.core.modules.commands.SimpleCommand) DBUser(net.kodehawa.mantarobot.db.entities.DBUser) CommandPermission(net.kodehawa.mantarobot.core.modules.commands.base.CommandPermission) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) CompilationException(com.github.natanbc.javaeval.CompilationException) Subscribe(com.google.common.eventbus.Subscribe)

Example 67 with GuildMessageReceivedEvent

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;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) MantaroInfo(net.kodehawa.mantarobot.MantaroInfo) java.util(java.util) SneakyThrows(lombok.SneakyThrows) URL(java.net.URL) RateLimiter(net.kodehawa.mantarobot.utils.commands.RateLimiter) NewRateLimiter(net.kodehawa.mantarobot.utils.commands.NewRateLimiter) MantaroBot(net.kodehawa.mantarobot.MantaroBot) RethinkDB.r(com.rethinkdb.RethinkDB.r) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) JSONObject(org.json.JSONObject) Matcher(java.util.regex.Matcher) CharStreams(com.google.common.io.CharStreams) okhttp3(okhttp3) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) SimpleCommand(net.kodehawa.mantarobot.core.modules.commands.SimpleCommand) Config(net.kodehawa.mantarobot.data.Config) Connection(com.rethinkdb.net.Connection) Resty(us.monoid.web.Resty) net.dv8tion.jda.core.entities(net.dv8tion.jda.core.entities) IOException(java.io.IOException) Field(java.lang.reflect.Field) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Slf4j(lombok.extern.slf4j.Slf4j) URLEncoder(java.net.URLEncoder) EmoteReference(net.kodehawa.mantarobot.utils.commands.EmoteReference) MantaroData(net.kodehawa.mantarobot.data.MantaroData) FinderUtil(com.jagrosh.jdautilities.utils.FinderUtil) Pattern(java.util.regex.Pattern) InputStream(java.io.InputStream) OptsCmd.optsCmd(net.kodehawa.mantarobot.commands.OptsCmd.optsCmd) HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) URL(java.net.URL) IOException(java.io.IOException)

Example 68 with GuildMessageReceivedEvent

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;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) MantaroInfo(net.kodehawa.mantarobot.MantaroInfo) java.util(java.util) SneakyThrows(lombok.SneakyThrows) URL(java.net.URL) RateLimiter(net.kodehawa.mantarobot.utils.commands.RateLimiter) NewRateLimiter(net.kodehawa.mantarobot.utils.commands.NewRateLimiter) MantaroBot(net.kodehawa.mantarobot.MantaroBot) RethinkDB.r(com.rethinkdb.RethinkDB.r) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) JSONObject(org.json.JSONObject) Matcher(java.util.regex.Matcher) CharStreams(com.google.common.io.CharStreams) okhttp3(okhttp3) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) SimpleCommand(net.kodehawa.mantarobot.core.modules.commands.SimpleCommand) Config(net.kodehawa.mantarobot.data.Config) Connection(com.rethinkdb.net.Connection) Resty(us.monoid.web.Resty) net.dv8tion.jda.core.entities(net.dv8tion.jda.core.entities) IOException(java.io.IOException) Field(java.lang.reflect.Field) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Slf4j(lombok.extern.slf4j.Slf4j) URLEncoder(java.net.URLEncoder) EmoteReference(net.kodehawa.mantarobot.utils.commands.EmoteReference) MantaroData(net.kodehawa.mantarobot.data.MantaroData) FinderUtil(com.jagrosh.jdautilities.utils.FinderUtil) Pattern(java.util.regex.Pattern) InputStream(java.io.InputStream) OptsCmd.optsCmd(net.kodehawa.mantarobot.commands.OptsCmd.optsCmd) InputStreamReader(java.io.InputStreamReader) Resty(us.monoid.web.Resty) InputStream(java.io.InputStream) IOException(java.io.IOException)

Aggregations

GuildMessageReceivedEvent (net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent)68 Subscribe (com.google.common.eventbus.Subscribe)50 SimpleCommand (net.kodehawa.mantarobot.core.modules.commands.SimpleCommand)48 MessageEmbed (net.dv8tion.jda.core.entities.MessageEmbed)42 EmoteReference (net.kodehawa.mantarobot.utils.commands.EmoteReference)36 MantaroData (net.kodehawa.mantarobot.data.MantaroData)34 List (java.util.List)28 Category (net.kodehawa.mantarobot.core.modules.commands.base.Category)27 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)26 Utils (net.kodehawa.mantarobot.utils.Utils)26 TimeUnit (java.util.concurrent.TimeUnit)25 Collectors (java.util.stream.Collectors)25 CommandRegistry (net.kodehawa.mantarobot.core.CommandRegistry)25 Module (net.kodehawa.mantarobot.core.modules.Module)25 MantaroBot (net.kodehawa.mantarobot.MantaroBot)19 SubCommand (net.kodehawa.mantarobot.core.modules.commands.SubCommand)18 DBGuild (net.kodehawa.mantarobot.db.entities.DBGuild)18 RateLimiter (net.kodehawa.mantarobot.utils.commands.RateLimiter)18 Slf4j (lombok.extern.slf4j.Slf4j)17 Player (net.kodehawa.mantarobot.db.entities.Player)17