Compare commits
No commits in common. "a48daf7c8d7acee75773240be31661a99a7d60e1" and "59a63b724a4269e1ae42124677228a3771303d74" have entirely different histories.
a48daf7c8d
...
59a63b724a
@ -1,15 +0,0 @@
|
|||||||
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,12 +5,9 @@ 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
|
||||||
{
|
{
|
||||||
@ -19,8 +16,6 @@ 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);
|
||||||
@ -35,30 +30,20 @@ public class HidekoBot
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// load token from args
|
|
||||||
botToken = args[0];
|
botToken = args[0];
|
||||||
|
|
||||||
// if there are more than 1 args, then iterate through them because we have additional things to do
|
JDABuilder jdaBuilder;
|
||||||
if(args.length > 1) {
|
JDA jda;
|
||||||
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 = JDABuilder.createDefault(botToken);
|
jdaBuilder = JDABuilder.createDefault(botToken);
|
||||||
jdaBuilder.setActivity(Activity.playing("the piano"));
|
jdaBuilder.setActivity(Activity.playing("the piano"));
|
||||||
|
|
||||||
// enable necessary intents.
|
jdaBuilder.enableIntents(GatewayIntent.MESSAGE_CONTENT,
|
||||||
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)
|
||||||
@ -71,10 +56,6 @@ 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);
|
||||||
|
|
||||||
@ -82,10 +63,9 @@ 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
|
||||||
public static JDA getAPI()
|
jda.addEventListener(new MessageListener());
|
||||||
{
|
|
||||||
return jda;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,9 +4,7 @@ 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
|
||||||
{
|
{
|
||||||
@ -16,28 +14,11 @@ 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,47 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,29 +0,0 @@
|
|||||||
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