-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ports SecretClaire to ClaireBot. This was fairly straightforward as SecretClaire is based on BaseBot, which itself is based on ClaireBot. In other words, they share the same architecture. Adds the option to get a user's server avatar instead of only their global avatar. Not sure when I did this, but I must've done it at some point. Finally re-adds the Zerfas reaction from ClaireBot 2. This breaks backwards compatibility of config files and they will either need to be updated manually, or regenerated. You must also fill out these config parameters or the bot will generate many errors.
- Loading branch information
Showing
16 changed files
with
436 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 14 additions & 3 deletions
17
src/main/java/com/sidpatchy/clairebot/Embed/Commands/Regular/AvatarEmbed.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,30 @@ | ||
package com.sidpatchy.clairebot.Embed.Commands.Regular; | ||
|
||
import com.sidpatchy.clairebot.Main; | ||
import org.javacord.api.entity.Icon; | ||
import org.javacord.api.entity.message.embed.EmbedBuilder; | ||
import org.javacord.api.entity.server.Server; | ||
import org.javacord.api.entity.user.User; | ||
|
||
public class AvatarEmbed { | ||
|
||
public static EmbedBuilder getAvatar(User user, User author) { | ||
public static EmbedBuilder getAvatar(Server server, User user, User author, boolean globalAvatar) { | ||
if (user == null) { user = author; } | ||
|
||
// Check whether we should display the user's global avatar, or server specific avatar. | ||
Icon avatar; | ||
if (!globalAvatar && server != null) { | ||
avatar = user.getServerAvatar(server).orElse(user.getAvatar()); | ||
} | ||
else { | ||
avatar = user.getAvatar(); | ||
} | ||
|
||
return new EmbedBuilder() | ||
.setColor(Main.getColor(user.getIdAsString())) | ||
.setTimestampToNow() | ||
.setAuthor(user.getDiscriminatedName(), "", user.getAvatar()) | ||
.setImage(user.getAvatar().getUrl().toString().replace("?size=1024", "?size=4096")) | ||
.setAuthor(user.getDiscriminatedName(), "", user.getAvatar()) // always display global avatar in author field. | ||
.setImage(avatar.getUrl().toString().replace("?size=1024", "?size=4096")) | ||
.setFooter(String.valueOf(user.getId())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
src/main/java/com/sidpatchy/clairebot/Embed/Commands/Regular/SantaEmbed.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package com.sidpatchy.clairebot.Embed.Commands.Regular; | ||
|
||
import com.sidpatchy.clairebot.Main; | ||
import com.sidpatchy.clairebot.Util.SantaUtils; | ||
import org.javacord.api.entity.message.MessageBuilder; | ||
import org.javacord.api.entity.message.component.ActionRow; | ||
import org.javacord.api.entity.message.component.Button; | ||
import org.javacord.api.entity.message.embed.EmbedBuilder; | ||
import org.javacord.api.entity.permission.Role; | ||
import org.javacord.api.entity.server.Server; | ||
import org.javacord.api.entity.user.User; | ||
|
||
import java.util.*; | ||
|
||
public class SantaEmbed { | ||
|
||
public static EmbedBuilder getConfirmationEmbed(User author) { | ||
return new EmbedBuilder() | ||
.setColor(Main.getColor(author.getIdAsString())) | ||
.setAuthor("SecretClaire", "", "https://github.com/Sidpatchy/ClaireBot/blob/main/img/ClaireBot-SantaHat.png?raw=true") | ||
.setDescription("Confirmed! I've sent you a direct message. Please continue there."); | ||
} | ||
|
||
/** | ||
* Method for constructing the message to send to the host of the exchange. | ||
* | ||
* @param role The group of users participating | ||
* @param author Author of the command. | ||
* @param rules Rules for the exchange, seperated by \n. | ||
* @param theme Theme for the exchange. | ||
* @return Message with components | ||
*/ | ||
public static MessageBuilder getHostMessage(Role role, User author, String rules, String theme) { | ||
Set<User> users = role.getUsers(); | ||
Server server = role.getServer(); | ||
|
||
MessageBuilder message = new MessageBuilder(); | ||
|
||
EmbedBuilder embed = new EmbedBuilder() | ||
.setColor(Main.getColor(author.getIdAsString())) | ||
.setAuthor("SecretClaire", "", "https://github.com/Sidpatchy/ClaireBot/blob/main/img/ClaireBot-SantaHat.png?raw=true") | ||
.setFooter(SantaUtils.getSantaID(server.getIdAsString(), author.getIdAsString(), role.getIdAsString()), server.getIcon().orElse(null)); | ||
|
||
if (!theme.isEmpty()) { | ||
embed.addField("Theme", theme, false); | ||
} | ||
|
||
if (!rules.isEmpty()) { | ||
embed.addField("Rules", rules, false); | ||
} | ||
|
||
HashMap<User, User> santaList = assignSecretSanta(users); | ||
|
||
for (Map.Entry<User, User> userPair : santaList.entrySet()) { | ||
User giver = userPair.getKey(); | ||
User receiver = userPair.getValue(); | ||
|
||
embed.addField(giver.getIdAsString(), giver.getNicknameMentionTag() + " → " + receiver.getNicknameMentionTag(), false); | ||
} | ||
|
||
ActionRow actionRow = ActionRow.of( | ||
Button.primary("rules", "Add rules"), | ||
Button.primary("theme", "Add a theme"), | ||
Button.danger("send", "Send messages"), | ||
Button.success("test", "Send sample"), | ||
Button.secondary("randomize", "Re-randomize")); | ||
|
||
message.addEmbed(embed); | ||
message.addComponents(actionRow); | ||
return message; | ||
} | ||
|
||
public static MessageBuilder getSantaMessage(Server server, User author, User giver, User receiver, String rules, String theme) { | ||
|
||
MessageBuilder message = new MessageBuilder(); | ||
|
||
EmbedBuilder embed = new EmbedBuilder() | ||
.setColor(Main.getColor(author.getIdAsString())) | ||
.setAuthor("SecretClaire", "", "https://github.com/Sidpatchy/ClaireBot/blob/main/img/ClaireBot-SantaHat.png?raw=true") | ||
.setFooter("Sent by " + author.getName(), author.getAvatar()); | ||
|
||
if (!theme.isEmpty()) { | ||
embed.addField("Theme", theme, false); | ||
} | ||
|
||
if (!rules.isEmpty()) { | ||
embed.addField("Rules", rules, false); | ||
} | ||
|
||
embed.setDescription("Ho! Ho! Ho! You have received **" + receiver.getDisplayName(server) + "** in the " + server.getName() + " Secret Santa!"); | ||
|
||
message.addEmbed(embed); | ||
|
||
return message; | ||
} | ||
|
||
/** | ||
* Method implementing a simple selected-cycle approach for pairing users. | ||
* | ||
* @param participants the set of users participating in the exchange | ||
* @return A shuffled hashmap of paired up users | ||
*/ | ||
private static HashMap<User, User> assignSecretSanta(Set<User> participants) { | ||
List<User> userList = new ArrayList<>(participants); | ||
|
||
// Shuffle the list to ensure random assignment | ||
Collections.shuffle(userList); | ||
|
||
HashMap<User, User> users = new HashMap<>(); //giver, receiver | ||
|
||
// Creating a directed cycle | ||
for (int i = 0; i < userList.size(); i++) { | ||
User giver = userList.get(i); | ||
User receiver = userList.get((i + 1) % userList.size()); | ||
|
||
// Assign the receiver to the giver here | ||
users.put(giver, receiver); | ||
} | ||
|
||
return users; | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/main/java/com/sidpatchy/clairebot/Listener/ButtonClick.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.sidpatchy.clairebot.Listener; | ||
|
||
import com.sidpatchy.clairebot.Embed.Commands.Regular.SantaEmbed; | ||
import com.sidpatchy.clairebot.Main; | ||
import com.sidpatchy.clairebot.MessageComponents.Regular.SantaModal; | ||
import com.sidpatchy.clairebot.Util.SantaUtils; | ||
import org.javacord.api.entity.message.Message; | ||
import org.javacord.api.entity.message.embed.Embed; | ||
import org.javacord.api.entity.message.embed.EmbedFooter; | ||
import org.javacord.api.entity.permission.Role; | ||
import org.javacord.api.entity.server.Server; | ||
import org.javacord.api.entity.user.User; | ||
import org.javacord.api.event.interaction.ButtonClickEvent; | ||
import org.javacord.api.interaction.ButtonInteraction; | ||
import org.javacord.api.listener.interaction.ButtonClickListener; | ||
|
||
public class ButtonClick implements ButtonClickListener { | ||
@Override | ||
public void onButtonClick(ButtonClickEvent event) { | ||
ButtonInteraction buttonInteraction = event.getButtonInteraction(); | ||
|
||
String buttonID = buttonInteraction.getCustomId().toLowerCase(); | ||
User buttonAuthor = buttonInteraction.getUser(); | ||
Message message = buttonInteraction.getMessage(); | ||
|
||
Embed embed = buttonInteraction.getMessage().getEmbeds().get(0); | ||
EmbedFooter footer = embed.getFooter().orElse(null); | ||
|
||
// Extract data from embed fields | ||
SantaUtils.ExtractionResult extractionResult = SantaUtils.extractDataFromEmbed(embed, footer); | ||
|
||
Server server = Main.getApi().getServerById(extractionResult.santaID.get("serverID")).orElse(null); | ||
User author = Main.getApi().getUserById(extractionResult.santaID.get("authorID")).join(); | ||
|
||
switch (buttonID) { | ||
case "rules": | ||
buttonInteraction.respondWithModal("santa-rules-" + message.getIdAsString(), "Update Rules", | ||
SantaModal.getRulesRow() | ||
); | ||
|
||
break; | ||
case "theme": | ||
buttonInteraction.respondWithModal("santa-theme-" + message.getIdAsString(), "Update Theme", | ||
SantaModal.getThemeRow() | ||
); | ||
|
||
break; | ||
case "send": | ||
buttonInteraction.acknowledge(); | ||
for (int i = 0; i < extractionResult.givers.size(); i++) { | ||
SantaEmbed.getSantaMessage(server, author, extractionResult.givers.get(i), extractionResult.receivers.get(i), extractionResult.rules, extractionResult.theme).send(extractionResult.givers.get(i)); | ||
} | ||
|
||
break; | ||
case "test": | ||
buttonInteraction.acknowledge(); | ||
SantaEmbed.getSantaMessage(server, author, extractionResult.givers.get(0), extractionResult.receivers.get(0), extractionResult.rules, extractionResult.theme).send(buttonAuthor); | ||
|
||
break; | ||
case "randomize": | ||
buttonInteraction.acknowledge(); | ||
Role role = Main.getApi().getRoleById(extractionResult.santaID.get("roleID")).orElse(null); | ||
buttonInteraction.getMessage().delete(); | ||
SantaEmbed.getHostMessage(role, buttonAuthor, extractionResult.rules, extractionResult.theme).send(buttonAuthor); | ||
|
||
break; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.