diff --git a/ArtificialAutism/src/main/java/FunctionLayer/Datahandler.java b/ArtificialAutism/src/main/java/FunctionLayer/Datahandler.java index d6bcfb71..596d2141 100644 --- a/ArtificialAutism/src/main/java/FunctionLayer/Datahandler.java +++ b/ArtificialAutism/src/main/java/FunctionLayer/Datahandler.java @@ -633,23 +633,27 @@ public class Datahandler { stanfordCoreNLP.annotate(pipelineCoreDcoument); stanfordCoreNLPSentiment.annotate(pipelineCoreDcoumentLastMsg); String regex = "(.*?\\d){10,}"; - for (CoreEntityMention em : pipelineCoreDcoument.entityMentions()) { - String entityType = em.entityType(); - if (entityType == "PERSON") { - String str = responseMsg; - String emText = em.text(); - Pattern pattern = Pattern.compile(regex); - Matcher matcher = pattern.matcher(personName); - boolean isMatched = matcher.matches(); - if (emText != personName && !isMatched) { - for (CoreEntityMention emLastMsg : pipelineCoreDcoumentLastMsg.entityMentions()) { - if (emText != emLastMsg.text() && !Character.isDigit(Integer.parseInt(emLastMsg.text().trim()))) { - str = (responseMsg.substring(0, responseMsg.indexOf(emText)) + " " - + emLastMsg + " " + responseMsg.substring(responseMsg.indexOf(emText))); + if (pipelineCoreDcoument.entityMentions() != null) { + for (CoreEntityMention em : pipelineCoreDcoument.entityMentions()) { + String entityType = em.entityType(); + if (entityType == "PERSON") { + String str = responseMsg; + String emText = em.text(); + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(personName); + boolean isMatched = matcher.matches(); + if (emText != personName && !isMatched) { + if (pipelineCoreDcoumentLastMsg.entityMentions() != null) { + for (CoreEntityMention emLastMsg : pipelineCoreDcoumentLastMsg.entityMentions()) { + if (emText != emLastMsg.text() && !Character.isDigit(Integer.parseInt(emLastMsg.text().trim()))) { + str = (responseMsg.substring(0, responseMsg.indexOf(emText)) + " " + + emLastMsg + " " + responseMsg.substring(responseMsg.indexOf(emText))); + } + } } + str += personName; + return str; } - str += personName; - return str; } } } diff --git a/ArtificialAutism/src/main/java/PresentationLayer/DiscordHandler.java b/ArtificialAutism/src/main/java/PresentationLayer/DiscordHandler.java index 71265ee5..33838de7 100644 --- a/ArtificialAutism/src/main/java/PresentationLayer/DiscordHandler.java +++ b/ArtificialAutism/src/main/java/PresentationLayer/DiscordHandler.java @@ -3,14 +3,17 @@ package PresentationLayer; import DataLayer.settings; import FunctionLayer.Datahandler; import FunctionLayer.PipelineJMWESingleton; -import discord4j.core.DiscordClient; -import discord4j.core.GatewayDiscordClient; -import discord4j.core.event.domain.message.MessageCreateEvent; -import discord4j.core.object.entity.Sticker; -import discord4j.core.object.entity.User; -import discord4j.core.object.entity.channel.TextChannel; import edu.stanford.nlp.pipeline.StanfordCoreNLP; +import net.dv8tion.jda.api.JDABuilder; +import net.dv8tion.jda.api.entities.Activity; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.Message; +import net.dv8tion.jda.api.entities.MessageChannel; +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; +import net.dv8tion.jda.api.requests.GatewayIntent; +import javax.security.auth.login.LoginException; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.*; @@ -22,7 +25,7 @@ import java.util.List; /** * @author install1 */ -public class DiscordHandler { +public class DiscordHandler extends ListenerAdapter { private static void receiveAndSendPacket(DatagramSocket serverSocket, InetAddress ipAddress, int port, Datahandler datahandler, StanfordCoreNLP stanfordCoreNLP, StanfordCoreNLP stanfordCoreNLPSentiment) throws IOException { @@ -80,20 +83,27 @@ public class DiscordHandler { } } + + private static StanfordCoreNLP stanfordCoreNLP; + private static Datahandler datahandler; + private static StanfordCoreNLP stanfordCoreNLPSentiment; + //TODO add python program that edits the java code. python program just adds test if statements on //variables until the tests pass - public static void main(String[] args) throws IOException, SQLException { - Datahandler datahandler = new Datahandler(); - + public static void main(String[] args) throws LoginException { + datahandler = new Datahandler(); PipelineJMWESingleton.getINSTANCE(); - StanfordCoreNLP stanfordCoreNLP = datahandler.pipeLineSetUp(); - StanfordCoreNLP stanfordCoreNLPSentiment = datahandler.shiftReduceParserInitiate(); + stanfordCoreNLP = datahandler.pipeLineSetUp(); + stanfordCoreNLPSentiment = datahandler.shiftReduceParserInitiate(); + System.out.println("FINISHED ALL ANNOTATIONS"); System.out.println("updatedstring cache"); String token = new settings().getDiscordToken(); - final DiscordClient client = DiscordClient.create(token); - final GatewayDiscordClient gateway = client.login().block(); - String usernameBot = gateway.getSelf().block().getUsername(); + JDABuilder.createLight(token, GatewayIntent.GUILD_MESSAGES, GatewayIntent.DIRECT_MESSAGES) + .addEventListeners(new DiscordHandler()) + .setActivity(Activity.playing("Being the autism bot")) + .build(); + int autismbotCount = 4; //make sure not to use ports that are already occupied. for (int i = 0; i < autismbotCount; i++) { @@ -107,72 +117,58 @@ public class DiscordHandler { handleUDPTraffic(ports.get(j), datahandler, stanfordCoreNLP, stanfordCoreNLPSentiment); }).start(); } - gateway.on(MessageCreateEvent.class).subscribe(event -> { - String username = ""; - try { - username = event.getMessage().getAuthor().get().getUsername(); - } catch (java.util.NoSuchElementException e) { - username = null; - } - //System.out.println("past event.getMessage().getAuthor().get().getUsername()"); - List blockLast = event.getMessage().getUserMentions(); - String content = event.getMessage().getContent(); - if (blockLast != null) { - for (User user : blockLast) { - content = content.replace(user.getId().asString(), ""); - } - } - //System.out.println("past event.getMessage().getContent()"); + } - if (username != null && !username.equals(usernameBot) && !content.isEmpty()) { - TextChannel block = event.getMessage().getChannel().cast(TextChannel.class).block(); - String name = block.getCategory().block().getName(); - name = name.toLowerCase(); - String channelName = block.getName().toLowerCase(); - boolean channelpermissionsDenied = false; - if (channelName.contains("suggestion-box")) { + @Override + public void onMessageReceived(MessageReceivedEvent event) { + String content = event.getMessage().getContentRaw(); + String username = event.getMessage().getAuthor().getName(); + List mentionedMembers = event.getMessage().getMentionedMembers(); + if (mentionedMembers != null) { + for (Member member : mentionedMembers) { + content = content.replace(member.getId(), ""); + } + } + if (username != null && !event.getAuthor().isBot() && !content.isEmpty()) { + String channelName = event.getMessage().getChannel().getName().toLowerCase(); + boolean channelpermissionsDenied = false; + if (channelName.contains("suggestion-box")) { + channelpermissionsDenied = true; + } + String categoryName = event.getMessage().getCategory().getName().toLowerCase(); + switch (categoryName) { + case "public area": + case "information area": { + break; + } + default: { channelpermissionsDenied = true; - } - switch (name) { - case "public area": - case "information area": { - break; - } - default: { - channelpermissionsDenied = true; - break; - } - } - if (!channelpermissionsDenied) { - //System.out.println("past !channelpermissionsDenied"); - boolean mentionedBot = false; - if (blockLast != null) { - for (User user : blockLast) { - if (user.getUsername().equals(usernameBot)) { - mentionedBot = true; - break; - } - } - } - if (mentionedBot || channelName.contains("general-autism")) { - String ResponseStr; - ResponseStr = datahandler.getResponseMsg(content, username, stanfordCoreNLP, stanfordCoreNLPSentiment, - false); - if (!ResponseStr.isEmpty()) { - System.out.print("\nResponseStr3: " + ResponseStr + "\n"); - event.getMessage().getChannel().flatMap(chan -> chan.createMessage(ResponseStr)).block(); - //event.getMessage().getChannel().block().createMessage(ResponseStr).block(); - //System.out.print("past event.getMessage().getChannel()"); - } - } else { - //System.out.println("pre else statement trimString"); - String strF = datahandler.trimString(content); - //System.out.println("post else statement trimString. strf: " + strF); - datahandler.getResponseFutures(strF, stanfordCoreNLP, stanfordCoreNLPSentiment); - } + break; } } - }); - gateway.onDisconnect().block(); + if (!channelpermissionsDenied) { + boolean mentionedBot = false; + if (mentionedMembers != null) { + for (Member member : mentionedMembers) { + if (member.getEffectiveName().equals(event.getJDA().getSelfUser().getName())) { + mentionedBot = true; + break; + } + } + } + if (mentionedBot || channelName.contains("general-autism")) { + String ResponseStr; + ResponseStr = datahandler.getResponseMsg(content, username, stanfordCoreNLP, stanfordCoreNLPSentiment, + false); + if (!ResponseStr.isEmpty()) { + System.out.print("\nResponseStr3: " + ResponseStr + "\n"); + event.getMessage().getChannel().sendMessage(ResponseStr).queue(); + } + } else { + String strF = datahandler.trimString(content); + datahandler.getResponseFutures(strF, stanfordCoreNLP, stanfordCoreNLPSentiment); + } + } + } } }