Created
November 8, 2017 21:54
-
-
Save jagrosh/c00753fb8c0facca9d2850ddc00b3aa7 to your computer and use it in GitHub Desktop.
Bug/Feature Requests bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2017 John Grosh (jagrosh). | |
* | |
* 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 jagbot; | |
import com.jagrosh.jdautilities.waiter.EventWaiter; | |
import java.awt.Color; | |
import java.util.concurrent.TimeUnit; | |
import net.dv8tion.jda.core.EmbedBuilder; | |
import net.dv8tion.jda.core.MessageBuilder; | |
import net.dv8tion.jda.core.entities.MessageEmbed; | |
import net.dv8tion.jda.core.entities.User; | |
import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent; | |
import net.dv8tion.jda.core.events.message.priv.PrivateMessageReceivedEvent; | |
import net.dv8tion.jda.core.events.message.react.MessageReactionAddEvent; | |
import net.dv8tion.jda.core.hooks.ListenerAdapter; | |
/** | |
* | |
* @author John Grosh ([email protected]) | |
*/ | |
public class SuggestBot extends ListenerAdapter { | |
private final EventWaiter waiter; | |
private final static long JAGGUILD = 147698382092238848L; | |
private final static long JAGID = 113156185389092864L; | |
private final String RESPOND = "\uD83D\uDCDD"; | |
private final String APPROVE = "\u2705"; | |
private final String DENY = "\u26D4"; | |
private final String DELETE = "\uD83D\uDDD1"; | |
public SuggestBot(EventWaiter waiter) | |
{ | |
this.waiter = waiter; | |
} | |
@Override | |
public void onGuildMessageReceived(GuildMessageReceivedEvent event) { | |
if(event.getGuild().getIdLong()==JAGGUILD) | |
{ | |
String[] parts = event.getMessage().getRawContent().split("\\s+", 3); | |
Type type = null; | |
for(Type t: Type.values()) | |
if(t.command.equalsIgnoreCase(parts[0])) | |
type = t; | |
if(type!=null) | |
{ | |
if(event.getChannel().getIdLong()!=147698606739161088L && event.getChannel().getIdLong()!=159849470413111296L) | |
{ | |
event.getMessage().delete().queue(); | |
event.getAuthor().openPrivateChannel().queue(s -> s.sendMessage("Please use a testing channel for that command!").queue()); | |
} | |
else if(parts.length<3) | |
{ | |
event.getChannel().sendMessage("Missing parameters! Usage: `"+type.command+" <botname> <"+type.name+">`").queue(); | |
} | |
else | |
{ | |
Program program = null; | |
for(Program p: Program.values()) | |
for(String name: p.names) | |
if(name.equalsIgnoreCase(parts[1])) | |
program = p; | |
if(program==null) | |
{ | |
String str = "Invalid parameter! Usage: `"+type.command+" <botname> <"+type.name+">`\nBotname must be one of the following:"; | |
for(Program p: Program.values()) | |
str+=" "+p.names[0]; | |
event.getChannel().sendMessage(str).queue(); | |
} | |
else | |
{ | |
MessageBuilder mb = new MessageBuilder(); | |
EmbedBuilder eb = new EmbedBuilder(); | |
eb.setAuthor(event.getAuthor().getName()+"#"+event.getAuthor().getDiscriminator(), null, event.getAuthor().getEffectiveAvatarUrl()); | |
StringBuilder str = new StringBuilder(parts[2]); | |
event.getMessage().getAttachments().forEach(a -> str.append("\n").append(a.getUrl())); | |
eb.setDescription(str.toString()); | |
User p = event.getJDA().getUserById(program.id); | |
eb.setFooter(event.getAuthor().getId(), p.getEffectiveAvatarUrl()); | |
mb.append(program.names[0]+" "+type.name); | |
mb.setEmbed(eb.build()); | |
event.getGuild().getTextChannelById(type.channelId).sendMessage(mb.build()).queue(s -> { | |
s.addReaction(APPROVE).queue(); | |
s.addReaction(RESPOND).queue(); | |
s.addReaction(DENY).queue(); | |
s.addReaction(DELETE).queue(); | |
}); | |
event.getChannel().sendMessage("Your "+type.name+" for "+program.names[0]+" has been added to <#"+type.channelId+">!").queue(); | |
} | |
} | |
} | |
} | |
} | |
@Override | |
public void onMessageReactionAdd(MessageReactionAddEvent event) { | |
if(event.getUser().getIdLong()==JAGID) | |
{ | |
Type type = null; | |
for(Type t: Type.values()) | |
if(event.getChannel().getIdLong()==t.channelId) | |
type = t; | |
if(type!=null) | |
{ | |
switch(event.getReactionEmote().getName()) { | |
case APPROVE: | |
event.getChannel().getMessageById(event.getMessageIdLong()).queue(m -> { | |
MessageEmbed embed = m.getEmbeds().get(0); | |
m.editMessage(new MessageBuilder().append(m.getRawContent()).setEmbed(new EmbedBuilder(embed).setColor(Color.GREEN).build()).build()).queue(); | |
User u = event.getJDA().getUserById(embed.getFooter().getText()); | |
if(u!=null) | |
u.openPrivateChannel().queue(pc -> pc.sendMessage("Your "+m.getRawContent()+" has been approved!").queue()); | |
}); | |
break; | |
case DENY: | |
event.getChannel().getMessageById(event.getMessageIdLong()).queue(m -> { | |
MessageEmbed embed = m.getEmbeds().get(0); | |
m.editMessage(new MessageBuilder().append(m.getRawContent()).setEmbed(new EmbedBuilder(embed).setColor(Color.RED).build()).build()).queue(); | |
User u = event.getJDA().getUserById(embed.getFooter().getText()); | |
if(u!=null) | |
u.openPrivateChannel().queue(pc -> pc.sendMessage("Your "+m.getRawContent()+" has been denied!").queue()); | |
}); | |
break; | |
case DELETE: | |
event.getChannel().deleteMessageById(event.getMessageIdLong()).queue(); | |
break; | |
case RESPOND: | |
event.getChannel().getMessageById(event.getMessageIdLong()).queue(m -> { | |
MessageEmbed embed = m.getEmbeds().get(0); | |
event.getJDA().getUserById(JAGID).openPrivateChannel().queue(pc -> { | |
pc.sendMessage("How would you like to respond to this "+m.getRawContent()+"?").queue(); | |
waiter.waitForEvent(PrivateMessageReceivedEvent.class, pe->pe.getAuthor().getIdLong()==JAGID, pe->{ | |
m.editMessage(new MessageBuilder().append(m.getRawContent()).setEmbed(new EmbedBuilder(embed).addField("jagrosh:", pe.getMessage().getRawContent(), false).build()).build()).queue(); | |
User u = event.getJDA().getUserById(embed.getFooter().getText()); | |
if(u!=null) | |
u.openPrivateChannel().queue(pc2 -> pc2.sendMessage("Your "+m.getRawContent()+" has gotten a response:\n"+pe.getMessage().getRawContent()).queue()); | |
}, 5, TimeUnit.MINUTES, ()->pc.sendMessage("You took too long...").queue()); | |
}); | |
}); | |
break; | |
} | |
} | |
} | |
} | |
private enum Type { | |
FEATURE(159849695307497472L, "+feature","feature request"), | |
BUG(159849755294302208L, "+bug","bug report"); | |
private final long channelId; | |
private final String command, name; | |
private Type(long channelId, String command, String name) | |
{ | |
this.channelId = channelId; | |
this.command = command; | |
this.name = name; | |
} | |
} | |
private enum Program { | |
SPECTRA(135251434445733888L, "spectra"), | |
VORTEX(240254129333731328L, "vortex"), | |
GIVEAWAY(294882584201003009L, "giveawaybot","giveaway","giveaways"), | |
YGGDRASIL(247283454440374274L, "yggdrasil","ygg"), | |
FROST(197531150900658176L, "frost","cleverbot","frostcleverbot","jcleverbot"), | |
FLOPPY(159133099806949376L, "floppy","floppybot","floppydisk"), | |
MUSICBOT(174202555981758464L, "musicbot","music","clarity"); | |
private final long id; | |
private final String[] names; | |
private Program(long id, String... names) | |
{ | |
this.id = id; | |
this.names = names; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment