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

View File

@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.fpghoti</groupId>
<artifactId>Biscuit</artifactId>
<version>1.9</version>
<version>1.10</version>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<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.Member;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.entities.PrivateChannel;
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.events.message.priv.PrivateMessageReceivedEvent;
public class Captcha {
public static Captcha getUpdatedCaptcha(PrivateMessageReceivedEvent event) {
CaptchaUser c = CaptchaUser.getCaptchaUser(event.getAuthor());
public static Captcha getUpdatedCaptcha(User user, MessageChannel channel, String response) {
CaptchaUser c = CaptchaUser.getCaptchaUser(user);
if(c.getCaptcha() == null) {
Captcha captcha = new Captcha(event);
Captcha captcha = new Captcha(user, channel, response);
c.setCaptcha(captcha);
return captcha;
}else {
Captcha captcha = c.getCaptcha();
captcha.setEvent(event);
captcha.setChannel(channel);
captcha.setResponse(response);
c.setCaptcha(captcha);
return captcha;
}
}
private CaptchaUser user;
private PrivateMessageReceivedEvent event;
private PrivateChannel channel;
private User author;
private CaptchaUser captchaUser;
private MessageChannel channel;
private User user;
private String response;
private String token;
private Captcha(PrivateMessageReceivedEvent event) {
this.user = CaptchaUser.getCaptchaUser(event.getAuthor());
this.event = event;
this.channel = event.getChannel();
this.author = event.getAuthor();
private Captcha(User user, MessageChannel channel, String response) {
this.captchaUser = CaptchaUser.getCaptchaUser(user);
this.channel = channel;
this.user = user;
this.response = response;
this.token = null;
}
public void setEvent(PrivateMessageReceivedEvent event) {
this.event = event;
}
public User getAuthor() {
return author;
private void setChannel(MessageChannel channel) {
this.channel = channel;
}
public CaptchaUser getCaptchaUser() {
private void setResponse(String response) {
this.response = response;
}
public User getUser() {
return user;
}
public PrivateChannel getChannel() {
public CaptchaUser getCaptchaUser() {
return captchaUser;
}
public MessageChannel getChannel() {
return channel;
}
@ -78,20 +84,20 @@ public class Captcha {
}
public void handleResponse() {
if(user.isEmpty() && !user.inTestMode()) {
if(captchaUser.isEmpty() && !captchaUser.inTestMode()) {
return;
}
String response = leeway(event.getMessage().getContentDisplay());
if(token != null && !response.equalsIgnoreCase(token)) {
String captchaString = leeway(response);
if(token != null && !captchaString.equalsIgnoreCase(token)) {
respond("Sorry! That's not quite right! Please try again.");
return;
}
if(token == null) {
log("Generating captcha challenge for user " + author.getName() + " " + author.getAsMention() + "...");
log("Generating captcha challenge for user " + user.getName() + " " + user.getAsMention() + "...");
genToken();
generateImage();
@ -102,14 +108,14 @@ public class Captcha {
}else {
boolean disable = false;
if(user.inTestMode()) {
if(captchaUser.inTestMode()) {
disable = true;
}else {
doCaptchaReward();
}
respond("Well done, " + author.getAsMention() + "!");
respond("Well done, " + user.getAsMention() + "!");
if(disable) {
user.disableTestMode();
captchaUser.disableTestMode();
}
}
}
@ -121,10 +127,10 @@ public class Captcha {
try {
if(!Main.isPlugin) {
//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 {
//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);
}
} catch (FileNotFoundException e) {
@ -151,14 +157,14 @@ public class Captcha {
public File getImageFile() {
if(!Main.isPlugin) {
return new File("captcha/" + author.getId() + ".jpg");
return new File("captcha/" + user.getId() + ".jpg");
}else {
return new File(PluginCore.plugin.getDataFolder(), "captcha/" + author.getId() + ".jpg");
return new File(PluginCore.plugin.getDataFolder(), "captcha/" + user.getId() + ".jpg");
}
}
public void doCaptchaReward() {
for(PreUser p : user) {
for(PreUser p : captchaUser) {
//mark the PreUser as "done"
p.setDone();
@ -189,15 +195,15 @@ public class Captcha {
return;
}
Member member = g.getMemberById(author.getId());
Member member = g.getMemberById(user.getId());
g.addRoleToMember(member, newrole).complete();
g.removeRoleFromMember(member, defaultrole).complete();
p.remove();
token = null;
log(BColor.YELLOW_BOLD + author.getName() + " successfully completed a captcha challenge. Granting role.");
biscuit.eventLog(" ``" + author.getName() +"`` " + author.getAsMention() + " successfully completed a captcha challenge. Granting role.");
log(BColor.YELLOW_BOLD + user.getName() + " 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;
if(!Main.isPlugin) {
//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 {
//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();
}
@ -222,7 +228,7 @@ public class Captcha {
private void log(String msg) {
String prefix = "";
if(user.inTestMode()) {
if(captchaUser.inTestMode()) {
prefix = "[TEST] ";
}
Main.getMainBiscuit().log(prefix + msg);
@ -230,10 +236,14 @@ public class Captcha {
private void respond(String msg) {
String prefix = "";
if(user.inTestMode()) {
if(captchaUser.inTestMode()) {
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("Music-Channels", "", 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(code != null) {
prop.setProperty("Guild-Code", code);

View File

@ -380,4 +380,17 @@ public class BiscuitProperties {
}
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.captcha.Captcha;
import com.fpghoti.biscuit.captcha.HandleCaptcha;
import com.fpghoti.biscuit.logging.BColor;
import com.fpghoti.biscuit.user.CaptchaUser;
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());
}
String content = 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();
HandleCaptcha.handleCaptcha(event.getAuthor(), event.getChannel(), event.getMessage().getContentDisplay());
}
}

View File

@ -2,6 +2,7 @@ package com.fpghoti.biscuit.listener;
import com.fpghoti.biscuit.biscuit.Biscuit;
import com.fpghoti.biscuit.biscuit.BiscuitMessageStore;
import com.fpghoti.biscuit.captcha.HandleCaptcha;
import com.fpghoti.biscuit.logging.BColor;
import com.fpghoti.biscuit.rest.MessageText;
import com.fpghoti.biscuit.util.ChatFilter;
@ -26,19 +27,22 @@ public class MessageReceiveListener extends ListenerAdapter{
return;
}
PermUtil.clearUndeservedRoles(event.getMember());
logUser(event, biscuit);
if(isNaughty(event)) {
return;
}
if(handleSoftmuted(event, biscuit)) {
return;
}
if(!handleSpammer(event, biscuit) && biscuit.getProperties().spamPunishAllow()){
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) {

View File

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

View File

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

View File

@ -59,6 +59,10 @@ Done-Emote = ActionComplete
#Role will be replaced with reward role on clear
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
Captcha-Reward-Role = cleared