From 43163274073b0184f32c73cd2fda67b6fe2f7376 Mon Sep 17 00:00:00 2001 From: Koala <76986040+Koala3353@users.noreply.github.com> Date: Sun, 18 Aug 2024 13:35:50 +0800 Subject: [PATCH] Add files via upload --- build.gradle | 9 +- .../java/github/io/koala3353/bot/Bot.java | 4 +- .../bot/commands/BalanceCommand.java | 19 +++- .../bot/commands/ProfileCommand.java | 29 +++++- .../bot/commands/ViewInventoryCommand.java | 16 +++- .../io/koala3353/bot/database/Item.java | 77 ++++++++++++++++ .../koala3353/bot/database/MongoDBPojo.java | 87 ++++++++++++++++++ .../io/koala3353/bot/database/User.java | 90 +++++++++++++++++++ .../io/koala3353/bot/database/UserItem.java | 34 +++++++ 9 files changed, 356 insertions(+), 9 deletions(-) create mode 100644 src/main/java/github/io/koala3353/bot/database/Item.java create mode 100644 src/main/java/github/io/koala3353/bot/database/MongoDBPojo.java create mode 100644 src/main/java/github/io/koala3353/bot/database/User.java create mode 100644 src/main/java/github/io/koala3353/bot/database/UserItem.java diff --git a/build.gradle b/build.gradle index 68281ac..3bdd558 100644 --- a/build.gradle +++ b/build.gradle @@ -42,14 +42,15 @@ tasks.build { dependencies { implementation group: 'net.dv8tion', name: 'JDA', version: '5.0.2' implementation group: 'pw.chew', name: 'jda-chewtils', version: '2.0-SNAPSHOT' - implementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.3.0-alpha16' + implementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.4.12' implementation group: 'io.github.cdimascio', name: 'java-dotenv', version: '5.2.2' - implementation group: 'org.xerial', name: 'sqlite-jdbc', version: '3.36.0.3' + implementation group: 'org.xerial', name: 'sqlite-jdbc', version: '3.41.2.2' implementation group: 'com.zaxxer', name: 'HikariCP', version: '5.0.1' implementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.3.0-alpha16' implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0' implementation group: 'me.xdrop', name: 'fuzzywuzzy', version: '1.4.0' - implementation 'org.apache.httpcomponents:httpclient:4.5.13' - implementation group: 'org.json', name: 'json', version: '20220320' + implementation group: 'org.apache.httpcomponents', name:'httpclient', version: '4.5.13' + implementation group: 'org.json', name: 'json', version: '20231013' implementation 'com.googlecode.json-simple:json-simple:1.1.1' + implementation 'org.mongodb:mongodb-driver-sync:5.1.2' } \ No newline at end of file diff --git a/src/main/java/github/io/koala3353/bot/Bot.java b/src/main/java/github/io/koala3353/bot/Bot.java index 0569c06..e87044a 100644 --- a/src/main/java/github/io/koala3353/bot/Bot.java +++ b/src/main/java/github/io/koala3353/bot/Bot.java @@ -17,6 +17,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Bot { + public static EventWaiter eventWaiter; private static final Logger LOGGER = LoggerFactory.getLogger(Bot.class); public Bot() throws InterruptedException { @@ -25,9 +26,8 @@ public class Bot { // Set the client settings client.setOwnerId(Config.get("owner_id")); client.setStatus(OnlineStatus.ONLINE); - client.setActivity(Activity.listening("for loopholes")); addCommands(client); - EventWaiter eventWaiter = new EventWaiter(); + eventWaiter = new EventWaiter(); // Finalize the command client CommandClient commandClient = client.build(); diff --git a/src/main/java/github/io/koala3353/bot/commands/BalanceCommand.java b/src/main/java/github/io/koala3353/bot/commands/BalanceCommand.java index e29f9c0..42f0fbf 100644 --- a/src/main/java/github/io/koala3353/bot/commands/BalanceCommand.java +++ b/src/main/java/github/io/koala3353/bot/commands/BalanceCommand.java @@ -2,6 +2,11 @@ package github.io.koala3353.bot.commands; import com.jagrosh.jdautilities.command.SlashCommand; import com.jagrosh.jdautilities.command.SlashCommandEvent; +import github.io.koala3353.bot.database.MongoDBPojo; +import net.dv8tion.jda.api.EmbedBuilder; + +import java.awt.*; +import java.time.OffsetDateTime; public class BalanceCommand extends SlashCommand { public BalanceCommand() { @@ -11,6 +16,18 @@ public class BalanceCommand extends SlashCommand { @Override protected void execute(SlashCommandEvent event) { - event.reply("Balance command is not implemented yet!").queue(); + long userId = event.getUser().getIdLong(); + // Get the balance of the user + int balance = MongoDBPojo.getUser(userId).getBalance(); + + EmbedBuilder embed = new EmbedBuilder(); + embed.setAuthor(event.getUser().getEffectiveName() + "'s Balance", null, event.getUser().getAvatarUrl()); + embed.setTimestamp(OffsetDateTime.now()); + embed.setDescription("You have <:shekels:906039266650505256> " + balance + " shekels\n\n" + + "Use the shop to spend your shekels!\n" + + "**Pro tip:** Start your adventure to earn more!"); + embed.setColor(Color.decode("#4A2C6E")); + + event.replyEmbeds(embed.build()).setEphemeral(true).queue(); } } diff --git a/src/main/java/github/io/koala3353/bot/commands/ProfileCommand.java b/src/main/java/github/io/koala3353/bot/commands/ProfileCommand.java index dae8973..02ace2d 100644 --- a/src/main/java/github/io/koala3353/bot/commands/ProfileCommand.java +++ b/src/main/java/github/io/koala3353/bot/commands/ProfileCommand.java @@ -2,6 +2,14 @@ package github.io.koala3353.bot.commands; import com.jagrosh.jdautilities.command.SlashCommand; import com.jagrosh.jdautilities.command.SlashCommandEvent; +import github.io.koala3353.bot.database.MongoDBPojo; +import github.io.koala3353.bot.database.User; +import net.dv8tion.jda.api.EmbedBuilder; + +import java.awt.*; +import java.time.OffsetDateTime; +import java.util.ArrayList; +import java.util.List; public class ProfileCommand extends SlashCommand { public ProfileCommand() { @@ -11,6 +19,25 @@ public class ProfileCommand extends SlashCommand { @Override protected void execute(SlashCommandEvent event) { - event.reply("Profile command is not implemented yet!").queue(); + long userId = event.getUser().getIdLong(); + User dbUser = MongoDBPojo.getUser(userId); + int balance = dbUser.getBalance(); + int uniqueItemsOwned = dbUser.getItems().size(); + List suitsSelected = dbUser.getSuit(); + String suitsSelectedString = String.join(", ", suitsSelected); + boolean hasEscaped = dbUser.isEscaped(); + + EmbedBuilder embedBuilder = new EmbedBuilder(); + embedBuilder.setAuthor(event.getUser().getEffectiveName() + "'s Profile", null, event.getUser().getAvatarUrl()); + embedBuilder.addField("**Balance:**", "<:shekels:906039266650505256> " + balance + " shekels", false); + embedBuilder.addField("**Unique items owned:** ", uniqueItemsOwned + "", false); + embedBuilder.addField("**Suits selected:** ", suitsSelectedString, false); + embedBuilder.addField("**Has escaped:** ", hasEscaped ? "Yes" : "No", false); + embedBuilder.setDescription("Check out your profile!"); + embedBuilder.setColor(Color.decode("#4A2C6E")); + embedBuilder.setThumbnail(event.getJDA().getSelfUser().getAvatarUrl()); + embedBuilder.setTimestamp(OffsetDateTime.now()); + + event.replyEmbeds(embedBuilder.build()).setEphemeral(true).queue(); } } diff --git a/src/main/java/github/io/koala3353/bot/commands/ViewInventoryCommand.java b/src/main/java/github/io/koala3353/bot/commands/ViewInventoryCommand.java index dccc086..913ea07 100644 --- a/src/main/java/github/io/koala3353/bot/commands/ViewInventoryCommand.java +++ b/src/main/java/github/io/koala3353/bot/commands/ViewInventoryCommand.java @@ -2,6 +2,16 @@ package github.io.koala3353.bot.commands; import com.jagrosh.jdautilities.command.SlashCommand; import com.jagrosh.jdautilities.command.SlashCommandEvent; +import com.jagrosh.jdautilities.menu.ButtonEmbedPaginator; +import com.jagrosh.jdautilities.menu.Paginator; +import github.io.koala3353.bot.Bot; +import github.io.koala3353.bot.database.MongoDBPojo; +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.entities.MessageEmbed; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.concurrent.TimeUnit; public class ViewInventoryCommand extends SlashCommand { public ViewInventoryCommand() { @@ -11,6 +21,10 @@ public class ViewInventoryCommand extends SlashCommand { @Override protected void execute(SlashCommandEvent event) { - event.reply("Inventory command is not implemented yet!").queue(); + event.reply("Loading your inventory...").setEphemeral(true).queue(); + EmbedBuilder embed = new EmbedBuilder(); + embed.setAuthor(event.getUser().getEffectiveName() + "'s Inventory", null, event.getUser().getAvatarUrl()); + + } } diff --git a/src/main/java/github/io/koala3353/bot/database/Item.java b/src/main/java/github/io/koala3353/bot/database/Item.java new file mode 100644 index 0000000..1bfa1b7 --- /dev/null +++ b/src/main/java/github/io/koala3353/bot/database/Item.java @@ -0,0 +1,77 @@ +package github.io.koala3353.bot.database; + +import net.dv8tion.jda.api.entities.emoji.Emoji; + +public class Item { + String name; + String description; + String item_id; + int cost; + String emoji; + + public Item() {} + + public String getName() { + return name; + } + + public Item setName(String name) { + this.name = name; + return this; + } + + public String getDescription() { + return description; + } + + public Item setDescription(String description) { + this.description = description; + return this; + } + + public String getItem_id() { + return item_id; + } + + public Item setItem_id(String item_id) { + this.item_id = item_id; + return this; + } + + public int getCost() { + return cost; + } + + public boolean isBuyable() { + return cost > 0; + } + + public Item setCost(int cost) { + this.cost = cost; + return this; + } + + public String getEmoji() { + return emoji; + } + + public Item setEmoji(String emoji) { + this.emoji = emoji; + return this; + } + + public Emoji getEmojiObject() { + return Emoji.fromFormatted(this.emoji); + } + + @Override + public String toString() { + return "Item{" + + "name='" + name + '\'' + + ", description='" + description + '\'' + + ", id='" + item_id + '\'' + + ", cost=" + cost + + ", emoji='" + emoji + '\'' + + '}'; + } +} diff --git a/src/main/java/github/io/koala3353/bot/database/MongoDBPojo.java b/src/main/java/github/io/koala3353/bot/database/MongoDBPojo.java new file mode 100644 index 0000000..4bbc871 --- /dev/null +++ b/src/main/java/github/io/koala3353/bot/database/MongoDBPojo.java @@ -0,0 +1,87 @@ +package github.io.koala3353.bot.database; + +import static com.mongodb.MongoClientSettings.getDefaultCodecRegistry; +import static com.mongodb.client.model.Filters.eq; +import static org.bson.codecs.configuration.CodecRegistries.fromProviders; +import static org.bson.codecs.configuration.CodecRegistries.fromRegistries; +import org.bson.codecs.configuration.CodecProvider; +import org.bson.codecs.configuration.CodecRegistry; +import org.bson.codecs.pojo.PojoCodecProvider; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoDatabase; +import org.slf4j.Logger; + +import java.util.ArrayList; +import java.util.List; + +public class MongoDBPojo { + private static final Logger LOGGER = org.slf4j.LoggerFactory.getLogger(MongoDBPojo.class); + public static User getUser(long discordId) { + CodecProvider pojoCodecProvider = PojoCodecProvider.builder().automatic(true).build(); + CodecRegistry pojoCodecRegistry = fromRegistries(getDefaultCodecRegistry(), fromProviders(pojoCodecProvider)); + // Replace the uri string with your MongoDB deployment's connection string + String uri = "mongodb+srv://keenebrigado:s2jwxTBHPbFxxtNO@rpg-loophole.oc40e.mongodb.net/?retryWrites=true&w=majority&appName=rpg-loophole"; + try (MongoClient mongoClient = MongoClients.create(uri)) { + MongoDatabase database = mongoClient.getDatabase("rpg").withCodecRegistry(pojoCodecRegistry); + MongoCollection collection = database.getCollection("user", User.class); + User user = collection.find(eq("discord", discordId)).first(); + LOGGER.info("Retrieved user from MongoDB successfully " + user.toString()); + return user; + } + } + + public static User addUser(long discordId) { + CodecProvider pojoCodecProvider = PojoCodecProvider.builder().automatic(true).build(); + CodecRegistry pojoCodecRegistry = fromRegistries(getDefaultCodecRegistry(), fromProviders(pojoCodecProvider)); + // Replace the uri string with your MongoDB deployment's connection string + String uri = "mongodb+srv://keenebrigado:s2jwxTBHPbFxxtNO@rpg-loophole.oc40e.mongodb.net/?retryWrites=true&w=majority&appName=rpg-loophole"; + try (MongoClient mongoClient = MongoClients.create(uri)) { + MongoDatabase database = mongoClient.getDatabase("rpg").withCodecRegistry(pojoCodecRegistry); + MongoCollection collection = database.getCollection("user", User.class); + User user = new User(discordId); + collection.insertOne(user); + LOGGER.info("Added user to MongoDB successfully " + user.toString()); + return user; + } + } + + // Items + + public static Item getItem(String itemId) { + CodecProvider pojoCodecProvider = PojoCodecProvider.builder().automatic(true).build(); + CodecRegistry pojoCodecRegistry = fromRegistries(getDefaultCodecRegistry(), fromProviders(pojoCodecProvider)); + // Replace the uri string with your MongoDB deployment's connection string + String uri = "mongodb+srv://keenebrigado:s2jwxTBHPbFxxtNO@rpg-loophole.oc40e.mongodb.net/?retryWrites=true&w=majority&appName=rpg-loophole"; + try (MongoClient mongoClient = MongoClients.create(uri)) { + MongoDatabase database = mongoClient.getDatabase("rpg").withCodecRegistry(pojoCodecRegistry); + MongoCollection collection = database.getCollection("items", Item.class); + Item item = collection.find(eq("id", itemId)).first(); + LOGGER.info("Retrieved item from MongoDB successfully " + item.toString()); + return item; + } + } + + public static List getItems() { + CodecProvider pojoCodecProvider = PojoCodecProvider.builder().automatic(true).build(); + CodecRegistry pojoCodecRegistry = fromRegistries(getDefaultCodecRegistry(), fromProviders(pojoCodecProvider)); + // Replace the uri string with your MongoDB deployment's connection string + String uri = "mongodb+srv://keenebrigado:s2jwxTBHPbFxxtNO@rpg-loophole.oc40e.mongodb.net/?retryWrites=true&w=majority&appName=rpg-loophole"; + try (MongoClient mongoClient = MongoClients.create(uri)) { + MongoDatabase database = mongoClient.getDatabase("rpg").withCodecRegistry(pojoCodecRegistry); + MongoCollection collection = database.getCollection("items", Item.class); + List items = new ArrayList<>(); + collection.find().into(items); + LOGGER.info("Retrieved items from MongoDB successfully " + items); + return items; + } + } + + public static void main(String[] args) { + // Test the MongoDB connection + getUser(959233424890142801L); + //addUser(959233424890142801); + getItems(); + } +} \ No newline at end of file diff --git a/src/main/java/github/io/koala3353/bot/database/User.java b/src/main/java/github/io/koala3353/bot/database/User.java new file mode 100644 index 0000000..a423027 --- /dev/null +++ b/src/main/java/github/io/koala3353/bot/database/User.java @@ -0,0 +1,90 @@ +package github.io.koala3353.bot.database; + +import java.util.ArrayList; +import java.util.List; + +public class User { + long discord; + List items; + int balance; + int loop; + boolean escaped; + List suit; + + public User(long discord) { + this.discord = discord; + this.items = new ArrayList<>(); + this.balance = 0; + this.loop = 0; + this.escaped = false; + this.suit = new ArrayList<>(); + } + + public User() {} + + public long getDiscord() { + return discord; + } + + public User setDiscord(long discord) { + this.discord = discord; + return this; + } + + public List getItems() { + return items; + } + + public User setItems(List items) { + this.items = items; + return this; + } + + public int getBalance() { + return balance; + } + + public User setBalance(int balance) { + this.balance = balance; + return this; + } + + public int getLoop() { + return loop; + } + + public User setLoop(int loop) { + this.loop = loop; + return this; + } + + public boolean isEscaped() { + return escaped; + } + + public User setEscaped(boolean escaped) { + this.escaped = escaped; + return this; + } + + public List getSuit() { + return suit; + } + + public User setSuit(List suit) { + this.suit = suit; + return this; + } + + @Override + public String toString() { + return "User{" + + "discord=" + discord + + ", items=" + items + + ", balance=" + balance + + ", loop=" + loop + + ", escaped=" + escaped + + ", suit=" + suit + + '}'; + } +} diff --git a/src/main/java/github/io/koala3353/bot/database/UserItem.java b/src/main/java/github/io/koala3353/bot/database/UserItem.java new file mode 100644 index 0000000..b2ae9d9 --- /dev/null +++ b/src/main/java/github/io/koala3353/bot/database/UserItem.java @@ -0,0 +1,34 @@ +package github.io.koala3353.bot.database; + +public class UserItem { + int amount; + String item; + + public UserItem() {} + + public int getAmount() { + return amount; + } + + public UserItem setAmount(int amount) { + this.amount = amount; + return this; + } + + public String getItem() { + return item; + } + + public UserItem setItem(String item) { + this.item = item; + return this; + } + + @Override + public String toString() { + return "UserItem{" + + "amount=" + amount + + ", item='" + item + '\'' + + '}'; + } +}