From 998216493fcd614cf84cf235fd15ebe31fb432bc Mon Sep 17 00:00:00 2001 From: Larsenv Date: Thu, 18 May 2023 12:56:10 -0500 Subject: [PATCH] Fixes --- .../rc24/bot/commands/general/ReviveCmd.java | 77 ++++++++ .../java/xyz/rc24/bot/core/entities/Poll.java | 36 ++++ .../bot/core/entities/impl/MiitomoPoll.java | 27 +++ .../rc24/bot/core/entities/impl/PollImpl.java | 66 +++++++ .../bot/core/entities/impl/UKPollImpl.java | 39 ++++ .../bot/core/entities/impl/USPollImpl.java | 39 ++++ .../bot/core/entities/impl/WorldPollImpl.java | 39 ++++ .../xyz/rc24/bot/managers/PollManager.java | 180 ++++++++++++++++++ 8 files changed, 503 insertions(+) create mode 100644 src/main/java/xyz/rc24/bot/commands/general/ReviveCmd.java create mode 100644 src/main/java/xyz/rc24/bot/core/entities/Poll.java create mode 100644 src/main/java/xyz/rc24/bot/core/entities/impl/MiitomoPoll.java create mode 100644 src/main/java/xyz/rc24/bot/core/entities/impl/PollImpl.java create mode 100644 src/main/java/xyz/rc24/bot/core/entities/impl/UKPollImpl.java create mode 100644 src/main/java/xyz/rc24/bot/core/entities/impl/USPollImpl.java create mode 100644 src/main/java/xyz/rc24/bot/core/entities/impl/WorldPollImpl.java create mode 100644 src/main/java/xyz/rc24/bot/managers/PollManager.java diff --git a/src/main/java/xyz/rc24/bot/commands/general/ReviveCmd.java b/src/main/java/xyz/rc24/bot/commands/general/ReviveCmd.java new file mode 100644 index 00000000..e7c0223c --- /dev/null +++ b/src/main/java/xyz/rc24/bot/commands/general/ReviveCmd.java @@ -0,0 +1,77 @@ +/* + * MIT License + * + * Copyright (c) 2017-2020 RiiConnect24 and its contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package xyz.rc24.bot.commands.general; + +import com.jagrosh.jdautilities.command.SlashCommand; +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.Permission; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import xyz.rc24.bot.Bot; +import xyz.rc24.bot.commands.Categories; +import xyz.rc24.bot.core.entities.Poll; +import xyz.rc24.bot.core.entities.impl.MiitomoPoll; +import xyz.rc24.bot.managers.PollManager; + +public class ReviveCmd extends SlashCommand +{ + private final PollManager manager; + + public ReviveCmd(Bot bot) + { + this.name = "revive"; + this.help = "Revives the chat by sending an Everybody Votes Channel or Miitomo poll for users to vote in."; + this.category = Categories.GENERAL; + this.botPermissions = new Permission[]{Permission.MESSAGE_EMBED_LINKS}; + this.manager = bot.getPollManager(); + } + + @Override + protected void execute(SlashCommandEvent event) + { + // Get a random poll + Poll poll = manager.getRandomPoll(); + + // Now we need to build the embed + EmbedBuilder embed = new EmbedBuilder() + {{ + setTitle("<:EverybodyVotesChannel:317090360449040388> " + poll.getQuestion()); + + if(!(poll instanceof MiitomoPoll)) + { + setDescription("\uD83C\uDD70 " + poll.getResponse1() + "\n" + + "_ _\n" + // Line separator + "\uD83C\uDD71 " + poll.getResponse2()); + setFooter("This question was from the " + poll.getCountryFlag() + " EVC", null); + } + else + setFooter("This question was from Miitomo"); + + // setColor(event.getSelfMember().getColor()); + }}; + + // Send embed to chat + event.replyEmbeds(embed.build()).queue(); + } +} diff --git a/src/main/java/xyz/rc24/bot/core/entities/Poll.java b/src/main/java/xyz/rc24/bot/core/entities/Poll.java new file mode 100644 index 00000000..2d4fec52 --- /dev/null +++ b/src/main/java/xyz/rc24/bot/core/entities/Poll.java @@ -0,0 +1,36 @@ +/* + * MIT License + * + * Copyright (c) 2017-2020 RiiConnect24 and its contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package xyz.rc24.bot.core.entities; + +public interface Poll +{ + String getQuestion(); + + String getResponse1(); + + String getResponse2(); + + String getCountryFlag(); +} diff --git a/src/main/java/xyz/rc24/bot/core/entities/impl/MiitomoPoll.java b/src/main/java/xyz/rc24/bot/core/entities/impl/MiitomoPoll.java new file mode 100644 index 00000000..c059eceb --- /dev/null +++ b/src/main/java/xyz/rc24/bot/core/entities/impl/MiitomoPoll.java @@ -0,0 +1,27 @@ +package xyz.rc24.bot.core.entities.impl; + +public class MiitomoPoll extends PollImpl +{ + public MiitomoPoll(String question) + { + super(question, null, null); + } + + @Override + public String getResponse1() + { + throw new UnsupportedOperationException("Miitomo polls don't have responses!"); + } + + @Override + public String getResponse2() + { + throw new UnsupportedOperationException("Miitomo polls don't have responses!"); + } + + @Override + public String getCountryFlag() + { + throw new UnsupportedOperationException("Miitomo polls don't have country flag!"); + } +} diff --git a/src/main/java/xyz/rc24/bot/core/entities/impl/PollImpl.java b/src/main/java/xyz/rc24/bot/core/entities/impl/PollImpl.java new file mode 100644 index 00000000..adb5ba15 --- /dev/null +++ b/src/main/java/xyz/rc24/bot/core/entities/impl/PollImpl.java @@ -0,0 +1,66 @@ +/* + * MIT License + * + * Copyright (c) 2017-2020 RiiConnect24 and its contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package xyz.rc24.bot.core.entities.impl; + +import xyz.rc24.bot.core.entities.Poll; + +public abstract class PollImpl implements Poll +{ + private String question, response1, response2; + + PollImpl(String question, String response1, String response2) + { + this.question = question; + this.response1 = response1; + this.response2 = response2; + } + + @Override + public String getQuestion() + { + return question; + } + + @Override + public String getResponse1() + { + return response1; + } + + @Override + public String getResponse2() + { + return response2; + } + + @Override + public abstract String getCountryFlag(); + + @Override + public String toString() + { + return "Poll(" + getQuestion() + ")"; + } +} diff --git a/src/main/java/xyz/rc24/bot/core/entities/impl/UKPollImpl.java b/src/main/java/xyz/rc24/bot/core/entities/impl/UKPollImpl.java new file mode 100644 index 00000000..6b5083e5 --- /dev/null +++ b/src/main/java/xyz/rc24/bot/core/entities/impl/UKPollImpl.java @@ -0,0 +1,39 @@ +/* + * MIT License + * + * Copyright (c) 2017-2020 RiiConnect24 and its contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package xyz.rc24.bot.core.entities.impl; + +public class UKPollImpl extends PollImpl +{ + public UKPollImpl(String question, String response1, String response2) + { + super(question, response1, response2); + } + + @Override + public String getCountryFlag() + { + return "\uD83C\uDDEC\uD83C\uDDE7"; + } +} diff --git a/src/main/java/xyz/rc24/bot/core/entities/impl/USPollImpl.java b/src/main/java/xyz/rc24/bot/core/entities/impl/USPollImpl.java new file mode 100644 index 00000000..99b1154d --- /dev/null +++ b/src/main/java/xyz/rc24/bot/core/entities/impl/USPollImpl.java @@ -0,0 +1,39 @@ +/* + * MIT License + * + * Copyright (c) 2017-2020 RiiConnect24 and its contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package xyz.rc24.bot.core.entities.impl; + +public class USPollImpl extends PollImpl +{ + public USPollImpl(String question, String response1, String response2) + { + super(question, response1, response2); + } + + @Override + public String getCountryFlag() + { + return "\uD83C\uDDFA\uD83C\uDDF8"; + } +} diff --git a/src/main/java/xyz/rc24/bot/core/entities/impl/WorldPollImpl.java b/src/main/java/xyz/rc24/bot/core/entities/impl/WorldPollImpl.java new file mode 100644 index 00000000..7dacc504 --- /dev/null +++ b/src/main/java/xyz/rc24/bot/core/entities/impl/WorldPollImpl.java @@ -0,0 +1,39 @@ +/* + * MIT License + * + * Copyright (c) 2017-2020 RiiConnect24 and its contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package xyz.rc24.bot.core.entities.impl; + +public class WorldPollImpl extends PollImpl +{ + public WorldPollImpl(String question, String response1, String response2) + { + super(question, response1, response2); + } + + @Override + public String getCountryFlag() + { + return "\uD83C\uDF0E"; + } +} diff --git a/src/main/java/xyz/rc24/bot/managers/PollManager.java b/src/main/java/xyz/rc24/bot/managers/PollManager.java new file mode 100644 index 00000000..8041c5f7 --- /dev/null +++ b/src/main/java/xyz/rc24/bot/managers/PollManager.java @@ -0,0 +1,180 @@ +/* + * MIT License + * + * Copyright (c) 2017-2020 RiiConnect24 and its contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package xyz.rc24.bot.managers; + +import org.yaml.snakeyaml.Yaml; +import xyz.rc24.bot.core.entities.Poll; +import xyz.rc24.bot.core.entities.impl.MiitomoPoll; +import xyz.rc24.bot.core.entities.impl.UKPollImpl; +import xyz.rc24.bot.core.entities.impl.USPollImpl; +import xyz.rc24.bot.core.entities.impl.WorldPollImpl; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.IOException; +import java.net.URL; +import java.nio.channels.Channels; +import java.nio.channels.FileChannel; +import java.nio.channels.ReadableByteChannel; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.Set; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +public class PollManager +{ + private final Set current; + private final List polls; + private final Random random; + private final ScheduledExecutorService threadPool; + + public PollManager() throws IOException + { + File miitomoFile = new File("miitomo_questions.yml"); + File usFile = new File("polls_049.yml"); + File ukFile = new File("polls_110.yml"); + File worldFile = new File("polls_world.yml"); + + if(!(miitomoFile.exists())) + downloadFile(MIITOMO_FILE, miitomoFile); + + if(!(usFile.exists())) + downloadFile(US_FILE, usFile); + + if(!(ukFile.exists())) + downloadFile(UK_FILE, ukFile); + + if(!(worldFile.exists())) + downloadFile(WORLD_FILE, worldFile); + + Yaml yaml = new Yaml(); + this.polls = new ArrayList<>(); + this.current = new HashSet<>(); + + populateList(yaml.load(new FileReader(miitomoFile)), yaml.load(new FileReader(usFile)), + yaml.load(new FileReader(ukFile)), yaml.load(new FileReader(worldFile))); + + this.random = new Random(); + + // Free up memory after this mess + System.gc(); + + this.threadPool = Executors.newScheduledThreadPool(2); + } + + public Poll getRandomPoll() + { + return polls.get(random.nextInt(polls.size())); + } + + public void trackId(long id) + { + current.add(id); + threadPool.schedule(() -> unTrackId(id), 10, TimeUnit.MINUTES); + } + + public void unTrackId(long id) + { + current.remove(id); + } + + public boolean isTracked(long id) + { + return current.contains(id); + } + + public ScheduledExecutorService getThreadPool() + { + return threadPool; + } + + private void downloadFile(String downloadUrl, File file) throws IOException + { + URL url = new URL(downloadUrl); + ReadableByteChannel readableByteChannel = Channels.newChannel(url.openStream()); + FileOutputStream fileOutputStream = new FileOutputStream(file); + FileChannel fileChannel = fileOutputStream.getChannel(); + fileChannel.transferFrom(readableByteChannel, 0, Long.MAX_VALUE); + } + + @SuppressWarnings("unchecked") + private void populateList(List> miitomo, List> us, + List> uk, List> world) + { + for(Map data : miitomo) + { + Map section = (Map) data.get("question"); + polls.add(new MiitomoPoll((String) section.get("english"))); + } + + for(Map data : us) + { + Map section = (Map) data.get("poll"); + polls.add(new USPollImpl( + getPart(section, "question"), + getPart(section, "response_1"), + getPart(section, "response_2"))); + } + + for(Map data : uk) + { + Map section = (Map) data.get("poll"); + polls.add(new UKPollImpl( + getPart(section, "question"), + getPart(section, "response_1"), + getPart(section, "response_2"))); + } + + for(Map data : world) + { + Map section = (Map) data.get("poll"); + polls.add(new WorldPollImpl( + getPart(section, "question"), + getPart(section, "response_1"), + getPart(section, "response_2"))); + } + } + + @SuppressWarnings("unchecked") + private String getPart(Map poll, String key) + { + return (String) ((Map) poll.get(key)).get("english"); + } + + private final String MIITOMO_FILE = "https://cdn.discordapp.com/attachments/287740297923002368/" + + "748654266352140288/miitomo_questions.yml"; + private final String UK_FILE = "https://raw.githubusercontent.com/RiiConnect24/Site/master/" + + "_data/votes/results_110.yml"; + private final String US_FILE = "https://raw.githubusercontent.com/RiiConnect24/Site/master/" + + "_data/votes/results_049.yml"; + private final String WORLD_FILE = "https://raw.githubusercontent.com/RiiConnect24/Site/master/" + + "_data/votes/results_world.yml"; +}