From b360d842fb7b88bb3187838256f5cc331a0a56a3 Mon Sep 17 00:00:00 2001 From: Hammer86gn Date: Sun, 21 Feb 2021 21:10:37 -0500 Subject: [PATCH 1/3] Added Clips Channel Mute Command --- README.md | 1 + .../helpbot/bot/HelpBotInstance.java | 1 + .../command/impl/other/ClipMuteCommand.java | 91 +++++++++++++++++++ .../helpbot/bot/config/Config.java | 4 + 4 files changed, 97 insertions(+) create mode 100644 src/main/java/com/diamondfire/helpbot/bot/command/impl/other/ClipMuteCommand.java diff --git a/README.md b/README.md index 440a341c..2ff5d4b3 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ created, use the following format. "guild": long, "log_channel": long, "discussion_channel": long, + "clips_channel": long, "muted_role": long, "verified_role": long, diff --git a/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java b/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java index f925688d..408b445b 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 ClipMuteCommand(), // statsbot new StatsCommand(), new SupportBadCommand(), diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/ClipMuteCommand.java b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/ClipMuteCommand.java new file mode 100644 index 00000000..d170dfa4 --- /dev/null +++ b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/ClipMuteCommand.java @@ -0,0 +1,91 @@ +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.database.impl.DatabaseQuery; +import com.diamondfire.helpbot.sys.database.impl.queries.BasicQuery; +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 ClipMuteCommand extends Command { + + public static final long CLIP_CHANNEL_ID = HelpBotInstance.getConfig().getClipsChannel(); + + @Override + public String getName() { + return "clipmute"; + } + + @Override + public HelpContext getHelpContext() { + return new HelpContext() + .description("Mutes a player from posting in the clips channel for one week") + .category(CommandCategory.OTHER) + .addArgument(new HelpContextArgument().name("user")) + .addArgument(new HelpContextArgument().name("duration")); + + } + + @Override + protected ArgumentSet compileArguments() { + LocalDate nextWeek = LocalDate.now().plusDays(7); + + return new ArgumentSet() + .addArgument("user",new DiscordUserArgument()) + .addArgument("duration",new SingleArgumentContainer<>(new TimeOffsetArgument()).optional(DateUtil.toDate(nextWeek))); + } + + @Override + public Permission getPermission() { + return Permission.MODERATION; + } + + @Override + public void run(CommandEvent event) { + PresetBuilder builder = new PresetBuilder(); + long user = event.getArgument("user"); + Date duration = event.getArgument("duration"); + long timeLeft = duration.toInstant().minusSeconds(Instant.now().getEpochSecond()).toEpochMilli(); + + event.getGuild().retrieveMemberById(user).queue((msg) -> { + new DatabaseQuery() + .query(new BasicQuery("INSERT INTO owen.muted_members (member,muted_by,muted_at,muted_till,reason) VALUES (?,?,CURRENT_TIMESTAMP(),?,'Clips Mute')", (statement) -> { + statement.setLong(1, user); + statement.setLong(2, event.getAuthor().getIdLong()); + statement.setTimestamp(3, DateUtil.toTimeStamp(duration)); + + })) + .compile(); + + builder.withPreset( + new InformativeReply(InformativeReplyType.SUCCESS, "Muted!", String.format("Player will be muted for ``%s``.", FormatUtil.formatMilliTime(timeLeft))) + ); + Guild guild = event.getGuild(); + TextChannel channel = guild.getTextChannelById(CLIP_CHANNEL_ID); + guild.retrieveMemberById(user).queue((member) -> { + channel.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, duration, false)); + event.reply(builder); + },(error) -> { + builder.withPreset( + new InformativeReply(InformativeReplyType.ERROR, "Discord user was not found!") + ); + + event.reply(builder); + }); + } +} diff --git a/src/main/java/com/diamondfire/helpbot/bot/config/Config.java b/src/main/java/com/diamondfire/helpbot/bot/config/Config.java index 36106e8a..ec9a3e3c 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/config/Config.java +++ b/src/main/java/com/diamondfire/helpbot/bot/config/Config.java @@ -68,6 +68,10 @@ public long getDiscussionChannel() { return getPropertyLong("discussion_channel"); } + public long getClipsChannel() { + return getPropertyLong("clips_channel"); + } + public long getMutedRole() { return getPropertyLong("muted_role"); } From 5aac4fe12b8cd267e23cf28a12c85f065fa3df10 Mon Sep 17 00:00:00 2001 From: Hammer86gn Date: Sat, 27 Feb 2021 11:05:59 -0500 Subject: [PATCH 2/3] Added ChannelMuteCommand which replaced ClipMuteCommand --- README.md | 1 - .../helpbot/bot/HelpBotInstance.java | 2 +- .../impl/other/ChannelMuteCommand.java | 87 ++++++++++++++++++ .../command/impl/other/ClipMuteCommand.java | 91 ------------------- 4 files changed, 88 insertions(+), 93 deletions(-) create mode 100644 src/main/java/com/diamondfire/helpbot/bot/command/impl/other/ChannelMuteCommand.java delete mode 100644 src/main/java/com/diamondfire/helpbot/bot/command/impl/other/ClipMuteCommand.java diff --git a/README.md b/README.md index 2ff5d4b3..440a341c 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,6 @@ created, use the following format. "guild": long, "log_channel": long, "discussion_channel": long, - "clips_channel": long, "muted_role": long, "verified_role": long, diff --git a/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java b/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java index 408b445b..6a7e56ba 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java +++ b/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java @@ -66,7 +66,7 @@ public static void initialize() throws LoginException { new VerifyCommand(), new PollCommand(), new IdeaCommand(), - new ClipMuteCommand(), + new ChannelMuteCommand(), // statsbot new StatsCommand(), new SupportBadCommand(), 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..098a4fe1 --- /dev/null +++ b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/ChannelMuteCommand.java @@ -0,0 +1,87 @@ +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() { + LocalDate nextWeek = LocalDate.now().plusDays(7); + return new ArgumentSet() + .addArgument("user", new DiscordUserArgument()) + .addArgument("channel", new StringArgument()) + .addArgument("duration", new SingleArgumentContainer<>(new TimeOffsetArgument()).optional(DateUtil.toDate(nextWeek))) + .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 = Long.parseLong(event.getArgument("channel")); + Date duration = event.getArgument("duration"); + String reason = event.getArgument("reason"); + if (reason.length() == 0) { + reason = "No reason given"; + } + long timeLeft = duration.toInstant().minusSeconds(Instant.now().getEpochSecond()).toEpochMilli(); + 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, duration, false)); + event.reply(builder); + }, (error) -> { + builder.withPreset( + new InformativeReply(InformativeReplyType.ERROR, "Discord user was not found!") + ); + event.reply(builder); + } + ); + } +} diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/ClipMuteCommand.java b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/ClipMuteCommand.java deleted file mode 100644 index d170dfa4..00000000 --- a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/ClipMuteCommand.java +++ /dev/null @@ -1,91 +0,0 @@ -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.database.impl.DatabaseQuery; -import com.diamondfire.helpbot.sys.database.impl.queries.BasicQuery; -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 ClipMuteCommand extends Command { - - public static final long CLIP_CHANNEL_ID = HelpBotInstance.getConfig().getClipsChannel(); - - @Override - public String getName() { - return "clipmute"; - } - - @Override - public HelpContext getHelpContext() { - return new HelpContext() - .description("Mutes a player from posting in the clips channel for one week") - .category(CommandCategory.OTHER) - .addArgument(new HelpContextArgument().name("user")) - .addArgument(new HelpContextArgument().name("duration")); - - } - - @Override - protected ArgumentSet compileArguments() { - LocalDate nextWeek = LocalDate.now().plusDays(7); - - return new ArgumentSet() - .addArgument("user",new DiscordUserArgument()) - .addArgument("duration",new SingleArgumentContainer<>(new TimeOffsetArgument()).optional(DateUtil.toDate(nextWeek))); - } - - @Override - public Permission getPermission() { - return Permission.MODERATION; - } - - @Override - public void run(CommandEvent event) { - PresetBuilder builder = new PresetBuilder(); - long user = event.getArgument("user"); - Date duration = event.getArgument("duration"); - long timeLeft = duration.toInstant().minusSeconds(Instant.now().getEpochSecond()).toEpochMilli(); - - event.getGuild().retrieveMemberById(user).queue((msg) -> { - new DatabaseQuery() - .query(new BasicQuery("INSERT INTO owen.muted_members (member,muted_by,muted_at,muted_till,reason) VALUES (?,?,CURRENT_TIMESTAMP(),?,'Clips Mute')", (statement) -> { - statement.setLong(1, user); - statement.setLong(2, event.getAuthor().getIdLong()); - statement.setTimestamp(3, DateUtil.toTimeStamp(duration)); - - })) - .compile(); - - builder.withPreset( - new InformativeReply(InformativeReplyType.SUCCESS, "Muted!", String.format("Player will be muted for ``%s``.", FormatUtil.formatMilliTime(timeLeft))) - ); - Guild guild = event.getGuild(); - TextChannel channel = guild.getTextChannelById(CLIP_CHANNEL_ID); - guild.retrieveMemberById(user).queue((member) -> { - channel.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, duration, false)); - event.reply(builder); - },(error) -> { - builder.withPreset( - new InformativeReply(InformativeReplyType.ERROR, "Discord user was not found!") - ); - - event.reply(builder); - }); - } -} From 1131b91d3c06c16a5a2618c3407392b930a9c10a Mon Sep 17 00:00:00 2001 From: Hammer86gn Date: Tue, 2 Mar 2021 14:11:20 -0500 Subject: [PATCH 3/3] Fixed all the code! --- .../command/argument/impl/types/LongArgument.java | 12 ++++++++++++ .../bot/command/impl/other/ChannelMuteCommand.java | 13 ++++++++----- .../com/diamondfire/helpbot/bot/config/Config.java | 4 ---- 3 files changed, 20 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/diamondfire/helpbot/bot/command/argument/impl/types/LongArgument.java 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 index 098a4fe1..b3866147 100644 --- 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 @@ -37,11 +37,10 @@ public HelpContext getHelpContext() { @Override protected ArgumentSet compileArguments() { - LocalDate nextWeek = LocalDate.now().plusDays(7); return new ArgumentSet() .addArgument("user", new DiscordUserArgument()) - .addArgument("channel", new StringArgument()) - .addArgument("duration", new SingleArgumentContainer<>(new TimeOffsetArgument()).optional(DateUtil.toDate(nextWeek))) + .addArgument("channel", new LongArgument()) + .addArgument("duration", new SingleArgumentContainer<>(new TimeOffsetArgument()).optional(null)) .addArgument("reason", new StringArgument()); } @@ -54,13 +53,17 @@ public Permission getPermission() { public void run(CommandEvent event) { PresetBuilder builder = new PresetBuilder(); long user = event.getArgument("user"); - long channel = Long.parseLong(event.getArgument("channel")); + 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 @@ -74,7 +77,7 @@ public void run(CommandEvent event) { 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, duration, false)); + HelpBotInstance.getScheduler().schedule(new MuteExpireTask(user, finalDuration, false)); event.reply(builder); }, (error) -> { builder.withPreset( diff --git a/src/main/java/com/diamondfire/helpbot/bot/config/Config.java b/src/main/java/com/diamondfire/helpbot/bot/config/Config.java index ec9a3e3c..36106e8a 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/config/Config.java +++ b/src/main/java/com/diamondfire/helpbot/bot/config/Config.java @@ -68,10 +68,6 @@ public long getDiscussionChannel() { return getPropertyLong("discussion_channel"); } - public long getClipsChannel() { - return getPropertyLong("clips_channel"); - } - public long getMutedRole() { return getPropertyLong("muted_role"); }