channel message thing getter listener thing

This commit is contained in:
nora 2021-01-20 22:05:39 +01:00
parent 93f665c59a
commit 381081fc81
14 changed files with 332 additions and 14 deletions

View file

@ -0,0 +1,44 @@
package com.github.nilstrieb.commands.fun;
import com.github.nilstrieb.commands.handler.Command;
import com.github.nilstrieb.sections.ChannelMessageEventManager;
import com.github.nilstrieb.sections.Section;
import com.github.nilstrieb.util.trivia.TriviaQuestionData;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
public class TriviaCommand extends Command {
public TriviaCommand() {
super("trivia", "Answer random Trivia questions!", "trivia 0", "(maximal arc (inclusive) as a number)", """
Answer random trivia questions by the community!
You can choose the last arc the questions will be from to avoid spoilers
Arcs:
0 Hunter Exam arc
1 Zoldyck Family arc
2 Heavens Arena arc
3 Yorknew City arc
4 Greed Island arc
5 Chimera Ant arc
6 Election arc""
""");
}
@Override
public void called(MessageReceivedEvent event, String args) {
int arc = 6;
try {
arc = Integer.parseInt(args);
} catch (NumberFormatException ignored) {
}
reply(event, TriviaQuestionData.getQuestion(arc).getQuestion());
new Section(event.getTextChannel().getIdLong(), 414755070161453076L) {
@Override
public void messageReceived(MessageReceivedEvent event) {
event.getTextChannel().sendMessage("hallo ich bin killua").queue();
ChannelMessageEventManager.removeListener(this);
}
};
}
}

View file

@ -13,24 +13,66 @@ public abstract class Command {
private final String description;
private final String exampleUsage;
private final String arguments;
private final String detailDescription;
private final CommandParser parser = CommandParser.getInstance();
public Command(String name, String description, String exampleUsage, String arguments, boolean hidden) {
/**
* New command
* @param name command name
* @param description quick description
* @param exampleUsage example usage without the prefix
* @param arguments all arguments (() -> optional. <> -> required
* @param detailDescription a detailed description
*/
public Command(String name, String description, String exampleUsage, String arguments, String detailDescription) {
this.name = name;
this.description = description;
this.exampleUsage = exampleUsage;
this.arguments = arguments;
this.detailDescription = detailDescription;
CommandHandler.addCommand(name, this, false);
}
/**
* New command
* @param name command name
* @param description quick description
* @param exampleUsage example usage without the prefix
* @param arguments all arguments (() -> optional. <> -> required
*/
public Command(String name, String description, String exampleUsage, String arguments){
this(name, description, exampleUsage, arguments, "");
}
/**
* New command
* @param name command name
* @param description quick description
*/
public Command(String name, String description) {
this(name, description, name, "", "");
}
/**
* Hidden command
* @param name name
* @param hidden should always be true
*/
public Command(String name, boolean hidden) {
this.name = name;
this.description = "";
this.exampleUsage = "";
this.arguments = "";
this.detailDescription = "";
CommandHandler.addCommand(name, this, hidden);
}
public Command(String name, String description, String exampleUsage, String arguments) {
this(name, description, exampleUsage, arguments, false);
}
public Command(String name, String description) {
this(name, description, name, "", false);
}
/**
* The method called by the CommandHandler
* @param event The event
* @param args The arguments (after the command and an optional whitespace)
*/
public abstract void called(MessageReceivedEvent event, String args);
protected void reply(MessageReceivedEvent event, String message) {
@ -89,4 +131,8 @@ public abstract class Command {
public String getArguments() {
return arguments;
}
public String getDetailDescription() {
return detailDescription;
}
}

View file

@ -90,7 +90,8 @@ public class CommandHandler {
.setTitle("Killua help: " + cmd.getName())
.addField("Name", cmd.getName(), true)
.addField("Description", cmd.getDescription(), true)
.addField("Example usage", "`" + cmd.getExampleUsage() + "`", true);
.addField("Example usage", "`" + cmd.getExampleUsage() + "`", true)
.addField("Detail:", cmd.getDetailDescription(), true);
if (!cmd.getArguments().equals("")) {
builder.addField("Arguments", "`" + cmd.getArguments() + "`", true);

View file

@ -5,7 +5,7 @@ import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
public class EvalCommand extends Command {
public EvalCommand() {
super("eval", "no", "", "<command>", true);
super("eval", true);
}
@Override

View file

@ -4,10 +4,12 @@ import com.github.nilstrieb.cofig.Secrets;
import com.github.nilstrieb.commands.fun.DepartureCommand;
import com.github.nilstrieb.commands.fun.QuoteCommand;
import com.github.nilstrieb.commands.fun.SayCommand;
import com.github.nilstrieb.commands.fun.TriviaCommand;
import com.github.nilstrieb.commands.info.InviteCommand;
import com.github.nilstrieb.commands.info.EvalCommand;
import com.github.nilstrieb.commands.info.HelpCommand;
import com.github.nilstrieb.commands.info.ToukaCommand;
import com.github.nilstrieb.listener.ChannelMessageListener;
import com.github.nilstrieb.listener.CommandListener;
import com.github.nilstrieb.listener.ReactionEventListener;
import com.github.nilstrieb.listener.StartUpListener;
@ -25,7 +27,7 @@ public class Main {
builder.setCompression(Compression.ZLIB);
builder.setActivity(Activity.watching("over Gon"));
builder.addEventListeners(new StartUpListener(), new CommandListener(), new ReactionEventListener());
builder.addEventListeners(new StartUpListener(), new ChannelMessageListener(), new CommandListener(), new ReactionEventListener());
JDA jda = builder.build();
setupCommands();
@ -39,5 +41,6 @@ public class Main {
new InviteCommand();
new QuoteCommand();
new DepartureCommand();
new TriviaCommand();
}
}

View file

@ -0,0 +1,15 @@
package com.github.nilstrieb.listener;
import com.github.nilstrieb.sections.ChannelMessageEventManager;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull;
public class ChannelMessageListener extends ListenerAdapter {
@Override
public void onMessageReceived(@NotNull MessageReceivedEvent event) {
if (!event.getAuthor().isBot()) {
ChannelMessageEventManager.onMessageReceived(event);
}
}
}

View file

@ -9,8 +9,8 @@ import org.jetbrains.annotations.NotNull;
public class CommandListener extends ListenerAdapter {
@Override
public void onMessageReceived(@NotNull MessageReceivedEvent event) {
System.out.println(ConsoleColors.CYAN + "[CListener] Received message: '" + event.getMessage().getContentRaw() + "'" + ConsoleColors.RESET);
if (!event.getAuthor().isBot()) {
System.out.println(ConsoleColors.CYAN + "[CListener] Received message: '" + event.getMessage().getContentRaw() + "'" + ConsoleColors.RESET);
CommandHandler.call(event);
}
}

View file

@ -1,5 +1,6 @@
package com.github.nilstrieb.listener;
import com.github.nilstrieb.util.ConsoleColors;
import net.dv8tion.jda.api.events.ReadyEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull;
@ -8,6 +9,6 @@ public class StartUpListener extends ListenerAdapter {
@Override
public void onReady(@NotNull ReadyEvent event) {
System.out.println("[Startup] Killua started");
System.out.println(ConsoleColors.BLACK_BACKGROUND + ConsoleColors.YELLOW_BOLD_BRIGHT + "[Startup] Killua started" + ConsoleColors.RESET);
}
}

View file

@ -0,0 +1,9 @@
package com.github.nilstrieb.sections;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
public interface ChannelListener {
void messageReceived(MessageReceivedEvent event);
long getUserID();
long getChannelID();
}

View file

@ -0,0 +1,55 @@
package com.github.nilstrieb.sections;
import com.github.nilstrieb.util.ConsoleColors;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import java.util.*;
public class ChannelMessageEventManager {
private static HashMap<Long, List<ChannelListener>> listeners = new HashMap<>();
private static List<ChannelListener> removeBuffer = new ArrayList<>();
private static Set<Long> removedChannels = new HashSet<>();
public static void addListener(ChannelListener listener, long channel) {
if (!listeners.containsKey(channel)) {
listeners.put(channel, new ArrayList<>());
}
listeners.get(channel).add(listener);
}
public static void removeListener(ChannelListener listener) {
if (listeners.containsKey(listener.getChannelID())) {
removeBuffer.add(listener);
}
}
public static void onMessageReceived(MessageReceivedEvent event) {
long id = event.getTextChannel().getIdLong();
if (listeners.containsKey(id)) {
System.out.println(ConsoleColors.YELLOW + "[ChannelMsgEvtMgr] Message in listened channel " +
event.getTextChannel().getName() + " by " + event.getAuthor().getAsTag() + ": " + event.getMessage().getContentRaw() + ConsoleColors.RESET);
List<ChannelListener> list = listeners.get(id);
for (ChannelListener channelListener : list) {
if (channelListener.getUserID() == 0) {
channelListener.messageReceived(event);
} else if (channelListener.getUserID() == event.getAuthor().getIdLong()) {
channelListener.messageReceived(event);
}
}
for (ChannelListener channelListener : removeBuffer) {
listeners.get(channelListener.getChannelID()).remove(channelListener);
removedChannels.add(channelListener.getChannelID());
}
for (Long removedChannel : removedChannels) {
list = listeners.get(removedChannel);
if (list.isEmpty()) {
listeners.remove(removedChannel);
}
}
removedChannels.clear();
removeBuffer.clear();
}
}
}

View file

@ -0,0 +1,34 @@
package com.github.nilstrieb.sections;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import java.util.HashMap;
public abstract class Section implements ChannelListener{
private long textChannelID;
private long userID;
private boolean multiUser;
public Section(long textChannelID, long userID) {
this.textChannelID = textChannelID;
this.userID = userID;
this.multiUser = false;
ChannelMessageEventManager.addListener(this, textChannelID);
}
public Section(long textChannelID) {
this.textChannelID = textChannelID;
this.multiUser = true;
}
@Override
public long getUserID() {
return userID;
}
@Override
public long getChannelID() {
return textChannelID;
}
}

View file

@ -0,0 +1,15 @@
package com.github.nilstrieb.util.trivia;
public enum Arc {
EXAM(0), ZOLDYCK_FAMILY(1), HEAVENS_ARENA(2), YORKNEW_CITY(3),
GREED_ISLAND(4), CHIMERA_ANT(5), ELECTION(6);
private final int number;
Arc(int number) {
this.number = number;
}
public int getNumber() {
return number;
}
}

View file

@ -0,0 +1,31 @@
package com.github.nilstrieb.util.trivia;
public class TriviaQuestion {
private String question;
private String[] answers;
private int correctAnswer;
private Arc arc;
public TriviaQuestion(String question, int correctAnswer, Arc arc, String ... answers) {
this.question = question;
this.answers = answers;
this.arc = arc;
this.correctAnswer = correctAnswer;
}
public String getQuestion() {
return question;
}
public String[] getAnswers() {
return answers;
}
public int getCorrectAnswer() {
return correctAnswer;
}
public Arc getArc() {
return arc;
}
}

View file

@ -0,0 +1,64 @@
package com.github.nilstrieb.util.trivia;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class TriviaQuestionData {
static List<List<TriviaQuestion>> questions = new ArrayList<>();
private static final Random random = new Random();
static {
questions.add(new ArrayList<>());
questions.add(new ArrayList<>());
questions.add(new ArrayList<>());
questions.add(new ArrayList<>());
questions.add(new ArrayList<>());
questions.add(new ArrayList<>());
questions.add(new ArrayList<>());
add(new TriviaQuestion("What's the name of Gons aunt?", 1, Arc.EXAM,
"Kite", "Mito", "Ging", "Kurapika"));
add(new TriviaQuestion("What is Bisky's true form?", 1, Arc.GREED_ISLAND,
"Boy", "Big Woman", "Little Girl"));
add(new TriviaQuestion("Why does Leorio want money?", 0, Arc.EXAM,
"To become a doctor", "To send it to e-girls", "So that others don't get it", "To buy large quantities of drugs"));
add(new TriviaQuestion("Where did Meruem die?", 2, Arc.CHIMERA_ANT,
"Volcano", "In the desert with Netero", "In the basement of the palace", "In the womb of the queen"));
add(new TriviaQuestion("Who is Alluka?", 3, Arc.ELECTION,
"Gon's sister", "A Zodiac", "The 'dark' side of Nanika", "Killua's sister"));
add(new TriviaQuestion("How did Gon die on Greed Island?", 0, Arc.GREED_ISLAND,
"He didn't die on Greed Island", "Hisoka killed him with a card to the neck", "He lost too muich blood when Genthru blew his arm of", "He killed himself after Killuas death"));
add(new TriviaQuestion("How many doors are there at the Testing Gate of the Zoldyck Family?", 0, Arc.ZOLDYCK_FAMILY,
"7", "5", "12", "8"));
add(new TriviaQuestion("What was Gon's aim after Killua left him alone playing with the Chairman Netero", 1, Arc.EXAM,
"Get the ball", "Make Netero use his right hand", "Make Netero use his left leg", "Let Netero drop the ball"));
add(new TriviaQuestion("What did Tonpa mix into the drink?", 2, Arc.EXAM,
"Sleeping pills", "Headache tabletsschla", "Laxative", "Rat poison"));
}
private static void add(TriviaQuestion triviaQuestion) {
questions.get(triviaQuestion.getArc().getNumber()).add(triviaQuestion);
}
public static TriviaQuestion getQuestion(int toArc) {
int totalQuestions = 0;
for (int i = 0; i <= toArc; i++) {
totalQuestions += questions.get(i).size();
}
int randomQuestion = random.nextInt(totalQuestions);
for (int i = 0; i <= toArc; i++) {
if (randomQuestion >= questions.get(i).size()) {
randomQuestion -= questions.get(i).size();
} else {
return questions.get(i).get(randomQuestion);
}
}
throw new IndexOutOfBoundsException("No Question available for arc " + toArc);
}
}