changed to different discord API

This commit is contained in:
christian 2021-12-05 13:33:53 +01:00
parent 7eba2a4ef6
commit 2f35d04d30
2 changed files with 93 additions and 93 deletions

View File

@ -633,23 +633,27 @@ public class Datahandler {
stanfordCoreNLP.annotate(pipelineCoreDcoument); stanfordCoreNLP.annotate(pipelineCoreDcoument);
stanfordCoreNLPSentiment.annotate(pipelineCoreDcoumentLastMsg); stanfordCoreNLPSentiment.annotate(pipelineCoreDcoumentLastMsg);
String regex = "(.*?\\d){10,}"; String regex = "(.*?\\d){10,}";
for (CoreEntityMention em : pipelineCoreDcoument.entityMentions()) { if (pipelineCoreDcoument.entityMentions() != null) {
String entityType = em.entityType(); for (CoreEntityMention em : pipelineCoreDcoument.entityMentions()) {
if (entityType == "PERSON") { String entityType = em.entityType();
String str = responseMsg; if (entityType == "PERSON") {
String emText = em.text(); String str = responseMsg;
Pattern pattern = Pattern.compile(regex); String emText = em.text();
Matcher matcher = pattern.matcher(personName); Pattern pattern = Pattern.compile(regex);
boolean isMatched = matcher.matches(); Matcher matcher = pattern.matcher(personName);
if (emText != personName && !isMatched) { boolean isMatched = matcher.matches();
for (CoreEntityMention emLastMsg : pipelineCoreDcoumentLastMsg.entityMentions()) { if (emText != personName && !isMatched) {
if (emText != emLastMsg.text() && !Character.isDigit(Integer.parseInt(emLastMsg.text().trim()))) { if (pipelineCoreDcoumentLastMsg.entityMentions() != null) {
str = (responseMsg.substring(0, responseMsg.indexOf(emText)) + " " for (CoreEntityMention emLastMsg : pipelineCoreDcoumentLastMsg.entityMentions()) {
+ emLastMsg + " " + responseMsg.substring(responseMsg.indexOf(emText))); 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;
} }
} }
} }

View File

@ -3,14 +3,17 @@ package PresentationLayer;
import DataLayer.settings; import DataLayer.settings;
import FunctionLayer.Datahandler; import FunctionLayer.Datahandler;
import FunctionLayer.PipelineJMWESingleton; 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 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.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.*; import java.net.*;
@ -22,7 +25,7 @@ import java.util.List;
/** /**
* @author install1 * @author install1
*/ */
public class DiscordHandler { public class DiscordHandler extends ListenerAdapter {
private static void receiveAndSendPacket(DatagramSocket serverSocket, InetAddress ipAddress, int port, private static void receiveAndSendPacket(DatagramSocket serverSocket, InetAddress ipAddress, int port,
Datahandler datahandler, StanfordCoreNLP stanfordCoreNLP, StanfordCoreNLP stanfordCoreNLPSentiment) throws IOException { 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 //TODO add python program that edits the java code. python program just adds test if statements on
//variables until the tests pass //variables until the tests pass
public static void main(String[] args) throws IOException, SQLException { public static void main(String[] args) throws LoginException {
Datahandler datahandler = new Datahandler(); datahandler = new Datahandler();
PipelineJMWESingleton.getINSTANCE(); PipelineJMWESingleton.getINSTANCE();
StanfordCoreNLP stanfordCoreNLP = datahandler.pipeLineSetUp(); stanfordCoreNLP = datahandler.pipeLineSetUp();
StanfordCoreNLP stanfordCoreNLPSentiment = datahandler.shiftReduceParserInitiate(); stanfordCoreNLPSentiment = datahandler.shiftReduceParserInitiate();
System.out.println("FINISHED ALL ANNOTATIONS"); System.out.println("FINISHED ALL ANNOTATIONS");
System.out.println("updatedstring cache"); System.out.println("updatedstring cache");
String token = new settings().getDiscordToken(); String token = new settings().getDiscordToken();
final DiscordClient client = DiscordClient.create(token); JDABuilder.createLight(token, GatewayIntent.GUILD_MESSAGES, GatewayIntent.DIRECT_MESSAGES)
final GatewayDiscordClient gateway = client.login().block(); .addEventListeners(new DiscordHandler())
String usernameBot = gateway.getSelf().block().getUsername(); .setActivity(Activity.playing("Being the autism bot"))
.build();
int autismbotCount = 4; int autismbotCount = 4;
//make sure not to use ports that are already occupied. //make sure not to use ports that are already occupied.
for (int i = 0; i < autismbotCount; i++) { for (int i = 0; i < autismbotCount; i++) {
@ -107,72 +117,58 @@ public class DiscordHandler {
handleUDPTraffic(ports.get(j), datahandler, stanfordCoreNLP, stanfordCoreNLPSentiment); handleUDPTraffic(ports.get(j), datahandler, stanfordCoreNLP, stanfordCoreNLPSentiment);
}).start(); }).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<User> 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()) { @Override
TextChannel block = event.getMessage().getChannel().cast(TextChannel.class).block(); public void onMessageReceived(MessageReceivedEvent event) {
String name = block.getCategory().block().getName(); String content = event.getMessage().getContentRaw();
name = name.toLowerCase(); String username = event.getMessage().getAuthor().getName();
String channelName = block.getName().toLowerCase(); List<Member> mentionedMembers = event.getMessage().getMentionedMembers();
boolean channelpermissionsDenied = false; if (mentionedMembers != null) {
if (channelName.contains("suggestion-box")) { 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; channelpermissionsDenied = true;
} break;
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);
}
} }
} }
}); if (!channelpermissionsDenied) {
gateway.onDisconnect().block(); 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);
}
}
}
} }
} }