Add coin-flip with random utils class and allow verbosity as arg
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
a032712450
commit
a48daf7c8d
@ -5,9 +5,12 @@ import net.dv8tion.jda.api.JDABuilder;
|
|||||||
import net.dv8tion.jda.api.entities.Activity;
|
import net.dv8tion.jda.api.entities.Activity;
|
||||||
import net.dv8tion.jda.api.requests.GatewayIntent;
|
import net.dv8tion.jda.api.requests.GatewayIntent;
|
||||||
import wtf.beatrice.hidekobot.listeners.MessageListener;
|
import wtf.beatrice.hidekobot.listeners.MessageListener;
|
||||||
|
import wtf.beatrice.hidekobot.listeners.MessageLogger;
|
||||||
import wtf.beatrice.hidekobot.utils.Logger;
|
import wtf.beatrice.hidekobot.utils.Logger;
|
||||||
|
|
||||||
import javax.security.auth.login.LoginException;
|
import javax.security.auth.login.LoginException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class HidekoBot
|
public class HidekoBot
|
||||||
{
|
{
|
||||||
@ -16,6 +19,8 @@ public class HidekoBot
|
|||||||
private static String botUserId;
|
private static String botUserId;
|
||||||
private static final String version = "0.0.1"; // we should probably find a way to make this consistent with Maven
|
private static final String version = "0.0.1"; // we should probably find a way to make this consistent with Maven
|
||||||
|
|
||||||
|
private static JDA jda;
|
||||||
|
|
||||||
|
|
||||||
// create a logger instance for ease of use
|
// create a logger instance for ease of use
|
||||||
private static final Logger logger = new Logger(HidekoBot.class);
|
private static final Logger logger = new Logger(HidekoBot.class);
|
||||||
@ -30,20 +35,30 @@ public class HidekoBot
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// load token from args
|
||||||
botToken = args[0];
|
botToken = args[0];
|
||||||
|
|
||||||
JDABuilder jdaBuilder;
|
// if there are more than 1 args, then iterate through them because we have additional things to do
|
||||||
JDA jda;
|
if(args.length > 1) {
|
||||||
|
List<String> argsList = new ArrayList<>();
|
||||||
|
for(int i = 1; i < args.length; i++)
|
||||||
|
{ argsList.add(args[i]); }
|
||||||
|
|
||||||
|
if(argsList.contains("verbose")) Configuration.setVerbose(true);
|
||||||
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// try to create the bot object and authenticate it with discord.
|
// try to create the bot object and authenticate it with discord.
|
||||||
jdaBuilder = JDABuilder.createDefault(botToken);
|
JDABuilder jdaBuilder = JDABuilder.createDefault(botToken);
|
||||||
jdaBuilder.setActivity(Activity.playing("the piano"));
|
jdaBuilder.setActivity(Activity.playing("the piano"));
|
||||||
|
|
||||||
jdaBuilder.enableIntents(GatewayIntent.MESSAGE_CONTENT,
|
// enable necessary intents.
|
||||||
|
jdaBuilder.enableIntents(
|
||||||
|
GatewayIntent.MESSAGE_CONTENT,
|
||||||
GatewayIntent.DIRECT_MESSAGES,
|
GatewayIntent.DIRECT_MESSAGES,
|
||||||
GatewayIntent.GUILD_MESSAGES);
|
GatewayIntent.GUILD_MESSAGES
|
||||||
|
);
|
||||||
|
|
||||||
jda = jdaBuilder.build().awaitReady();
|
jda = jdaBuilder.build().awaitReady();
|
||||||
} catch (LoginException | InterruptedException e)
|
} catch (LoginException | InterruptedException e)
|
||||||
@ -56,6 +71,10 @@ public class HidekoBot
|
|||||||
botUserId = jda.getSelfUser().getId();
|
botUserId = jda.getSelfUser().getId();
|
||||||
standardInviteLink = standardInviteLink.replace("%userid%", botUserId);
|
standardInviteLink = standardInviteLink.replace("%userid%", botUserId);
|
||||||
|
|
||||||
|
// register listeners
|
||||||
|
jda.addEventListener(new MessageListener());
|
||||||
|
if(Configuration.isVerbose()) jda.addEventListener(new MessageLogger());
|
||||||
|
|
||||||
// print the bot logo.
|
// print the bot logo.
|
||||||
logger.log("Ready!\n\n" + logger.getLogo() + "\nv" + version + " - bot is ready!\n", 2);
|
logger.log("Ready!\n\n" + logger.getLogo() + "\nv" + version + " - bot is ready!\n", 2);
|
||||||
|
|
||||||
@ -63,9 +82,10 @@ public class HidekoBot
|
|||||||
logger.log("Bot User ID: " + botUserId, 4);
|
logger.log("Bot User ID: " + botUserId, 4);
|
||||||
logger.log("Invite Link: " + standardInviteLink, 5);
|
logger.log("Invite Link: " + standardInviteLink, 5);
|
||||||
|
|
||||||
// register listeners
|
}
|
||||||
jda.addEventListener(new MessageListener());
|
public static JDA getAPI()
|
||||||
|
{
|
||||||
|
return jda;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,9 @@ import net.dv8tion.jda.api.entities.*;
|
|||||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import wtf.beatrice.hidekobot.Configuration;
|
||||||
import wtf.beatrice.hidekobot.utils.Logger;
|
import wtf.beatrice.hidekobot.utils.Logger;
|
||||||
|
import wtf.beatrice.hidekobot.utils.RandomUtil;
|
||||||
|
|
||||||
public class MessageListener extends ListenerAdapter
|
public class MessageListener extends ListenerAdapter
|
||||||
{
|
{
|
||||||
@ -14,11 +16,28 @@ public class MessageListener extends ListenerAdapter
|
|||||||
@Override
|
@Override
|
||||||
public void onMessageReceived(@NotNull MessageReceivedEvent event)
|
public void onMessageReceived(@NotNull MessageReceivedEvent event)
|
||||||
{
|
{
|
||||||
|
|
||||||
if(event.getMessage().getContentDisplay().equalsIgnoreCase("ping"))
|
if(event.getMessage().getContentDisplay().equalsIgnoreCase("ping"))
|
||||||
{
|
{
|
||||||
MessageChannel channel = event.getChannel();
|
MessageChannel channel = event.getChannel();
|
||||||
channel.sendMessage("Pong!").queue();
|
channel.sendMessage("Pong!").queue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(event.getMessage().getContentDisplay().equalsIgnoreCase("flip a coin"))
|
||||||
|
{
|
||||||
|
MessageChannel channel = event.getChannel();
|
||||||
|
|
||||||
|
int rand = RandomUtil.getRandomNumber(0, 1);
|
||||||
|
String msg;
|
||||||
|
|
||||||
|
if(rand == 1)
|
||||||
|
{
|
||||||
|
msg = ":coin: It's **Heads**!";
|
||||||
|
} else {
|
||||||
|
msg = "It's **Tails**! :coin:";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
channel.sendMessage(msg).queue();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,2 +1,29 @@
|
|||||||
package wtf.beatrice.hidekobot.utils;public class RandomUtil {
|
package wtf.beatrice.hidekobot.utils;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class RandomUtil
|
||||||
|
{
|
||||||
|
private static final Random random = new Random();
|
||||||
|
|
||||||
|
public static int getRandomNumber(int min, int max)
|
||||||
|
{
|
||||||
|
if(min == max) return min; // dumbass
|
||||||
|
if(min > max) // swap em
|
||||||
|
{
|
||||||
|
min = min - max;
|
||||||
|
max = min + max;
|
||||||
|
min = max - min;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// find our range of randomness (eg. 5 -> 8 = 4), add +1 since we want to be inclusive at both sides
|
||||||
|
int difference = (max - min) + 1;
|
||||||
|
|
||||||
|
// find a number between 0 and our range (eg. 5 -> 8 = 0 -> 3)
|
||||||
|
int randomTemp = random.nextInt(difference);
|
||||||
|
|
||||||
|
// add the minimum value, so we are sure to be in the original range (0->5, 1->6, 2->7, 3->8)
|
||||||
|
return randomTemp + min;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user