Search in sources :

Example 1 with RateLimiter

use of net.kodehawa.mantarobot.commands.currency.RateLimiter 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();
        }
    });
}
Also used : SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) Color(java.awt.Color) MantaroInfo(net.kodehawa.mantarobot.MantaroInfo) CommandProcessor(net.kodehawa.mantarobot.core.CommandProcessor) java.util(java.util) CommandStatsManager(net.kodehawa.mantarobot.commands.info.CommandStatsManager) Utils(net.kodehawa.mantarobot.utils.Utils) Module(net.kodehawa.mantarobot.modules.Module) MantaroShard(net.kodehawa.mantarobot.MantaroShard) StatsHelper.calculateDouble(net.kodehawa.mantarobot.commands.info.StatsHelper.calculateDouble) MantaroBot(net.kodehawa.mantarobot.MantaroBot) MessageBuilder(net.dv8tion.jda.core.MessageBuilder) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) Cursor(com.rethinkdb.net.Cursor) DBGuild(net.kodehawa.mantarobot.data.entities.DBGuild) Duration(java.time.Duration) JDA(net.dv8tion.jda.core.JDA) CommandRegistry(net.kodehawa.mantarobot.modules.CommandRegistry) ManagementFactory(java.lang.management.ManagementFactory) Command(net.kodehawa.mantarobot.modules.Command) TextChannelGround(net.kodehawa.mantarobot.commands.currency.TextChannelGround) CommandListener(net.kodehawa.mantarobot.core.listeners.command.CommandListener) GuildStatsManager(net.kodehawa.mantarobot.commands.info.GuildStatsManager) PostLoadEvent(net.kodehawa.mantarobot.modules.events.PostLoadEvent) JDAInfo(net.dv8tion.jda.core.JDAInfo) net.dv8tion.jda.core.entities(net.dv8tion.jda.core.entities) RateLimiter(net.kodehawa.mantarobot.commands.currency.RateLimiter) MantaroListener(net.kodehawa.mantarobot.core.listeners.MantaroListener) Category(net.kodehawa.mantarobot.modules.commands.base.Category) Instant(java.time.Instant) AsyncInfoMonitor(net.kodehawa.mantarobot.commands.info.AsyncInfoMonitor) Collectors(java.util.stream.Collectors) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) OnlineStatus(net.dv8tion.jda.core.OnlineStatus) TimeUnit(java.util.concurrent.TimeUnit) PlayerLibrary(com.sedmelluq.discord.lavaplayer.tools.PlayerLibrary) DurationFormatUtils(org.apache.commons.lang3.time.DurationFormatUtils) GuildData(net.kodehawa.mantarobot.data.entities.helpers.GuildData) EmoteReference(net.kodehawa.mantarobot.utils.commands.EmoteReference) DateTimeFormatter(java.time.format.DateTimeFormatter) CommandPermission(net.kodehawa.mantarobot.modules.commands.CommandPermission) MantaroData(net.kodehawa.mantarobot.data.MantaroData) HelpUtils.forType(net.kodehawa.mantarobot.commands.info.HelpUtils.forType) RethinkDB(com.rethinkdb.RethinkDB) StatsHelper.calculateInt(net.kodehawa.mantarobot.commands.info.StatsHelper.calculateInt) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) RateLimiter(net.kodehawa.mantarobot.commands.currency.RateLimiter) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) Command(net.kodehawa.mantarobot.modules.Command)

Example 2 with RateLimiter

use of net.kodehawa.mantarobot.commands.currency.RateLimiter in project MantaroBot by Mantaro.

the class CurrencyCmds method rep.

@Command
public static void rep(CommandRegistry cr) {
    cr.register("rep", new SimpleCommand(Category.CURRENCY) {

        RateLimiter rateLimiter = new RateLimiter(TimeUnit.HOURS, 12);

        @Override
        public void call(GuildMessageReceivedEvent event, String content, String[] args) {
            if (event.getMessage().getMentionedUsers().isEmpty()) {
                event.getChannel().sendMessage(EmoteReference.ERROR + "You need to mention at least one user.").queue();
                return;
            }
            if (event.getMessage().getMentionedUsers().get(0).isBot()) {
                event.getChannel().sendMessage(EmoteReference.THINKING + "You cannot rep a bot.").queue();
                return;
            }
            if (event.getMessage().getMentionedUsers().get(0).equals(event.getAuthor())) {
                event.getChannel().sendMessage(EmoteReference.THINKING + "You cannot rep yourself.").queue();
                return;
            }
            if (event.getMessage().getMentionedUsers().isEmpty()) {
                event.getChannel().sendMessage(EmoteReference.THINKING + "You need to mention one user.").queue();
                return;
            }
            if (!rateLimiter.process(event.getMember())) {
                event.getChannel().sendMessage(EmoteReference.ERROR + "You can only rep once every 12 hours.\n**You'll be able to use this command again in " + Utils.getVerboseTime(rateLimiter.tryAgainIn(event.getMember())) + ".**").queue();
                return;
            }
            User mentioned = event.getMessage().getMentionedUsers().get(0);
            Player player = MantaroData.db().getPlayer(event.getGuild().getMember(mentioned));
            player.addReputation(1L);
            player.saveAsync();
            event.getChannel().sendMessage(EmoteReference.CORRECT + "Added reputation to **" + mentioned.getName() + "**").queue();
        }

        @Override
        public MessageEmbed help(GuildMessageReceivedEvent event) {
            return helpEmbed(event, "Reputation command").setDescription("**Reps an user**").addField("Usage", "`~>rep <@user>` - **Gives reputation to x user**", false).addField("Parameters", "`@user` - user to mention", false).addField("Important", "Only usable every 24 hours.", false).build();
        }
    });
    cr.registerAlias("rep", "reputation");
}
Also used : Player(net.kodehawa.mantarobot.data.entities.Player) DBUser(net.kodehawa.mantarobot.data.entities.DBUser) User(net.dv8tion.jda.core.entities.User) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) RateLimiter(net.kodehawa.mantarobot.commands.currency.RateLimiter) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) Command(net.kodehawa.mantarobot.modules.Command)

Aggregations

GuildMessageReceivedEvent (net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent)2 RateLimiter (net.kodehawa.mantarobot.commands.currency.RateLimiter)2 Command (net.kodehawa.mantarobot.modules.Command)2 SimpleCommand (net.kodehawa.mantarobot.modules.commands.SimpleCommand)2 RethinkDB (com.rethinkdb.RethinkDB)1 Cursor (com.rethinkdb.net.Cursor)1 PlayerLibrary (com.sedmelluq.discord.lavaplayer.tools.PlayerLibrary)1 Color (java.awt.Color)1 ManagementFactory (java.lang.management.ManagementFactory)1 Duration (java.time.Duration)1 Instant (java.time.Instant)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 java.util (java.util)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)1 JDA (net.dv8tion.jda.core.JDA)1 JDAInfo (net.dv8tion.jda.core.JDAInfo)1 MessageBuilder (net.dv8tion.jda.core.MessageBuilder)1 OnlineStatus (net.dv8tion.jda.core.OnlineStatus)1