Skip to content
This repository has been archived by the owner on Jun 19, 2021. It is now read-only.

Commit

Permalink
Merge pull request #10 from Shawiizz/master
Browse files Browse the repository at this point in the history
Fixe code pos && added a feature to send captcha if user enable DMs.
  • Loading branch information
duplexsystem authored Feb 15, 2021
2 parents e249134 + 1e79aa8 commit 049c836
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/yatopiamc/bot/YatopiaBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public Thread newThread(@NotNull Runnable r) {

public void start() throws LoginException, InterruptedException, IOException {
JDA jda =
JDABuilder.create(GatewayIntent.GUILD_MESSAGES, GatewayIntent.GUILD_EMOJIS, GatewayIntent.DIRECT_MESSAGES, GatewayIntent.GUILD_MEMBERS)
JDABuilder.create(GatewayIntent.GUILD_MESSAGES, GatewayIntent.GUILD_EMOJIS, GatewayIntent.DIRECT_MESSAGES, GatewayIntent.GUILD_MEMBERS, GatewayIntent.GUILD_MESSAGE_REACTIONS)
.setToken(token)
.setGatewayPool(executor)
.setCallbackPool(executor)
Expand Down
22 changes: 17 additions & 5 deletions src/main/java/org/yatopiamc/bot/captcha/YatoCaptcha.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.yatopiamc.bot.captcha;

import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.ReadyEvent;
import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent;
import net.dv8tion.jda.api.events.message.priv.PrivateMessageReceivedEvent;
import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -20,7 +20,6 @@
import java.io.InputStream;
import java.util.HashMap;
import java.util.Objects;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

public class YatoCaptcha extends ListenerAdapter {
Expand All @@ -38,9 +37,9 @@ private static InputStream generateImage(User u) {
Graphics2D g = (Graphics2D) image.getGraphics();
g.setFont(Font.createFont(Font.TRUETYPE_FONT, Objects.requireNonNull(ClassLoader.getSystemClassLoader().getResourceAsStream("LoveBaby.ttf"))).deriveFont(40f));
AffineTransform at = new AffineTransform();
at.setToRotation(ThreadLocalRandom.current().nextInt(0, 360));
at.setToRotation(Math.random());
g.setTransform(at);
g.drawString(code, 50, 50);
g.drawString(code, 130, 130);
g.dispose();

ByteArrayOutputStream os = new ByteArrayOutputStream();
Expand All @@ -56,7 +55,20 @@ private static InputStream generateImage(User u) {

public void onGuildMemberJoin(GuildMemberJoinEvent e) {
if(e.getGuild().getId().equals("743126646617407649" /*GUILD ID*/))
e.getUser().openPrivateChannel().flatMap(privateChannel -> privateChannel.sendMessage("Hello, please send me the code on this image so I can verify you are not a robot.").addFile(generateImage(e.getUser()), "yatocatpcha.png").onErrorFlatMap(throwable -> helpChannel.sendMessage("Hello " + e.getMember().getAsMention() + ", I need you to open your DMs so that I can send you a captcha."))).queue();
sendCaptcha(e.getUser());
}

private static void sendCaptcha(User u) {
u.openPrivateChannel().flatMap(privateChannel -> privateChannel.sendMessage("Hello, please send me the code on this image so I can verify you are not a robot.").addFile(generateImage(u), "yatocatpcha.png").onErrorFlatMap(throwable -> helpChannel.sendMessage("Hello " + u.getAsMention() + ", I need that you open your DMs so that I can send you a captcha.\n When you did it, add a reaction to this message and I'll send you a captcha verification."))).queue();
}

public void onMessageReactionAdd(MessageReactionAddEvent e) {
e.getChannel().retrieveMessageById(e.getMessageId()).queue(message -> {
if(message.getAuthor().getId().equals("806584703687065632") && message.getContentRaw().contains("I need that you open your DMs") && message.getMentionedMembers().contains(e.getMember())) {
sendCaptcha(Objects.requireNonNull(e.getUser()));
message.delete().queue();
}
});
}

public void onPrivateMessageReceived(PrivateMessageReceivedEvent e) {
Expand Down

0 comments on commit 049c836

Please sign in to comment.