diff --git a/pom.xml b/pom.xml index aec47e3..e1aacdf 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.jagrosh Vortex - 2.0 + 2.1 jar diff --git a/src/main/java/com/jagrosh/vortex/Vortex.java b/src/main/java/com/jagrosh/vortex/Vortex.java index 72d0038..8c55d40 100644 --- a/src/main/java/com/jagrosh/vortex/Vortex.java +++ b/src/main/java/com/jagrosh/vortex/Vortex.java @@ -156,6 +156,7 @@ new UnignoreCmd(this), // Tools + new AnnounceCmd(), new AuditCmd(), new DehoistCmd(), new InvitepruneCmd(this), diff --git a/src/main/java/com/jagrosh/vortex/commands/tools/AnnounceCmd.java b/src/main/java/com/jagrosh/vortex/commands/tools/AnnounceCmd.java new file mode 100644 index 0000000..5b7c9f3 --- /dev/null +++ b/src/main/java/com/jagrosh/vortex/commands/tools/AnnounceCmd.java @@ -0,0 +1,103 @@ +/* + * Copyright 2018 John Grosh (john.a.grosh@gmail.com). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.jagrosh.vortex.commands.tools; + +import com.jagrosh.jdautilities.command.Command; +import com.jagrosh.jdautilities.command.CommandEvent; +import com.jagrosh.jdautilities.commons.utils.FinderUtil; +import com.jagrosh.vortex.commands.CommandExceptionListener.CommandErrorException; +import com.jagrosh.vortex.commands.CommandExceptionListener.CommandWarningException; +import com.jagrosh.vortex.utils.FormatUtil; +import java.util.List; +import net.dv8tion.jda.core.Permission; +import net.dv8tion.jda.core.entities.Role; +import net.dv8tion.jda.core.entities.TextChannel; + +/** + * + * @author John Grosh (john.a.grosh@gmail.com) + */ +public class AnnounceCmd extends Command +{ + public AnnounceCmd() + { + this.name = "announce"; + this.aliases = new String[]{"announcement", "announcer"}; + this.arguments = "<#channel> | "; + this.help = "pings a role with an announcement in the specified channel"; + this.category = new Category("Tools"); + this.botPermissions = new Permission[]{Permission.MANAGE_ROLES}; + this.userPermissions = new Permission[]{Permission.MANAGE_ROLES}; + this.guildOnly = true; + this.cooldown = 3; + this.cooldownScope = CooldownScope.GUILD; + } + + @Override + protected void execute(CommandEvent event) + { + if(event.getArgs().isEmpty()) + throw new CommandErrorException("Please include a channel and role name, a | as a separator, and a message to send!"); + String[] parts = event.getArgs().split("\\s+", 2); + List list = FinderUtil.findTextChannels(parts[0], event.getGuild()); + if(list.isEmpty()) + throw new CommandErrorException("I couldn't find any text channel called `"+parts[0]+"`."); + if(list.size()>1) + throw new CommandWarningException(FormatUtil.listOfText(list, parts[0])); + TextChannel tc = list.get(0); + if(!tc.canTalk()) + throw new CommandWarningException("I do not have permission to Send Messages in "+tc.getAsMention()+"!"); + if(!tc.canTalk(event.getMember())) + throw new CommandErrorException("You do not have permission to Send Messages in "+tc.getAsMention()+"!"); + if(parts.length<2) + throw new CommandErrorException("Please include a channel and role name, a | as a separator, and a message to send!"); + String[] parts2 = parts[1].split("\\s*\\|\\s*", 2); + List rlist = FinderUtil.findRoles(parts2[0], event.getGuild()); + if(rlist.isEmpty()) + throw new CommandErrorException("I couldn't find any role called `"+parts2[0]+"`."); + if(rlist.size()>1) + throw new CommandWarningException(FormatUtil.listOfRoles(rlist, parts2[0])); + Role role = rlist.get(0); + if(!event.getSelfMember().canInteract(role)) + throw new CommandWarningException("I cannot modify the role `"+role.getName()+"`. Try moving my highest role above the `"+role.getName()+"` role."); + if(!event.getMember().canInteract(role)) + throw new CommandErrorException("You cannot modify the `"+role.getName()+"` role."); + if(parts2.length<2 || parts2[1].isEmpty()) + throw new CommandErrorException("Please include a channel and role name, a | as a separator, and a message to send!"); + + if(!role.isMentionable()) + { + role.getManager().setMentionable(true).queue(s -> + { + tc.sendMessage(role.getAsMention()+": "+parts2[1]).queue(m -> + { + event.replySuccess("Announcement for `"+role.getName()+"` sent to "+tc.getAsMention()+"!"); + role.getManager().setMentionable(false).queue(s2->{}, f2->{}); + }, f -> + { + event.replyError("Failed to send message."); + role.getManager().setMentionable(false).queue(s2->{}, f2->{}); + }); + }, f -> event.replyError("Failed to modify the role `"+role.getName()+"`.")); + } + else + { + tc.sendMessage(role.getAsMention()+": "+parts2[1]).queue( + m -> event.replySuccess("Announcement for `"+role.getName()+"` sent to "+tc.getAsMention()+"!"), + f -> event.replyError("Failed to send message.")); + } + } +} diff --git a/src/main/java/com/jagrosh/vortex/commands/tools/DehoistCmd.java b/src/main/java/com/jagrosh/vortex/commands/tools/DehoistCmd.java index cd408eb..aaafff4 100644 --- a/src/main/java/com/jagrosh/vortex/commands/tools/DehoistCmd.java +++ b/src/main/java/com/jagrosh/vortex/commands/tools/DehoistCmd.java @@ -17,13 +17,9 @@ import com.jagrosh.jdautilities.command.Command; import com.jagrosh.jdautilities.command.CommandEvent; -import com.jagrosh.vortex.automod.AutoMod; import com.jagrosh.vortex.commands.CommandExceptionListener.CommandErrorException; import com.jagrosh.vortex.utils.OtherUtil; -import java.util.List; -import java.util.stream.Collectors; import net.dv8tion.jda.core.Permission; -import net.dv8tion.jda.core.entities.Member; /** *