diff --git a/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java b/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java index f925688d..6a7e56ba 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java +++ b/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java @@ -66,6 +66,7 @@ public static void initialize() throws LoginException { new VerifyCommand(), new PollCommand(), new IdeaCommand(), + new ChannelMuteCommand(), // statsbot new StatsCommand(), new SupportBadCommand(), diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/argument/impl/types/LongArgument.java b/src/main/java/com/diamondfire/helpbot/bot/command/argument/impl/types/LongArgument.java new file mode 100644 index 00000000..dad4cc9c --- /dev/null +++ b/src/main/java/com/diamondfire/helpbot/bot/command/argument/impl/types/LongArgument.java @@ -0,0 +1,12 @@ +package com.diamondfire.helpbot.bot.command.argument.impl.types; + +import com.diamondfire.helpbot.bot.command.argument.impl.parsing.exceptions.ArgumentException; +import org.jetbrains.annotations.NotNull; + +public class LongArgument extends AbstractSimpleValueArgument{ + + @Override + protected Long parse(@NotNull String argument) throws ArgumentException { + return Long.parseLong(argument); + } +} diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/ChannelMuteCommand.java b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/ChannelMuteCommand.java new file mode 100644 index 00000000..b3866147 --- /dev/null +++ b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/ChannelMuteCommand.java @@ -0,0 +1,90 @@ +package com.diamondfire.helpbot.bot.command.impl.other; + +import com.diamondfire.helpbot.bot.HelpBotInstance; +import com.diamondfire.helpbot.bot.command.argument.ArgumentSet; +import com.diamondfire.helpbot.bot.command.argument.impl.parsing.types.SingleArgumentContainer; +import com.diamondfire.helpbot.bot.command.argument.impl.types.*; +import com.diamondfire.helpbot.bot.command.help.*; +import com.diamondfire.helpbot.bot.command.impl.Command; +import com.diamondfire.helpbot.bot.command.permissions.Permission; +import com.diamondfire.helpbot.bot.command.reply.PresetBuilder; +import com.diamondfire.helpbot.bot.command.reply.feature.informative.*; +import com.diamondfire.helpbot.bot.events.CommandEvent; +import com.diamondfire.helpbot.sys.tasks.impl.MuteExpireTask; +import com.diamondfire.helpbot.util.*; +import net.dv8tion.jda.api.entities.*; + +import java.time.*; +import java.util.Date; + +public class ChannelMuteCommand extends Command { + + @Override + public String getName() { + return "channelmute"; + } + + @Override + public HelpContext getHelpContext() { + return new HelpContext() + .description("Mutes a player from posing in a specific channel for one week (7 days)") + .category(CommandCategory.OTHER) + .addArgument(new HelpContextArgument().name("user")) + .addArgument(new HelpContextArgument().name("channel")) + .addArgument(new HelpContextArgument().name("duration")) + .addArgument(new HelpContextArgument().name("reason").optional()); + } + + @Override + protected ArgumentSet compileArguments() { + return new ArgumentSet() + .addArgument("user", new DiscordUserArgument()) + .addArgument("channel", new LongArgument()) + .addArgument("duration", new SingleArgumentContainer<>(new TimeOffsetArgument()).optional(null)) + .addArgument("reason", new StringArgument()); + } + + @Override + public Permission getPermission() { + return Permission.MODERATION; + } + + @Override + public void run(CommandEvent event) { + PresetBuilder builder = new PresetBuilder(); + long user = event.getArgument("user"); + long channel = event.getArgument("channel"); + Date duration = event.getArgument("duration"); + if (duration == null) { + duration = DateUtil.toDate(LocalDate.now().plusDays(7)); + } + String reason = event.getArgument("reason"); + if (reason.length() == 0) { + reason = "No reason given"; + } + long timeLeft = duration.toInstant().minusSeconds(Instant.now().getEpochSecond()).toEpochMilli(); + Date finalDuration = duration; + event.getGuild().retrieveMemberById(user).queue((msg) -> { + //TODO Owen DBQuery + + + builder.withPreset( + new InformativeReply(InformativeReplyType.SUCCESS, "Muted!", String.format("User will be muted for ``%s``.", FormatUtil.formatMilliTime(timeLeft))) + ); + Guild punishmentGuild = event.getGuild(); + TextChannel textChannel = punishmentGuild.getTextChannelById(channel); + punishmentGuild.retrieveMemberById(user).queue((member) -> { + textChannel.putPermissionOverride(member).deny(net.dv8tion.jda.api.Permission.MESSAGE_ADD_REACTION, net.dv8tion.jda.api.Permission.MESSAGE_WRITE).queue(); + }); + + HelpBotInstance.getScheduler().schedule(new MuteExpireTask(user, finalDuration, false)); + event.reply(builder); + }, (error) -> { + builder.withPreset( + new InformativeReply(InformativeReplyType.ERROR, "Discord user was not found!") + ); + event.reply(builder); + } + ); + } +}