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

Commit 049c836

Browse files
authored
Merge pull request #10 from Shawiizz/master
Fixe code pos && added a feature to send captcha if user enable DMs.
2 parents e249134 + 1e79aa8 commit 049c836

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/main/java/org/yatopiamc/bot/YatopiaBot.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public Thread newThread(@NotNull Runnable r) {
7474

7575
public void start() throws LoginException, InterruptedException, IOException {
7676
JDA jda =
77-
JDABuilder.create(GatewayIntent.GUILD_MESSAGES, GatewayIntent.GUILD_EMOJIS, GatewayIntent.DIRECT_MESSAGES, GatewayIntent.GUILD_MEMBERS)
77+
JDABuilder.create(GatewayIntent.GUILD_MESSAGES, GatewayIntent.GUILD_EMOJIS, GatewayIntent.DIRECT_MESSAGES, GatewayIntent.GUILD_MEMBERS, GatewayIntent.GUILD_MESSAGE_REACTIONS)
7878
.setToken(token)
7979
.setGatewayPool(executor)
8080
.setCallbackPool(executor)

src/main/java/org/yatopiamc/bot/captcha/YatoCaptcha.java

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package org.yatopiamc.bot.captcha;
22

3-
import net.dv8tion.jda.api.entities.Member;
43
import net.dv8tion.jda.api.entities.TextChannel;
54
import net.dv8tion.jda.api.entities.User;
65
import net.dv8tion.jda.api.events.ReadyEvent;
76
import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent;
87
import net.dv8tion.jda.api.events.message.priv.PrivateMessageReceivedEvent;
8+
import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent;
99
import net.dv8tion.jda.api.hooks.ListenerAdapter;
1010
import org.slf4j.Logger;
1111
import org.slf4j.LoggerFactory;
@@ -20,7 +20,6 @@
2020
import java.io.InputStream;
2121
import java.util.HashMap;
2222
import java.util.Objects;
23-
import java.util.Random;
2423
import java.util.concurrent.ThreadLocalRandom;
2524

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

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

5756
public void onGuildMemberJoin(GuildMemberJoinEvent e) {
5857
if(e.getGuild().getId().equals("743126646617407649" /*GUILD ID*/))
59-
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();
58+
sendCaptcha(e.getUser());
59+
}
60+
61+
private static void sendCaptcha(User u) {
62+
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();
63+
}
64+
65+
public void onMessageReactionAdd(MessageReactionAddEvent e) {
66+
e.getChannel().retrieveMessageById(e.getMessageId()).queue(message -> {
67+
if(message.getAuthor().getId().equals("806584703687065632") && message.getContentRaw().contains("I need that you open your DMs") && message.getMentionedMembers().contains(e.getMember())) {
68+
sendCaptcha(Objects.requireNonNull(e.getUser()));
69+
message.delete().queue();
70+
}
71+
});
6072
}
6173

6274
public void onPrivateMessageReceived(PrivateMessageReceivedEvent e) {

0 commit comments

Comments
 (0)