Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a log channel for creating and manually ending giveaways #234

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 33 additions & 9 deletions src/main/java/com/jagrosh/giveawaybot/GiveawayManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,12 @@ public boolean deleteGiveaway(Giveaway giveaway)
return false;
}
}

public boolean endGiveaway(Giveaway giveaway)

public boolean endGiveaway(Giveaway giveaway) {
return endGiveaway(giveaway, false, null);
}

public boolean endGiveaway(Giveaway giveaway, boolean manual, String endingUserId)
{
List<CachedUser> entries = database.getEntriesList(giveaway.getMessageId());
database.removeGiveaway(giveaway.getMessageId());
Expand All @@ -142,8 +146,12 @@ public boolean endGiveaway(Giveaway giveaway)
JSONObject summary = createGiveawaySummary(giveaway, host, entries, winners);
String url = uploader.uploadFile(summary.toString(), "giveaway_summary.json");
String summaryKey = url == null ? null : url.replaceAll(".*/(\\d+/\\d+)/.*", "$1");
rest.request(Route.PATCH_MESSAGE.format(giveaway.getChannelId(), giveaway.getMessageId()), renderGiveaway(giveaway, entries.size(), winners, summaryKey).toJson()).get();
GuildSettings gs = database.getSettings(giveaway.getGuildId());
rest.request(Route.PATCH_MESSAGE.format(giveaway.getChannelId(), giveaway.getMessageId()), renderGiveaway(giveaway, entries.size(), winners, summaryKey, gs).toJson()).get();
rest.request(Route.POST_MESSAGE.format(giveaway.getChannelId()), renderWinnerMessage(giveaway, winners).toJson()).get();
if(manual) {
tryLog(gs, "Giveaway manually ended by <@" + endingUserId + ">");
}
}
catch(ExecutionException | InterruptedException ex)
{
Expand Down Expand Up @@ -209,7 +217,8 @@ public long sendGiveaway(Giveaway giveaway, long guildId, long channelId) throws
{
giveaway.setGuildId(guildId);
giveaway.setChannelId(channelId);
SentMessage sm = renderGiveaway(giveaway, 0);
GuildSettings settings = database.getSettings(guildId);
SentMessage sm = renderGiveaway(giveaway, 0, settings);
log.debug("Attempting giveaway creation in " + guildId + ", json: " + sm.toJson());
RestResponse res = rest.request(Route.POST_MESSAGE.format(channelId), sm.toJson()).get();
log.debug("Attempted to create giveaway, response: " + res.getStatus() + ", " + res.getBody());
Expand All @@ -222,7 +231,7 @@ public long sendGiveaway(Giveaway giveaway, long guildId, long channelId) throws
}
ReceivedMessage rm = new ReceivedMessage(res.getBody());
giveaway.setMessageId(rm.getIdLong());

tryLog(settings, "Giveaway started by <@" + giveaway.getUserId() + "> in <#" + giveaway.getChannelId() + ">");
// sanity checks
if(/*rm.getGuildId() != guildId ||*/ rm.getChannelId() != channelId)
{
Expand All @@ -246,15 +255,24 @@ public long sendGiveaway(Giveaway giveaway, long guildId, long channelId) throws

public SentMessage renderGiveaway(Giveaway giveaway, int numEntries)
{
return renderGiveaway(giveaway, numEntries, null, null);
return renderGiveaway(giveaway, numEntries, null, null, database.getSettings(giveaway.getGuildId()));
}

public SentMessage renderGiveaway(Giveaway giveaway, int numEntries, GuildSettings settings)
{
return renderGiveaway(giveaway, numEntries, null, null, settings);
}

public SentMessage renderGiveaway(Giveaway giveaway, int numEntries, List<CachedUser> winners, String summaryKey)
{
GuildSettings gs = database.getSettings(giveaway.getGuildId());
return renderGiveaway(giveaway, numEntries, winners, summaryKey, database.getSettings(giveaway.getGuildId()));
}

public SentMessage renderGiveaway(Giveaway giveaway, int numEntries, List<CachedUser> winners, String summaryKey, GuildSettings gs)
{
String message = (giveaway.getDescription() == null || giveaway.getDescription().isEmpty() ? "" : giveaway.getDescription() + "\n\n")
+ (winners == null ? LocalizedMessage.GIVEAWAY_ENDS.getLocalizedMessage(gs.getLocale()) : LocalizedMessage.GIVEAWAY_ENDED.getLocalizedMessage(gs.getLocale()))
+ ": <t:" + giveaway.getEndInstant().getEpochSecond() + ":R> (<t:" + giveaway.getEndInstant().getEpochSecond() + ":f>)"
+ (winners == null ? LocalizedMessage.GIVEAWAY_ENDS.getLocalizedMessage(gs.getLocale()) : LocalizedMessage.GIVEAWAY_ENDED.getLocalizedMessage(gs.getLocale()))
+ ": <t:" + giveaway.getEndInstant().getEpochSecond() + ":R> (<t:" + giveaway.getEndInstant().getEpochSecond() + ":f>)"
+ "\n" + LocalizedMessage.GIVEAWAY_HOSTED.getLocalizedMessage(gs.getLocale()) + ": <@" + giveaway.getUserId() + ">"
+ "\n" + LocalizedMessage.GIVEAWAY_ENTRIES.getLocalizedMessage(gs.getLocale()) + ": **" + numEntries + "**"
+ "\n" + LocalizedMessage.GIVEAWAY_WINNERS.getLocalizedMessage(gs.getLocale()) + ": " + (winners == null ? "**" + giveaway.getWinners() + "**" : renderWinners(winners));
Expand Down Expand Up @@ -313,4 +331,10 @@ private ButtonComponent createEntryButton(EmojiParser.ParsedEntryButton pe)
pe.hasEmoji() ? new PartialEmoji(pe.name, pe.id, pe.animated) : null,
ENTER_BUTTON_ID, null, false);
}

private void tryLog(GuildSettings settings, String msg) throws ExecutionException, InterruptedException {
if(settings.getLogChannelId() != 0L)
rest.request(Route.POST_MESSAGE.format(settings.getLogChannelId()), new SentMessage.Builder()
.setContent(msg).build().toJson()).get();
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/jagrosh/giveawaybot/commands/EndCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public InteractionResponse gbExecute(Interaction interaction) throws GiveawayExc
if(g == null || g.getGuildId() != interaction.getGuildId())
return respondError(LocalizedMessage.ERROR_GIVEAWAY_NOT_FOUND.getLocalizedMessage(interaction.getEffectiveLocale(), id+""));

boolean success = bot.getGiveawayManager().endGiveaway(g);
boolean success = bot.getGiveawayManager().endGiveaway(g, true, interaction.getUser().getId());

if(success)
return respondSuccess(LocalizedMessage.SUCCESS_GIVEAWAY_ENDED.getLocalizedMessage(interaction.getEffectiveLocale(), id+""));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ public SettingsCmd(GiveawayBot bot)
ApplicationCommandOption group = new ApplicationCommandOption(ApplicationCommandOption.Type.SUB_COMMAND_GROUP, "set", "set settings", false);
ApplicationCommandOption colorCmd = new ApplicationCommandOption(ApplicationCommandOption.Type.SUB_COMMAND, "color", "set giveaway embed color", false);
colorCmd.addOptions(new ApplicationCommandOption(ApplicationCommandOption.Type.STRING, "hex", "hex code or standard color name", true));
ApplicationCommandOption logCmd = new ApplicationCommandOption(ApplicationCommandOption.Type.SUB_COMMAND, "logchannel", "sets the log channel", false);
logCmd.addOptions(new ApplicationCommandOption(ApplicationCommandOption.Type.CHANNEL, "channel", "channel to log to", true));
ApplicationCommandOption buttonCmd = new ApplicationCommandOption(ApplicationCommandOption.Type.SUB_COMMAND, "emoji", "set giveaway button emoji and text", false);
buttonCmd.addOptions(new ApplicationCommandOption(ApplicationCommandOption.Type.STRING, "emoji", "emoji or button text", true, 1, 64, false));
//ApplicationCommandOption roleCmd = new ApplicationCommandOption(ApplicationCommandOption.Type.SUB_COMMAND, "role", "set giveaway manager role", false);
//roleCmd.addOptions(new ApplicationCommandOption(ApplicationCommandOption.Type.ROLE, "role", "role that can create and manage giveaways", true));
group.addOptions(colorCmd, buttonCmd/*, roleCmd*/);
group.addOptions(colorCmd, buttonCmd/*, roleCmd*/, logCmd);
this.app = new ApplicationCommand.Builder()
.setType(ApplicationCommand.Type.CHAT_INPUT)
.setName(bot.getCommandPrefix() + "settings")
Expand Down Expand Up @@ -104,6 +106,10 @@ protected InteractionResponse gbExecute(Interaction interaction) throws Giveaway
return respondSuccess(LocalizedMessage.SUCCESS_SETTINGS_EMOJI.getLocalizedMessage(wl, pe.render()));
}
else return respondError(LocalizedMessage.ERROR_INVALID_EMOJI_CHOICE.getLocalizedMessage(wl, bot.getGiveawayManager().getEmojiManager().getFreeEmoji()));
case "logchannel":
long channel = cmd.getOptionByName("channel").getIdValue();
bot.getDatabase().setGuildLogChannel(interaction.getGuildId(), channel);
return respondSuccess(LocalizedMessage.SUCCESS_SETTINGS_LOGCHANNEL.getLocalizedMessage(wl, "<#" + channel + ">"));
default:
return respondError("Unknown settings command.");
}
Expand All @@ -113,6 +119,7 @@ protected InteractionResponse gbExecute(Interaction interaction) throws Giveaway
+ LocalizedMessage.INFO_SETTINGS_PREMIUM.getLocalizedMessage(wl) + ": **" + bot.getDatabase().getPremiumLevel(gs.getGuildId(), gs.getOwnerId()) + "**\n"
//+ LocalizedMessage.INFO_SETTINGS_ROLE.getLocalizedMessage(wl) + ": " + (gs.getManagerRoleId() == 0L ? "N/A" : "<@&" + gs.getManagerRoleId() + ">") + "\n"
+ LocalizedMessage.INFO_SETTINGS_EMOJI.getLocalizedMessage(wl) + ": " + gs.getEmoji() + "\n"
+ LocalizedMessage.INFO_SETTINGS_LOGCHANNEL.getLocalizedMessage(wl) + ": <#" + gs.getLogChannelId() + ">\n"
+ LocalizedMessage.INFO_SETTINGS_LOCALE.getLocalizedMessage(wl) + ": " + gs.getLocale().getTextualName() + "\n\n"
+ LocalizedMessage.INFO_SETTINGS_ETC.getLocalizedMessage(wl);
return new MessageCallback(new SentMessage.Builder()
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/jagrosh/giveawaybot/data/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,19 @@ public synchronized void setGuildColor(long guildId, Color color)
gs.setColor(color);
em.getTransaction().commit();
}

public synchronized void setGuildLogChannel(long guildId, long logChannel) {
GuildSettings gs = em.find(GuildSettings.class, guildId);
em.getTransaction().begin();
if(gs == null) {
gs = new GuildSettings();
gs.setGuildId(guildId);
em.persist(gs);
}
gs.setLogChannelId(logChannel);
em.getTransaction().commit();

}

public synchronized void setGuildEmoji(long guildId, String emoji)
{
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/jagrosh/giveawaybot/data/GuildSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class GuildSettings
private String emoji;
@Column(name = "OWNER")
private long ownerId;
@Column(name = "LOG")
private long logChannelId;
@Column(name = "LOCALE")
private String locale;
@Column(name = "LAST_FETCH")
Expand All @@ -62,6 +64,7 @@ public GuildSettings(long guildId)
this.ownerId = 0L;
this.locale = null;
this.latestRetrieval = 0L;
this.logChannelId = 0L;
}

public long getGuildId()
Expand Down Expand Up @@ -124,6 +127,14 @@ public void setOwnerId(long ownerId)
this.ownerId = ownerId;
}

public long getLogChannelId() {
return logChannelId;
}

public void setLogChannelId(long logChannelId) {
this.logChannelId = logChannelId;
}

public WebLocale getLocale()
{
return WebLocale.of(locale);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public enum LocalizedMessage
SUCCESS_SETTINGS_ROLE("success.settings.role"),
SUCCESS_SETTINGS_COLOR("success.settings.color"),
SUCCESS_SETTINGS_EMOJI("success.settings.emoji"),
SUCCESS_SETTINGS_LOGCHANNEL("success.settings.logchannel"),
SUCCESS_GIVEAWAY_ENDED("success.giveaway_ended"),
SUCCESS_GIVEAWAY_REROLL("success.giveaway_reroll"),
SUCCESS_GIVEAWAY_DELETE("success.giveaway_delete"),
Expand All @@ -99,6 +100,7 @@ public enum LocalizedMessage
INFO_SETTINGS_EMOJI("info.settings.emoji"),
INFO_SETTINGS_ROLE("info.settings.role"),
INFO_SETTINGS_COLOR("info.settings.color"),
INFO_SETTINGS_LOGCHANNEL("info.settings.logchannel"),
INFO_SETTINGS_LOCALE("info.settings.locale"),
INFO_SETTINGS_ETC("info.settings.etc"),
INFO_HELP_GENERAL("info.help.general"),
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/localization/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ success.ping = Pong!
success.winner = Congratulations {0}! You won the **{1}**!
success.settings.role = Users with the {0} role can now manage giveaways!
success.settings.color = The embed color for giveaways has been changed to `{0}`.
success.settings.emoji = The custom button emoji as been changed to `{0}`.
success.settings.emoji = The custom button emoji has been changed to `{0}`.
success.settings.logchannel = The log channel has been changed to `{0}`.
success.giveaway_ended = Successfully ended giveaway {0}!
success.giveaway_reroll = {0} rerolled the giveaway! Congratulations {1}!
success.giveaway_delete = Successfully deleted giveaway {0}!
Expand All @@ -69,6 +70,7 @@ info.settings.role = Manager Role
info.settings.color = Embed Color
info.settings.locale = Language
info.settings.etc = To control which roles can manage giveaways, head to **Server Settings** > **Integrations** > **GiveawayBot**
info.settings.logchannel = Log Channel
info.help.general = General Commands
info.help.creation = Giveaway Creation Commands
info.help.manipulation = Giveaway Manipulation Commands
Expand Down