Last active
May 17, 2023 21:13
-
-
Save jagrosh/8615aec0801530055c988b4f0d5e320a to your computer and use it in GitHub Desktop.
Simple GiveawayBot using JDA
This file contains hidden or 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
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package giveawaybot; | |
import java.io.File; | |
import java.util.LinkedList; | |
import java.util.List; | |
import javax.security.auth.login.LoginException; | |
import net.dv8tion.jda.core.AccountType; | |
import net.dv8tion.jda.core.JDABuilder; | |
import net.dv8tion.jda.core.Permission; | |
import net.dv8tion.jda.core.entities.Icon; | |
import net.dv8tion.jda.core.entities.Message; | |
import net.dv8tion.jda.core.entities.User; | |
import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent; | |
import net.dv8tion.jda.core.exceptions.RateLimitedException; | |
import net.dv8tion.jda.core.hooks.ListenerAdapter; | |
import net.dv8tion.jda.core.utils.PermissionUtil; | |
/** | |
* | |
* @author John Grosh ([email protected]) | |
*/ | |
public class GiveawayBot extends ListenerAdapter { | |
@Override | |
public void onGuildMessageReceived(GuildMessageReceivedEvent event) { | |
if(event.getMessage().getRawContent().equals("!ghelp")) | |
{ | |
event.getChannel().sendMessage("<:yay:294906617378504704> GiveawayBot help: <:yay:294906617378504704>\n" | |
+ "`!ghelp` - this message\n" | |
+ "`!gstart <seconds> [item]` - starts a giveway. Ex: `!gstart 180` for a 3 minute giveaway\n" | |
+ "`!greroll <messageid>` - rerolls a winner for the giveaway on the provided message\n\n" | |
+ "Commands require Manage Server permission to use\n" | |
+ "Don't include <> nor []; <> means required, [] means optional").queue(); | |
} | |
else if(event.getMessage().getRawContent().startsWith("!gstart")) | |
{ | |
if(!PermissionUtil.checkPermission(event.getGuild(), event.getMember(), Permission.MANAGE_SERVER)) | |
{ | |
event.getChannel().sendMessage("You must have Manage Server perms to use this!").queue(); | |
return; | |
} | |
String str = event.getMessage().getRawContent().substring(7).trim(); | |
String[] parts = str.split("\\s+",2); | |
try{ | |
int sec = Integer.parseInt(parts[0]); | |
event.getChannel().sendMessage("<:yay:294906617378504704> **GIVEAWAY!** <:yay:294906617378504704>\n"+(parts.length>1 ? "\u25AB*`"+parts[1]+"`*\u25AB\n" : "")+"React with \uD83C\uDF89 to enter!").queue(m -> { | |
m.addReaction("\uD83C\uDF89").queue(); | |
new Giveaway(sec,m,parts.length>1 ? parts[1] : null).start(); | |
}); | |
event.getMessage().delete().queue(); | |
} catch(NumberFormatException ex) | |
{ | |
event.getChannel().sendMessage("Could not parse seconds from `"+parts[0]+"`").queue(); | |
} | |
} | |
else if(event.getMessage().getRawContent().startsWith("!greroll")) | |
{ | |
if(!PermissionUtil.checkPermission(event.getGuild(), event.getMember(), Permission.MANAGE_SERVER)) | |
{ | |
event.getChannel().sendMessage("You must have Manage Server perms to use this!").queue(); | |
return; | |
} | |
String id = event.getMessage().getRawContent().substring(8).trim(); | |
if(!id.matches("\\d{17,22}")) | |
{ | |
event.getChannel().sendMessage("Invalid message id").queue(); | |
return; | |
} | |
Message m = event.getChannel().getMessageById(id).complete(); | |
if(m==null) | |
{ | |
event.getChannel().sendMessage("Message not found!").queue(); | |
return; | |
} | |
m.getReactions() | |
.stream().filter(mr -> mr.getEmote().getName().equals("\uD83C\uDF89")) | |
.findAny().ifPresent(mr -> { | |
List<User> users = new LinkedList<>(mr.getUsers().complete()); | |
users.remove(m.getJDA().getSelfUser()); | |
String uid = users.get((int)(Math.random()*users.size())).getId(); | |
event.getChannel().sendMessage("Congratulations to <@"+uid+">! You won the reroll!").queue(); | |
}); | |
} | |
else if(event.getAuthor().getId().equals("OWNERID")) | |
{ | |
if(event.getMessage().getRawContent().startsWith("!say")) | |
event.getChannel().sendMessage(event.getMessage().getRawContent().substring(4).trim()).queue(); | |
else if(event.getMessage().getRawContent().startsWith("!ava")) | |
{ | |
String loc = event.getMessage().getRawContent().substring(4).trim(); | |
try{ | |
event.getJDA().getSelfUser().getManager().setAvatar(Icon.from(new File(loc))).complete(); | |
event.getChannel().sendMessage("Updated!").queue(); | |
}catch(Exception ex){ | |
event.getChannel().sendMessage("Error: "+ex).queue(); | |
} | |
} | |
} | |
} | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) throws LoginException, IllegalArgumentException, RateLimitedException { | |
new JDABuilder(AccountType.BOT).setToken("REDACTED") | |
.addListener(new GiveawayBot()) | |
.buildAsync(); | |
} | |
public class Giveaway { | |
int seconds; | |
Message message; | |
String item; | |
public Giveaway(int time, Message message, String item) | |
{ | |
seconds = time; | |
this.message = message; | |
this.item = item; | |
} | |
public void start() | |
{ | |
new Thread(){ | |
@Override | |
public void run() { | |
while(seconds>5) | |
{ | |
message.editMessage("<:yay:294906617378504704> **GIVEAWAY!** <:yay:294906617378504704>\n"+(item!=null ? "\u25AB*`"+item+"`*\u25AB\n" : "")+"React with \uD83C\uDF89 to enter!\nTime remaining: "+secondsToTime(seconds)).queue(); | |
seconds-=5; | |
try{Thread.sleep(5000);}catch(Exception e){} | |
} | |
while(seconds>0) | |
{ | |
message.editMessage("<:yay:294906617378504704> **G I V E A W A Y!** <:yay:294906617378504704>\nLAST CHANCE TO ENTER!!!\n"+(item!=null ? "\u25AB*`"+item+"`*\u25AB\n" : "")+"React with \uD83C\uDF89 to enter!\nTime remaining: "+secondsToTime(seconds)).queue(); | |
seconds--; | |
try{Thread.sleep(1000);}catch(Exception e){} | |
} | |
message.getChannel().getMessageById(message.getId()).complete().getReactions() | |
.stream().filter(mr -> mr.getEmote().getName().equals("\uD83C\uDF89")) | |
.findAny().ifPresent(mr -> { | |
List<User> users = new LinkedList<>(mr.getUsers().complete()); | |
users.remove(message.getJDA().getSelfUser()); | |
String id = users.get((int)(Math.random()*users.size())).getId(); | |
message.editMessage("<:yay:294906617378504704> **GIVEAWAY ENDED!** <:yay:294906617378504704>\n"+(item!=null ? "\u25AB*`"+item+"`*\u25AB\n" : "")+"\nWinner: <@"+id+"> \uD83C\uDF89").queue(); | |
message.getChannel().sendMessage("Congratulations to <@"+id+">! You won"+(item==null ? "" : " the "+item)+"!").queue(); | |
}); | |
} | |
}.start(); | |
} | |
} | |
public static String secondsToTime(long timeseconds) | |
{ | |
StringBuilder builder = new StringBuilder(); | |
int years = (int)(timeseconds / (60*60*24*365)); | |
if(years>0) | |
{ | |
builder.append("**").append(years).append("** years, "); | |
timeseconds = timeseconds % (60*60*24*365); | |
} | |
int weeks = (int)(timeseconds / (60*60*24*365)); | |
if(weeks>0) | |
{ | |
builder.append("**").append(weeks).append("** weeks, "); | |
timeseconds = timeseconds % (60*60*24*7); | |
} | |
int days = (int)(timeseconds / (60*60*24)); | |
if(days>0) | |
{ | |
builder.append("**").append(days).append("** days, "); | |
timeseconds = timeseconds % (60*60*24); | |
} | |
int hours = (int)(timeseconds / (60*60)); | |
if(hours>0) | |
{ | |
builder.append("**").append(hours).append("** hours, "); | |
timeseconds = timeseconds % (60*60); | |
} | |
int minutes = (int)(timeseconds / (60)); | |
if(minutes>0) | |
{ | |
builder.append("**").append(minutes).append("** minutes, "); | |
timeseconds = timeseconds % (60); | |
} | |
if(timeseconds>0) | |
builder.append("**").append(timeseconds).append("** seconds"); | |
String str = builder.toString(); | |
if(str.endsWith(", ")) | |
str = str.substring(0,str.length()-2); | |
if(str.equals("")) | |
str="**No time**"; | |
return str; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment