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,6 +633,7 @@ public class Datahandler {
stanfordCoreNLP.annotate(pipelineCoreDcoument); stanfordCoreNLP.annotate(pipelineCoreDcoument);
stanfordCoreNLPSentiment.annotate(pipelineCoreDcoumentLastMsg); stanfordCoreNLPSentiment.annotate(pipelineCoreDcoumentLastMsg);
String regex = "(.*?\\d){10,}"; String regex = "(.*?\\d){10,}";
if (pipelineCoreDcoument.entityMentions() != null) {
for (CoreEntityMention em : pipelineCoreDcoument.entityMentions()) { for (CoreEntityMention em : pipelineCoreDcoument.entityMentions()) {
String entityType = em.entityType(); String entityType = em.entityType();
if (entityType == "PERSON") { if (entityType == "PERSON") {
@ -642,17 +643,20 @@ public class Datahandler {
Matcher matcher = pattern.matcher(personName); Matcher matcher = pattern.matcher(personName);
boolean isMatched = matcher.matches(); boolean isMatched = matcher.matches();
if (emText != personName && !isMatched) { if (emText != personName && !isMatched) {
if (pipelineCoreDcoumentLastMsg.entityMentions() != null) {
for (CoreEntityMention emLastMsg : pipelineCoreDcoumentLastMsg.entityMentions()) { for (CoreEntityMention emLastMsg : pipelineCoreDcoumentLastMsg.entityMentions()) {
if (emText != emLastMsg.text() && !Character.isDigit(Integer.parseInt(emLastMsg.text().trim()))) { if (emText != emLastMsg.text() && !Character.isDigit(Integer.parseInt(emLastMsg.text().trim()))) {
str = (responseMsg.substring(0, responseMsg.indexOf(emText)) + " " str = (responseMsg.substring(0, responseMsg.indexOf(emText)) + " "
+ emLastMsg + " " + responseMsg.substring(responseMsg.indexOf(emText))); + emLastMsg + " " + responseMsg.substring(responseMsg.indexOf(emText)));
} }
} }
}
str += personName; str += personName;
return str; return str;
} }
} }
} }
}
} catch (Exception e) { } catch (Exception e) {
System.out.println("SCUFFED JAYZ: " + e.getMessage()); System.out.println("SCUFFED JAYZ: " + e.getMessage());
} }

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,33 +117,26 @@ 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();
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; boolean channelpermissionsDenied = false;
if (channelName.contains("suggestion-box")) { if (channelName.contains("suggestion-box")) {
channelpermissionsDenied = true; channelpermissionsDenied = true;
} }
switch (name) { String categoryName = event.getMessage().getCategory().getName().toLowerCase();
switch (categoryName) {
case "public area": case "public area":
case "information area": { case "information area": {
break; break;
@ -144,11 +147,10 @@ public class DiscordHandler {
} }
} }
if (!channelpermissionsDenied) { if (!channelpermissionsDenied) {
//System.out.println("past !channelpermissionsDenied");
boolean mentionedBot = false; boolean mentionedBot = false;
if (blockLast != null) { if (mentionedMembers != null) {
for (User user : blockLast) { for (Member member : mentionedMembers) {
if (user.getUsername().equals(usernameBot)) { if (member.getEffectiveName().equals(event.getJDA().getSelfUser().getName())) {
mentionedBot = true; mentionedBot = true;
break; break;
} }
@ -160,19 +162,13 @@ public class DiscordHandler {
false); false);
if (!ResponseStr.isEmpty()) { if (!ResponseStr.isEmpty()) {
System.out.print("\nResponseStr3: " + ResponseStr + "\n"); System.out.print("\nResponseStr3: " + ResponseStr + "\n");
event.getMessage().getChannel().flatMap(chan -> chan.createMessage(ResponseStr)).block(); event.getMessage().getChannel().sendMessage(ResponseStr).queue();
//event.getMessage().getChannel().block().createMessage(ResponseStr).block();
//System.out.print("past event.getMessage().getChannel()");
} }
} else { } else {
//System.out.println("pre else statement trimString");
String strF = datahandler.trimString(content); String strF = datahandler.trimString(content);
//System.out.println("post else statement trimString. strf: " + strF);
datahandler.getResponseFutures(strF, stanfordCoreNLP, stanfordCoreNLPSentiment); datahandler.getResponseFutures(strF, stanfordCoreNLP, stanfordCoreNLPSentiment);
} }
} }
} }
});
gateway.onDisconnect().block();
} }
} }