Allow captcha verification in guild channels

This commit is contained in:
Ghoti 2021-08-05 18:32:32 -05:00
parent 7a2d31233a
commit 8d91f00fda
12 changed files with 142 additions and 71 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.fpghoti</groupId> <groupId>com.fpghoti</groupId>
<artifactId>Biscuit</artifactId> <artifactId>Biscuit</artifactId>
<version>1.9</version> <version>1.10</version>
<build> <build>
<sourceDirectory>src/main/java</sourceDirectory> <sourceDirectory>src/main/java</sourceDirectory>
<resources> <resources>
@ -103,7 +103,7 @@
</dependency> </dependency>
</dependencies> </dependencies>
<properties> <properties>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties> </properties>
</project> </project>

View File

@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.fpghoti</groupId> <groupId>com.fpghoti</groupId>
<artifactId>Biscuit</artifactId> <artifactId>Biscuit</artifactId>
<version>1.9</version> <version>1.10</version>
<build> <build>
<sourceDirectory>src/main/java</sourceDirectory> <sourceDirectory>src/main/java</sourceDirectory>
<resources> <resources>

View File

@ -17,54 +17,60 @@ import com.github.cage.Cage;
import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.entities.PrivateChannel; import net.dv8tion.jda.api.entities.PrivateChannel;
import net.dv8tion.jda.api.entities.Role; import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.message.priv.PrivateMessageReceivedEvent;
public class Captcha { public class Captcha {
public static Captcha getUpdatedCaptcha(PrivateMessageReceivedEvent event) { public static Captcha getUpdatedCaptcha(User user, MessageChannel channel, String response) {
CaptchaUser c = CaptchaUser.getCaptchaUser(event.getAuthor()); CaptchaUser c = CaptchaUser.getCaptchaUser(user);
if(c.getCaptcha() == null) { if(c.getCaptcha() == null) {
Captcha captcha = new Captcha(event); Captcha captcha = new Captcha(user, channel, response);
c.setCaptcha(captcha); c.setCaptcha(captcha);
return captcha; return captcha;
}else { }else {
Captcha captcha = c.getCaptcha(); Captcha captcha = c.getCaptcha();
captcha.setEvent(event); captcha.setChannel(channel);
captcha.setResponse(response);
c.setCaptcha(captcha); c.setCaptcha(captcha);
return captcha; return captcha;
} }
} }
private CaptchaUser user; private CaptchaUser captchaUser;
private PrivateMessageReceivedEvent event; private MessageChannel channel;
private PrivateChannel channel; private User user;
private User author; private String response;
private String token; private String token;
private Captcha(PrivateMessageReceivedEvent event) { private Captcha(User user, MessageChannel channel, String response) {
this.user = CaptchaUser.getCaptchaUser(event.getAuthor()); this.captchaUser = CaptchaUser.getCaptchaUser(user);
this.event = event; this.channel = channel;
this.channel = event.getChannel(); this.user = user;
this.author = event.getAuthor(); this.response = response;
this.token = null; this.token = null;
} }
public void setEvent(PrivateMessageReceivedEvent event) { private void setChannel(MessageChannel channel) {
this.event = event; this.channel = channel;
}
public User getAuthor() {
return author;
} }
public CaptchaUser getCaptchaUser() { private void setResponse(String response) {
this.response = response;
}
public User getUser() {
return user; return user;
} }
public PrivateChannel getChannel() { public CaptchaUser getCaptchaUser() {
return captchaUser;
}
public MessageChannel getChannel() {
return channel; return channel;
} }
@ -78,20 +84,20 @@ public class Captcha {
} }
public void handleResponse() { public void handleResponse() {
if(user.isEmpty() && !user.inTestMode()) { if(captchaUser.isEmpty() && !captchaUser.inTestMode()) {
return; return;
} }
String response = leeway(event.getMessage().getContentDisplay()); String captchaString = leeway(response);
if(token != null && !response.equalsIgnoreCase(token)) { if(token != null && !captchaString.equalsIgnoreCase(token)) {
respond("Sorry! That's not quite right! Please try again."); respond("Sorry! That's not quite right! Please try again.");
return; return;
} }
if(token == null) { if(token == null) {
log("Generating captcha challenge for user " + author.getName() + " " + author.getAsMention() + "..."); log("Generating captcha challenge for user " + user.getName() + " " + user.getAsMention() + "...");
genToken(); genToken();
generateImage(); generateImage();
@ -102,14 +108,14 @@ public class Captcha {
}else { }else {
boolean disable = false; boolean disable = false;
if(user.inTestMode()) { if(captchaUser.inTestMode()) {
disable = true; disable = true;
}else { }else {
doCaptchaReward(); doCaptchaReward();
} }
respond("Well done, " + author.getAsMention() + "!"); respond("Well done, " + user.getAsMention() + "!");
if(disable) { if(disable) {
user.disableTestMode(); captchaUser.disableTestMode();
} }
} }
} }
@ -121,10 +127,10 @@ public class Captcha {
try { try {
if(!Main.isPlugin) { if(!Main.isPlugin) {
//If Biscuit is running standalone output to this directory //If Biscuit is running standalone output to this directory
os = new FileOutputStream("captcha/" + author.getId() + ".jpg", false); os = new FileOutputStream("captcha/" + user.getId() + ".jpg", false);
}else { }else {
//If Biscuit is running as a Spigot plugin output to this directory //If Biscuit is running as a Spigot plugin output to this directory
File c = new File(PluginCore.plugin.getDataFolder(), "captcha/" + author.getId() + ".jpg"); File c = new File(PluginCore.plugin.getDataFolder(), "captcha/" + user.getId() + ".jpg");
os = new FileOutputStream(c, false); os = new FileOutputStream(c, false);
} }
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
@ -151,14 +157,14 @@ public class Captcha {
public File getImageFile() { public File getImageFile() {
if(!Main.isPlugin) { if(!Main.isPlugin) {
return new File("captcha/" + author.getId() + ".jpg"); return new File("captcha/" + user.getId() + ".jpg");
}else { }else {
return new File(PluginCore.plugin.getDataFolder(), "captcha/" + author.getId() + ".jpg"); return new File(PluginCore.plugin.getDataFolder(), "captcha/" + user.getId() + ".jpg");
} }
} }
public void doCaptchaReward() { public void doCaptchaReward() {
for(PreUser p : user) { for(PreUser p : captchaUser) {
//mark the PreUser as "done" //mark the PreUser as "done"
p.setDone(); p.setDone();
@ -189,15 +195,15 @@ public class Captcha {
return; return;
} }
Member member = g.getMemberById(author.getId()); Member member = g.getMemberById(user.getId());
g.addRoleToMember(member, newrole).complete(); g.addRoleToMember(member, newrole).complete();
g.removeRoleFromMember(member, defaultrole).complete(); g.removeRoleFromMember(member, defaultrole).complete();
p.remove(); p.remove();
token = null; token = null;
log(BColor.YELLOW_BOLD + author.getName() + " successfully completed a captcha challenge. Granting role."); log(BColor.YELLOW_BOLD + user.getName() + " successfully completed a captcha challenge. Granting role.");
biscuit.eventLog(" ``" + author.getName() +"`` " + author.getAsMention() + " successfully completed a captcha challenge. Granting role."); biscuit.eventLog(" ``" + user.getName() +"`` " + user.getAsMention() + " successfully completed a captcha challenge. Granting role.");
} }
} }
@ -205,10 +211,10 @@ public class Captcha {
File captcha; File captcha;
if(!Main.isPlugin) { if(!Main.isPlugin) {
//Biscuit is running standalone. Remove file from this directory //Biscuit is running standalone. Remove file from this directory
captcha = new File("captcha/" + user.getUser().getId() + ".jpg"); captcha = new File("captcha/" + captchaUser.getUser().getId() + ".jpg");
}else { }else {
//Biscuit is running as Spigot plugin. Remove file from this directory //Biscuit is running as Spigot plugin. Remove file from this directory
captcha = new File(PluginCore.plugin.getDataFolder(), "captcha/" + user.getUser().getId() + ".jpg"); captcha = new File(PluginCore.plugin.getDataFolder(), "captcha/" + captchaUser.getUser().getId() + ".jpg");
} }
captcha.delete(); captcha.delete();
} }
@ -222,7 +228,7 @@ public class Captcha {
private void log(String msg) { private void log(String msg) {
String prefix = ""; String prefix = "";
if(user.inTestMode()) { if(captchaUser.inTestMode()) {
prefix = "[TEST] "; prefix = "[TEST] ";
} }
Main.getMainBiscuit().log(prefix + msg); Main.getMainBiscuit().log(prefix + msg);
@ -230,10 +236,14 @@ public class Captcha {
private void respond(String msg) { private void respond(String msg) {
String prefix = ""; String prefix = "";
if(user.inTestMode()) { if(captchaUser.inTestMode()) {
prefix = "[TEST] "; prefix = "[TEST] ";
} }
MessageText.send(channel, prefix + msg); if(channel instanceof TextChannel) {
MessageText.send((TextChannel)channel, prefix + msg);
}else if(channel instanceof PrivateChannel) {
MessageText.send((PrivateChannel)channel, prefix + msg);
}
} }
} }

View File

@ -0,0 +1,5 @@
package com.fpghoti.biscuit.captcha;
public class CaptchaChannel {
}

View File

@ -0,0 +1,48 @@
package com.fpghoti.biscuit.captcha;
import com.fpghoti.biscuit.Main;
import com.fpghoti.biscuit.biscuit.Biscuit;
import com.fpghoti.biscuit.user.CaptchaUser;
import com.fpghoti.biscuit.util.PermUtil;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.User;
public class HandleCaptcha {
public static void handleCaptcha(User user, MessageChannel channel, String content) {
//String content = event.getMessage().getContentDisplay();
Captcha captcha = Captcha.getUpdatedCaptcha(user, channel, content);
CaptchaUser capUser = captcha.getCaptchaUser();
//User is requesting a captcha test
if(content.equalsIgnoreCase("captcha pls") || content.equalsIgnoreCase("cpls")) {
JDA jda = Main.getJDA();
for(Guild g : jda.getGuilds()) {
if(g.isMember(capUser.getUser())) {
Member m = g.getMember(capUser.getUser());
if(PermUtil.isAdmin(m)) {
capUser.enableTestMode();
}
}
}
}
captcha.handleResponse();
}
public static Boolean isCaptchaChannel(TextChannel c) {
Biscuit biscuit = Biscuit.getBiscuit(c.getGuild());
Boolean a = true;
for(String s: biscuit.getProperties().getCaptchaChannels()) {
if(c.getName().equalsIgnoreCase(s)) {
a = false;
}
}
return a;
}
}

View File

@ -181,7 +181,8 @@ public class BiscuitConfig {
added = addProperty("Allow-Music-Bot", "true", prop, added, silent); added = addProperty("Allow-Music-Bot", "true", prop, added, silent);
added = addProperty("Music-Channels", "", prop, added, silent); added = addProperty("Music-Channels", "", prop, added, silent);
added = addProperty("Music-Controller-Role", "music-key", prop, added, silent); added = addProperty("Music-Controller-Role", "music-key", prop, added, silent);
added = addProperty("Captcha-Channels", "captcha,verification", prop, added, silent);
if(!isMain) { if(!isMain) {
if(code != null) { if(code != null) {
prop.setProperty("Guild-Code", code); prop.setProperty("Guild-Code", code);

View File

@ -380,4 +380,17 @@ public class BiscuitProperties {
} }
return list; return list;
} }
public String[] getCaptchaChannels(){
String key = "Captcha-Channels";
if(biscuit.getConfig().getFromConfig(key).equalsIgnoreCase("[global]") && biscuit.getGuild() != null) {
return Main.getMainBiscuit().getProperties().getToggleChannels();
}
String [] list = biscuit.getConfig().getFromConfig(key).replace(" ", "").split(",");
if(list.length == 1 && list[0].equals("")) {
String[] blank = {};
return blank;
}
return list;
}
} }

View File

@ -2,6 +2,7 @@ package com.fpghoti.biscuit.listener;
import com.fpghoti.biscuit.Main; import com.fpghoti.biscuit.Main;
import com.fpghoti.biscuit.captcha.Captcha; import com.fpghoti.biscuit.captcha.Captcha;
import com.fpghoti.biscuit.captcha.HandleCaptcha;
import com.fpghoti.biscuit.logging.BColor; import com.fpghoti.biscuit.logging.BColor;
import com.fpghoti.biscuit.user.CaptchaUser; import com.fpghoti.biscuit.user.CaptchaUser;
import com.fpghoti.biscuit.util.PermUtil; import com.fpghoti.biscuit.util.PermUtil;
@ -25,23 +26,7 @@ public class DMListener extends ListenerAdapter{
Main.getMainBiscuit().log(BColor.YELLOW + event.getAuthor().getName() + ": " + BColor.WHITE_BOLD + event.getMessage().getContentDisplay()); Main.getMainBiscuit().log(BColor.YELLOW + event.getAuthor().getName() + ": " + BColor.WHITE_BOLD + event.getMessage().getContentDisplay());
} }
String content = event.getMessage().getContentDisplay(); HandleCaptcha.handleCaptcha(event.getAuthor(), event.getChannel(), event.getMessage().getContentDisplay());
Captcha captcha = Captcha.getUpdatedCaptcha(event);
CaptchaUser capUser = captcha.getCaptchaUser();
//User is requesting a captcha test
if(content.equalsIgnoreCase("captcha pls") || content.equalsIgnoreCase("cpls")) {
JDA jda = Main.getJDA();
for(Guild g : jda.getGuilds()) {
if(g.isMember(capUser.getUser())) {
Member m = g.getMember(capUser.getUser());
if(PermUtil.isAdmin(m)) {
capUser.enableTestMode();
}
}
}
}
captcha.handleResponse();
} }
} }

View File

@ -2,6 +2,7 @@ package com.fpghoti.biscuit.listener;
import com.fpghoti.biscuit.biscuit.Biscuit; import com.fpghoti.biscuit.biscuit.Biscuit;
import com.fpghoti.biscuit.biscuit.BiscuitMessageStore; import com.fpghoti.biscuit.biscuit.BiscuitMessageStore;
import com.fpghoti.biscuit.captcha.HandleCaptcha;
import com.fpghoti.biscuit.logging.BColor; import com.fpghoti.biscuit.logging.BColor;
import com.fpghoti.biscuit.rest.MessageText; import com.fpghoti.biscuit.rest.MessageText;
import com.fpghoti.biscuit.util.ChatFilter; import com.fpghoti.biscuit.util.ChatFilter;
@ -26,19 +27,22 @@ public class MessageReceiveListener extends ListenerAdapter{
return; return;
} }
PermUtil.clearUndeservedRoles(event.getMember()); PermUtil.clearUndeservedRoles(event.getMember());
logUser(event, biscuit); logUser(event, biscuit);
if(isNaughty(event)) { if(isNaughty(event)) {
return; return;
} }
if(handleSoftmuted(event, biscuit)) { if(handleSoftmuted(event, biscuit)) {
return; return;
} }
if(!handleSpammer(event, biscuit) && biscuit.getProperties().spamPunishAllow()){ if(!handleSpammer(event, biscuit) && biscuit.getProperties().spamPunishAllow()){
checkNewSpammer(event, biscuit); checkNewSpammer(event, biscuit);
} }
//Channel is a captcha channel
if(HandleCaptcha.isCaptchaChannel(event.getChannel())) {
HandleCaptcha.handleCaptcha(event.getAuthor(), event.getChannel(), event.getMessage().getContentDisplay());
}
} }
private void logBot(GuildMessageReceivedEvent event, Biscuit biscuit) { private void logBot(GuildMessageReceivedEvent event, Biscuit biscuit) {

View File

@ -9,6 +9,7 @@ import com.fpghoti.biscuit.captcha.Captcha;
import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.message.priv.PrivateMessageReceivedEvent; import net.dv8tion.jda.api.events.message.priv.PrivateMessageReceivedEvent;
@ -92,8 +93,8 @@ public class CaptchaUser implements Iterable<PreUser>{
return user; return user;
} }
public Captcha getCaptcha(PrivateMessageReceivedEvent event) { public Captcha getCaptcha(MessageChannel channel, String content) {
captcha = Captcha.getUpdatedCaptcha(event); captcha = Captcha.getUpdatedCaptcha(user, channel, content);
return captcha; return captcha;
} }

View File

@ -25,7 +25,7 @@ public class Util {
} }
return a; return a;
} }
public static boolean isDigit(String s){ public static boolean isDigit(String s){
return s.matches("[0-9]+"); return s.matches("[0-9]+");
} }

View File

@ -59,6 +59,10 @@ Done-Emote = ActionComplete
#Role will be replaced with reward role on clear #Role will be replaced with reward role on clear
Captcha = false Captcha = false
#All channels listed here will allow for users in these channels
#to initiate captcha with the bot.
Captcha-Channels = captcha,verification
#Reward this role when captcha cleared #Reward this role when captcha cleared
Captcha-Reward-Role = cleared Captcha-Reward-Role = cleared