Compare commits
2 Commits
59a63b724a
...
a48daf7c8d
Author | SHA1 | Date | |
---|---|---|---|
a48daf7c8d | |||
a032712450 |
15
src/main/java/wtf/beatrice/hidekobot/Configuration.java
Normal file
15
src/main/java/wtf/beatrice/hidekobot/Configuration.java
Normal file
@ -0,0 +1,15 @@
|
||||
package wtf.beatrice.hidekobot;
|
||||
|
||||
public class Configuration
|
||||
{
|
||||
|
||||
|
||||
private static boolean verbose = false;
|
||||
|
||||
|
||||
public static boolean isVerbose() { return verbose; }
|
||||
|
||||
// WARNING: verbosity spams the logs a LOT!
|
||||
public static void setVerbose(boolean v) { verbose = v; }
|
||||
|
||||
}
|
@ -5,9 +5,12 @@ import net.dv8tion.jda.api.JDABuilder;
|
||||
import net.dv8tion.jda.api.entities.Activity;
|
||||
import net.dv8tion.jda.api.requests.GatewayIntent;
|
||||
import wtf.beatrice.hidekobot.listeners.MessageListener;
|
||||
import wtf.beatrice.hidekobot.listeners.MessageLogger;
|
||||
import wtf.beatrice.hidekobot.utils.Logger;
|
||||
|
||||
import javax.security.auth.login.LoginException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class HidekoBot
|
||||
{
|
||||
@ -16,6 +19,8 @@ public class HidekoBot
|
||||
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 JDA jda;
|
||||
|
||||
|
||||
// create a logger instance for ease of use
|
||||
private static final Logger logger = new Logger(HidekoBot.class);
|
||||
@ -30,20 +35,30 @@ public class HidekoBot
|
||||
return;
|
||||
}
|
||||
|
||||
// load token from args
|
||||
botToken = args[0];
|
||||
|
||||
JDABuilder jdaBuilder;
|
||||
JDA jda;
|
||||
// if there are more than 1 args, then iterate through them because we have additional things to do
|
||||
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 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.enableIntents(GatewayIntent.MESSAGE_CONTENT,
|
||||
// enable necessary intents.
|
||||
jdaBuilder.enableIntents(
|
||||
GatewayIntent.MESSAGE_CONTENT,
|
||||
GatewayIntent.DIRECT_MESSAGES,
|
||||
GatewayIntent.GUILD_MESSAGES);
|
||||
GatewayIntent.GUILD_MESSAGES
|
||||
);
|
||||
|
||||
jda = jdaBuilder.build().awaitReady();
|
||||
} catch (LoginException | InterruptedException e)
|
||||
@ -56,6 +71,10 @@ public class HidekoBot
|
||||
botUserId = jda.getSelfUser().getId();
|
||||
standardInviteLink = standardInviteLink.replace("%userid%", botUserId);
|
||||
|
||||
// register listeners
|
||||
jda.addEventListener(new MessageListener());
|
||||
if(Configuration.isVerbose()) jda.addEventListener(new MessageLogger());
|
||||
|
||||
// print the bot logo.
|
||||
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("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.hooks.ListenerAdapter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import wtf.beatrice.hidekobot.Configuration;
|
||||
import wtf.beatrice.hidekobot.utils.Logger;
|
||||
import wtf.beatrice.hidekobot.utils.RandomUtil;
|
||||
|
||||
public class MessageListener extends ListenerAdapter
|
||||
{
|
||||
@ -14,11 +16,28 @@ public class MessageListener extends ListenerAdapter
|
||||
@Override
|
||||
public void onMessageReceived(@NotNull MessageReceivedEvent event)
|
||||
{
|
||||
|
||||
if(event.getMessage().getContentDisplay().equalsIgnoreCase("ping"))
|
||||
{
|
||||
MessageChannel channel = event.getChannel();
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,47 @@
|
||||
package wtf.beatrice.hidekobot.listeners;
|
||||
|
||||
import net.dv8tion.jda.api.entities.PrivateChannel;
|
||||
import net.dv8tion.jda.api.entities.TextChannel;
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import wtf.beatrice.hidekobot.utils.Logger;
|
||||
|
||||
public class MessageLogger extends ListenerAdapter
|
||||
{
|
||||
// this class only gets loaded as a listener if verbosity is set to true on startup.
|
||||
|
||||
|
||||
private final static String guildChannelFormat = "[%guild%] [#%channel%] %user%: %message%";
|
||||
private final static String dmFormat = "[DM] %user%: %message%";
|
||||
|
||||
private final Logger logger = new Logger(MessageLogger.class);
|
||||
|
||||
@Override
|
||||
public void onMessageReceived(@NotNull MessageReceivedEvent event)
|
||||
{
|
||||
String toLog = "";
|
||||
String userName = event.getAuthor().getName();
|
||||
String message = event.getMessage().getContentDisplay();
|
||||
|
||||
if(event.getChannel() instanceof TextChannel)
|
||||
{
|
||||
String guildName = ((TextChannel) event.getChannel()).getGuild().getName();
|
||||
String channelName = event.getChannel().getName();
|
||||
|
||||
toLog = guildChannelFormat
|
||||
.replace("%guild%", guildName)
|
||||
.replace("%channel%", channelName);
|
||||
}
|
||||
else if(event.getChannel() instanceof PrivateChannel)
|
||||
{
|
||||
toLog = dmFormat;
|
||||
}
|
||||
|
||||
toLog = toLog
|
||||
.replace("%user%", userName)
|
||||
.replace("%message%", message);
|
||||
|
||||
logger.log(toLog);
|
||||
}
|
||||
}
|
29
src/main/java/wtf/beatrice/hidekobot/utils/RandomUtil.java
Normal file
29
src/main/java/wtf/beatrice/hidekobot/utils/RandomUtil.java
Normal file
@ -0,0 +1,29 @@
|
||||
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