add list-data bot command

Signed-off-by: TudbuT <tudbut@tudbut.de>
Original-Message: tud you'll probably get what i mean by this commit, fix it if it's broken :)
This commit is contained in:
Jess H 2024-06-13 18:16:25 +01:00 committed by TudbuT
parent f7737de145
commit 7a834559bc
Signed by: TudbuT
GPG key ID: B3CF345217F202D3
2 changed files with 113 additions and 123 deletions

View file

@ -72,6 +72,16 @@ public class Main extends ListenerAdapter {
.setDefaultPermissions(DefaultMemberPermissions.DISABLED)
);
commands.addCommands(
Commands.slash("list-data", "List data of given user.")
.addOptions(new OptionData(USER, "user", "The user who's HWID we should reset.")
.setRequired(true))
.addOptions(new OptionData(STRING, "password", "The admin password.")
.setRequired(true))
.setGuildOnly(true)
.setDefaultPermissions(DefaultMemberPermissions.DISABLED)
);
commands.addCommands(
Commands.slash("password", "Set your password.")
.addOptions(new OptionData(STRING, "password", "Your new password.")
@ -98,51 +108,38 @@ public class Main extends ListenerAdapter {
}
System.out.println("Got event " + event.getName());
try {
switch (event.getName()) {
case "reset": {
User user = event.getOption("user").getAsUser();
String password = event.getOption("password").getAsString();
try {
TCN tcn = new TCN();
tcn.set("action", "reset");
tcn.set("authorisation", Hasher.sha512hex(password));
tcn.set("discord-id", user.getIdLong());
tos.writeString(tcn.toString());
switch (tis.readInt()) {
case 201: {
if(processReply(event, tis)) {
event.getChannel().sendMessage("Reset the HWID of **`" + user.getName() + "`**.").queue();
event.reply("`Success.`").setEphemeral(true).queue();
break;
}
case 403: {
event.reply("`Incorrect Admin Password.`").setEphemeral(true).queue();
break;
}
case "list-data": {
User user = event.getOption("user").getAsUser();
String password = event.getOption("password").getAsString();
case 400: {
event.reply("`Error! I sent malformed data somehow.`").setEphemeral(true).queue();
break;
}
TCN tcn = new TCN();
tcn.set("action", "list-data");
tcn.set("authorisation", Hasher.sha512hex(password));
tcn.set("discord-id", user.getIdLong());
tos.writeString(tcn.toString());
case 404: {
event.reply("`User Not Found, Do they have BaseBand?`").setEphemeral(true).queue();
break;
if(processReply(event, tis)) {
event.reply("Success! ```json\n" + tis.readString() + "\n```").setEphemeral(true).queue();
}
default: {
event.reply("Error! I am out of sync with the server.").setEphemeral(true).queue();
break;
}
}
} catch (IOException e) {
e.printStackTrace();
event.reply("Exception occurred pushing data to server.").setEphemeral(true).queue();
}
break;
}
case "branch": {
@ -150,8 +147,6 @@ public class Main extends ListenerAdapter {
String branch = event.getOption("branch").getAsString();
String password = event.getOption("password").getAsString();
try {
TCN tcn = new TCN();
tcn.set("action", "branch");
tcn.set("authorisation", Hasher.sha512hex(password));
@ -159,36 +154,9 @@ public class Main extends ListenerAdapter {
tcn.set("branch", branch);
tos.writeString(tcn.toString());
switch (tis.readInt()) {
case 201: {
if (processReply(event, tis)) {
event.getChannel().sendMessage("Branch of **`" + user.getName() + "`** has been set.").queue();
event.reply("`Success.`").setEphemeral(true).queue();
break;
}
case 403: {
event.reply("`Incorrect Admin Password.`").setEphemeral(true).queue();
break;
}
case 400: {
event.reply("`Error! I sent malformed data somehow.`").setEphemeral(true).queue();
break;
}
case 404: {
event.reply("`User Not Found, Do they have BaseBand?`").setEphemeral(true).queue();
break;
}
default: {
event.reply("Error! I am out of sync with the server.").setEphemeral(true).queue();
break;
}
}
} catch (IOException e) {
e.printStackTrace();
event.reply("Exception occurred pushing data to server.").setEphemeral(true).queue();
}
break;
@ -198,44 +166,16 @@ public class Main extends ListenerAdapter {
String password = event.getOption("password").getAsString();
String username = event.getOption("username").getAsString();
try {
TCN tcn = new TCN();
tcn.set("action", "add");
tcn.set("username", username); //wow yes don't give the username to the server-side great job
tcn.set("username", username);
tcn.set("authorisation", Hasher.sha512hex(password));
tcn.set("discord-id", user.getIdLong());
tos.writeString(tcn.toString());
switch (tis.readInt()) {
case 201: {
if(processReply(event, tis)) {
event.getChannel().sendMessage("Created user with username **`" + username + "`**. (`" + user.getName() + "`)").queue();
event.reply("`Success.`").setEphemeral(true).queue();
break;
}
case 403: {
event.reply("`Incorrect Admin Password.`").setEphemeral(true).queue();
break;
}
case 409: {
event.reply("`User already exists!`").setEphemeral(true).queue();
break;
}
case 400: {
event.reply("`Error! I sent malformed data somehow.`").setEphemeral(true).queue();
break;
}
default: {
event.reply("`Error! I am out of sync with the server.`").setEphemeral(true).queue();
break;
}
}
} catch (IOException e) {
e.printStackTrace();
event.reply("Exception occurred pushing data to server.").setEphemeral(true).queue();
}
break;
@ -244,7 +184,6 @@ public class Main extends ListenerAdapter {
User user = event.getUser();
String password = event.getOption("password").getAsString();
try {
TCN tcn = new TCN();
tcn.set("action", "password");
tcn.set("authorisation", "1ee7a143df6e0d2f3d2b86b3e5c098c06a07d7c8eb01d629f51712b6bba3a468dc96ef9729d586007ca71383b1c203f6f996bdce3972772d0e5351364eba0d1e");
@ -278,15 +217,49 @@ public class Main extends ListenerAdapter {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
event.reply("Exception occurred pushing data to server.").setEphemeral(true).queue();
}
break;
}
default:
event.reply("I can't handle that command right now :(").setEphemeral(true).queue();
}
} catch (IOException e) {
e.printStackTrace();
event.reply("Exception occurred pushing data to server.").setEphemeral(true).queue();
}
}
private static boolean processReply(@NotNull SlashCommandInteractionEvent event, TypedInputStream tis) throws IOException {
switch (tis.readInt()) {
case 201: {
return true;
}
case 403: {
event.reply("Incorrect Admin Password.").setEphemeral(true).queue();
break;
}
case 400: {
event.reply("Error! I sent malformed data somehow.").setEphemeral(true).queue();
break;
}
case 409: {
event.reply("Already exists!").setEphemeral(true).queue();
break;
}
case 404: {
event.reply("User Not Found, Do they have BaseBand?").setEphemeral(true).queue();
break;
}
default: {
event.reply("Error! I am out of sync with the server.").setEphemeral(true).queue();
break;
}
}
return false;
}
}

View file

@ -3,6 +3,7 @@ package de.com.baseband.server;
import at.favre.lib.crypto.bcrypt.BCrypt;
import de.tudbut.io.TypedInputStream;
import de.tudbut.io.TypedOutputStream;
import de.tudbut.parsing.JSON;
import de.tudbut.parsing.TCN;
import java.io.IOException;
@ -59,6 +60,22 @@ public class BotServiceHandler {
break;
}
case "list-data": {
TCN user = (TCN) UserHandler.users.stream()
.filter(
a -> ((TCN) a).getLong("discord-id").equals(tcn.getLong("discord-id"))
).findFirst().orElse(null);
if(user != null) {
tos.writeInt(201);
tos.writeString(JSON.writeReadable(user, 4));
} else {
tos.writeInt(404);
}
break;
}
case "branch": {
TCN user = (TCN) UserHandler.users.stream()
.filter(