Skip to content

Commit

Permalink
changed emoji
Browse files Browse the repository at this point in the history
handles bad discord data
simplified code
updated deps
  • Loading branch information
jagrosh committed Jul 20, 2018
1 parent 3ff436c commit dbf0c96
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 30 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright {yyyy} {name of copyright owner}
Copyright 2018 John Grosh

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.jagrosh</groupId>
<artifactId>GiveawayBot</artifactId>
<version>2.1</version>
<version>2.2</version>
<packaging>jar</packaging>

<properties>
Expand All @@ -30,7 +30,7 @@
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>3.6.0_373</version>
<version>3.7.0_382</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/com/jagrosh/giveawaybot/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@
import net.dv8tion.jda.core.events.guild.member.GuildMemberRoleRemoveEvent;
import net.dv8tion.jda.core.events.role.update.RoleUpdateColorEvent;
import net.dv8tion.jda.core.hooks.ListenerAdapter;
import net.dv8tion.jda.core.utils.SessionController;
import net.dv8tion.jda.core.utils.SessionControllerAdapter;
import net.dv8tion.jda.webhook.WebhookClient;
import net.dv8tion.jda.webhook.WebhookClientBuilder;
import org.slf4j.Logger;
Expand Down Expand Up @@ -89,6 +87,11 @@ public ScheduledExecutorService getThreadpool()
return threadpool;
}

public WebhookClient getWebhook()
{
return webhook;
}

public Database getDatabase()
{
return database;
Expand Down Expand Up @@ -232,6 +235,8 @@ public static void main(int shardTotal, int shardSetId, int shardSetSize) throws
new ShutdownCommand(bot)
).build();

bot.getWebhook().send("\uD83C\uDF89 Starting shards `"+(shardSetId*shardSetSize + 1) + " - " + ((shardSetId+1)*shardSetSize) + "` of `"+shardTotal+"`...");

// start logging in
bot.setShardManager(new DefaultShardManagerBuilder()
.setShardsTotal(shardTotal)
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/jagrosh/giveawaybot/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public class Constants {
public static final OffsetDateTime STARTUP = OffsetDateTime.now();
public static final int PRIZE_MAX = 250;
public static final String TADA = "\uD83C\uDF89";
public static final String YAY = "<:yay:294906617378504704>";
public static final String REACTION = "yay:294906617378504704";
public static final String YAY = "<:yay:440620097543864320>";
public static final String REACTION = "yay:440620097543864320";
public static final Color BLURPLE = Color.decode("#7289DA");
public static final String INVITE = "https://giveawaybot.party/invite";
public static final int MIN_TIME = 10;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class CreateCommand extends Command {

private final static String CANCEL = "\n\n`Giveaway creation has been cancelled.`";
private final static String CHANNEL = "\n\n`Please type the name of a channel in this server.`";
private final static String TIME = "\n\n`Please enter the duration of the giveaway in seconds.`\n`Alternatively, enter a duration in minutes and include an M at the end.`";
private final static String TIME = "\n\n`Please enter the duration of the giveaway in seconds.`\n`Alternatively, enter a duration in minutes and include an M at the end, or days and include a D.`";
private final static String WINNERS = "\n\n`Please enter a number of winners between 1 and 15.`";
private final static String PRIZE = "\n\n`Please enter the giveaway prize. This will also begin the giveaway.`";
private final Bot bot;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected void execute(CommandEvent event)
event.replyWarning("I couldn't find any recent giveaways in this channel.");
else
{
Giveaway.getWinners(m, wins -> event.replySuccess("The new winner is "+wins.get(0).getAsMention()+"! Congratulations!"),
Giveaway.getWinner(m, wins -> event.replySuccess("The new winner is "+wins.getAsMention()+"! Congratulations!"),
() -> event.replyWarning("I couldn't determine a winner for that giveaway."), bot.getThreadpool());
}
}, v -> event.replyError("I failed to retrieve message history"));
Expand All @@ -79,7 +79,7 @@ else if(event.getArgs().matches("\\d{17,20}"))
if(giveaway==null)
{
event.getChannel().getMessageById(event.getArgs()).queue(m -> {
Giveaway.getWinners(m, wins -> event.replySuccess("The new winner is "+wins.get(0).getAsMention()+"! Congratulations!"),
Giveaway.getWinner(m, wins -> event.replySuccess("The new winner is "+wins.getAsMention()+"! Congratulations!"),
() -> event.replyWarning("I couldn't determine a winner for that giveaway."), bot.getThreadpool());
}, v -> event.replyError("I failed to retrieve that message."));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ else if(event.getArgs().matches("\\d{17,20}")) {
}

private void determineWinner(Message m, CommandEvent event) {
Giveaway.getWinners(m, wins -> event.replySuccess("The new winner is "+wins.get(0).getAsMention()+"! Congratulations!"),
Giveaway.getWinner(m, wins -> event.replySuccess("The new winner is "+wins.getAsMention()+"! Congratulations!"),
() -> event.replyWarning("I couldn't determine a winner for that giveaway."), bot.getThreadpool());
}
}
23 changes: 4 additions & 19 deletions src/main/java/com/jagrosh/giveawaybot/entities/Giveaway.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void end(RestJDA restJDA)
eb.setAuthor(prize, null, null);
try {
List<Long> ids = restJDA.getReactionUsers(Long.toString(channelId), Long.toString(messageId), MiscUtil.encodeUTF8(Constants.TADA))
.cache(true).stream().collect(Collectors.toList());
.cache(true).stream().distinct().collect(Collectors.toList());
List<Long> wins = selectWinners(ids, winners);
String toSend;
if(wins.isEmpty())
Expand Down Expand Up @@ -172,32 +172,17 @@ public static <T> List<T> selectWinners(List<T> list, int winners)
return winlist;
}

public static void getWinners(Message message, Consumer<List<User>> success, Runnable failure, ExecutorService threadpool)
public static void getWinner(Message message, Consumer<User> success, Runnable failure, ExecutorService threadpool)
{
threadpool.submit(() -> {
try {
MessageReaction mr = message.getReactions().stream().filter(r -> r.getReactionEmote().getName().equals(Constants.TADA)).findAny().orElse(null);
List<User> users = new LinkedList<>();
mr.getUsers().stream().forEach(u -> users.add(u));
users.remove(mr.getJDA().getSelfUser());
mr.getUsers().stream().distinct().filter(u -> !u.isBot()).forEach(u -> users.add(u));
if(users.isEmpty())
failure.run();
else
{
int wincount;
String[] split = message.getEmbeds().get(0).getFooter().getText().split(" ");
try {
wincount = Integer.parseInt(split[0]);
}catch(NumberFormatException e){
wincount = 1;
}
List<User> wins = new LinkedList<>();
for(int i=0; i<wincount && !users.isEmpty(); i++)
{
wins.add(users.remove((int)(Math.random()*users.size())));
}
success.accept(wins);
}
success.accept(users.get((int)(Math.random()*users.size())));
} catch(Exception e) {
failure.run();
}
Expand Down

0 comments on commit dbf0c96

Please sign in to comment.