Initial Commit
Initial commit of project.
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package dataStructures;
|
||||
|
||||
//import core.DatabaseTrackedObject;
|
||||
|
||||
public class KittyChannel //extends DatabaseTrackedObject
|
||||
{
|
||||
public String uniqueID;
|
||||
public KittyGuild guild;
|
||||
|
||||
//public KittyRating rating;
|
||||
//public bool isVoice;
|
||||
|
||||
public KittyChannel(String uniqueID, KittyGuild guild)
|
||||
{
|
||||
//super(uniqueID);
|
||||
this.uniqueID = uniqueID;
|
||||
this.guild = guild;
|
||||
}
|
||||
|
||||
// @Override
|
||||
// public String Serialize()
|
||||
// {
|
||||
// return "";
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void DeSerialzie(String string)
|
||||
// {
|
||||
//
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package dataStructures;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import core.DatabaseTrackedObject;
|
||||
|
||||
// Context for a given guild for kittybot. Primarily designed to hold guild-specific settings.
|
||||
public class KittyGuild extends DatabaseTrackedObject
|
||||
{
|
||||
public String uniqueID;
|
||||
public KittyRating contentRating;
|
||||
public KittyUser guildOwner;
|
||||
public boolean polling;
|
||||
public String poll;
|
||||
public ArrayList<String> hasVoted = new ArrayList<String>();
|
||||
public ArrayList <KittyPoll> choices = new ArrayList<KittyPoll>();
|
||||
|
||||
private String commandIndicator;
|
||||
|
||||
// Default content for a guild
|
||||
public KittyGuild(String uniqueID)
|
||||
{
|
||||
super(uniqueID);
|
||||
|
||||
this.uniqueID = uniqueID;
|
||||
this.contentRating = KittyRating.Safe;
|
||||
this.polling = false;
|
||||
SetCommandIndicator("!");
|
||||
}
|
||||
|
||||
// Explicit constructor
|
||||
public KittyGuild(String commandIndicator, KittyRating contentRating, KittyUser guildOwner, String uniqueID)
|
||||
{
|
||||
super(uniqueID);
|
||||
|
||||
SetCommandIndicator(commandIndicator);
|
||||
this.contentRating = contentRating;
|
||||
this.polling = false;
|
||||
this.guildOwner = guildOwner;
|
||||
}
|
||||
|
||||
public String startPoll(String poll)
|
||||
{
|
||||
polling = true;
|
||||
this.poll = poll;
|
||||
return "Poll `" + poll + "` started! Don't forget to add choices with `!poll choice`~";
|
||||
}
|
||||
|
||||
public String addChoiceToPoll(String choice)
|
||||
{
|
||||
choices.add(new KittyPoll(choice));
|
||||
return "Added `" + choice + "` to poll!";
|
||||
}
|
||||
|
||||
public String endPoll()
|
||||
{
|
||||
choices.clear();
|
||||
hasVoted.clear();
|
||||
this.polling = false;
|
||||
poll = null;
|
||||
return "Poll ended!";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String Serialize()
|
||||
{
|
||||
return commandIndicator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DeSerialzie(String string)
|
||||
{
|
||||
if(string == null || string.length() == 0)
|
||||
SetCommandIndicator("!");
|
||||
else
|
||||
SetCommandIndicator(string);
|
||||
}
|
||||
|
||||
public String GetCommandIndicator()
|
||||
{
|
||||
return commandIndicator;
|
||||
}
|
||||
|
||||
public void SetCommandIndicator(String newIndicator)
|
||||
{
|
||||
if(newIndicator != commandIndicator)
|
||||
{
|
||||
commandIndicator = newIndicator;
|
||||
MarkDirty();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package dataStructures;
|
||||
|
||||
public class KittyPoll
|
||||
{
|
||||
public String choice;
|
||||
public int votes;
|
||||
|
||||
public KittyPoll(String choice)
|
||||
{
|
||||
this.choice = choice;
|
||||
votes = 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package dataStructures;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class KittyRP
|
||||
{
|
||||
ArrayList <Long> users = new ArrayList<Long>();
|
||||
ArrayList <String> log = new ArrayList <String> ();
|
||||
KittyChannel channel;
|
||||
long timer;
|
||||
|
||||
public KittyRP(ArrayList <KittyUser> users, KittyChannel channel)
|
||||
{
|
||||
for(int i = 0; i < users.size(); i++)
|
||||
{
|
||||
this.users.add(Long.parseLong(users.get(i).discordID));
|
||||
}
|
||||
this.channel = channel;
|
||||
timer = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public void addLine(KittyUser user, String line)
|
||||
{
|
||||
if(users.contains(Long.parseLong(user.discordID)))
|
||||
{
|
||||
log.add(user.name + " " + formatString(line));
|
||||
resetTimer();
|
||||
}
|
||||
}
|
||||
|
||||
private String formatString(String line)
|
||||
{
|
||||
if((line.charAt(0) == '_' && line.charAt(line.length()-1) == '_')
|
||||
||(line.charAt(0) == '*' && line.charAt(line.length()-1) == '*'))
|
||||
{
|
||||
line = line.substring(1,line.length()-1);
|
||||
}
|
||||
|
||||
return line;
|
||||
}
|
||||
|
||||
public File endRP(KittyUser user) throws FileNotFoundException, UnsupportedEncodingException
|
||||
{
|
||||
if(!users.contains(Long.parseLong(user.discordID)) && user.GetRole().getValue() < 2)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
PrintWriter writer = new PrintWriter("RP.txt", "UTF-8");
|
||||
while(!log.isEmpty())
|
||||
{
|
||||
writer.println(log.get(0));
|
||||
log.remove(0);
|
||||
}
|
||||
writer.close();
|
||||
return new File("RP.txt");
|
||||
}
|
||||
|
||||
public void resetTimer()
|
||||
{
|
||||
timer = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public long getTimer()
|
||||
{
|
||||
return timer;
|
||||
}
|
||||
|
||||
public KittyChannel getChannel()
|
||||
{
|
||||
return channel;
|
||||
}
|
||||
|
||||
public ArrayList <Long> getUsers()
|
||||
{
|
||||
return users;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package dataStructures;
|
||||
|
||||
public enum KittyRating
|
||||
{
|
||||
Safe(0), Filtered(1), Explicit(2);
|
||||
|
||||
private final int value;
|
||||
private KittyRating(int value)
|
||||
{
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public int getValue()
|
||||
{
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package dataStructures;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
|
||||
public enum KittyRole
|
||||
{
|
||||
Blacklisted (0), General(1), Mod(2), Admin(3), Dev(4);
|
||||
|
||||
private final int value;
|
||||
private KittyRole(int value)
|
||||
{
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public int getValue()
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
public static Optional<KittyRole> valueOf(int value)
|
||||
{
|
||||
return Arrays.stream(values()).filter(role -> role.value == value).findFirst();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package dataStructures;
|
||||
|
||||
import core.DatabaseTrackedObject;
|
||||
import utils.GlobalLog;
|
||||
import utils.LogFilter;
|
||||
|
||||
// NOTE(wisp): A user is a specific instance of a discord user on a given
|
||||
// discord server. This means that if a user is on two servers, they have
|
||||
// two unique user objects associated with them.
|
||||
public class KittyUser extends DatabaseTrackedObject
|
||||
{
|
||||
public KittyGuild guild;
|
||||
public String name;
|
||||
public String uniqueID;
|
||||
public String discordID;
|
||||
public String avatarID;
|
||||
private KittyRole role;
|
||||
private long beans;
|
||||
|
||||
// Explicit Constructor
|
||||
public KittyUser(String name, KittyGuild guild, KittyRole role, String uniqueID, String avatarID, String discordID)
|
||||
{
|
||||
super(uniqueID);
|
||||
this.name = name;
|
||||
this.role = role;
|
||||
this.guild = guild;
|
||||
this.uniqueID = uniqueID;
|
||||
this.beans = 0;
|
||||
this.avatarID = avatarID;
|
||||
this.discordID = discordID;
|
||||
}
|
||||
|
||||
// Can be positive or negative
|
||||
public void ChangeBeans(int amount)
|
||||
{
|
||||
beans += amount;
|
||||
|
||||
if(amount != 0)
|
||||
MarkDirty();
|
||||
}
|
||||
|
||||
public void ChangeRole(KittyRole newRole)
|
||||
{
|
||||
if(newRole != role)
|
||||
{
|
||||
role = newRole;
|
||||
MarkDirty();
|
||||
}
|
||||
}
|
||||
|
||||
public long GetBeans()
|
||||
{
|
||||
return beans;
|
||||
}
|
||||
|
||||
public KittyRole GetRole()
|
||||
{
|
||||
return role;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String Serialize()
|
||||
{
|
||||
return beans + "," + role.getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DeSerialzie(String string)
|
||||
{
|
||||
try
|
||||
{
|
||||
String[] strings = string.split(",");
|
||||
beans = Integer.parseInt(strings[0]);
|
||||
if(strings.length > 1)
|
||||
{
|
||||
role = KittyRole.valueOf(Integer.parseInt(strings[1])).get();
|
||||
}
|
||||
else
|
||||
{
|
||||
GlobalLog.Log(LogFilter.Database, "Upgrading user " + name + " to include 'role' in DB");
|
||||
// Mark ourselves dirty to re-write the role information stored in the user.
|
||||
// Just uses defaults from earlier again.
|
||||
MarkDirty();
|
||||
}
|
||||
}
|
||||
catch (NumberFormatException e)
|
||||
{
|
||||
GlobalLog.Warn(LogFilter.Database, "Invalid user data for user " + name + "! "
|
||||
+ "Starting over at 0 beans with a general role!");
|
||||
// We don't need to specify the role at this point because it is set at this point.
|
||||
// We use what the user was created with whatever defaults were in the factory.
|
||||
// Beans are maintained too, just in case there's some in cache.
|
||||
MarkDirty();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package dataStructures;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import net.dv8tion.jda.core.JDA;
|
||||
import net.dv8tion.jda.core.entities.TextChannel;
|
||||
import net.dv8tion.jda.core.events.message.guild.*;
|
||||
import utils.GlobalLog;
|
||||
import utils.LogFilter;
|
||||
|
||||
// NOTE(wisp): This isn't constructed with the factory at this time, mostly
|
||||
// because we need it to handle all the response queue behavior and everything
|
||||
// internally. So long as commands aren't exposed to the GuildMessageReceivedEvent
|
||||
// then we should be fine.
|
||||
public class Response
|
||||
{
|
||||
private GuildMessageReceivedEvent event;
|
||||
private JDA kitty;
|
||||
private final int discordMessageMax = 2000;
|
||||
private final int kittyMessageMax = 1950;
|
||||
|
||||
public Response(GuildMessageReceivedEvent event, JDA kitty)
|
||||
{
|
||||
this.event = event;
|
||||
this.kitty = kitty;
|
||||
}
|
||||
|
||||
public void Call(String toRespondWith)
|
||||
{
|
||||
GlobalLog.Log(LogFilter.Response, "Sending response: " + toRespondWith);
|
||||
if(toRespondWith.length() > discordMessageMax)
|
||||
{
|
||||
event.getChannel().sendMessage(toRespondWith.substring(0, kittyMessageMax) + "\n\nI think that's enough!").queue();
|
||||
}
|
||||
else
|
||||
{
|
||||
event.getChannel().sendMessage(toRespondWith).queue();
|
||||
}
|
||||
}
|
||||
|
||||
public void CallToChannel(String toRespondWith, String channelID)
|
||||
{
|
||||
TextChannel channel;
|
||||
channel = kitty.getTextChannelById(Long.parseLong(channelID));
|
||||
if(toRespondWith.length() > discordMessageMax)
|
||||
{
|
||||
channel.sendMessage(toRespondWith.substring(0, kittyMessageMax) + "\n\nI think that's enough!").queue();
|
||||
}
|
||||
else
|
||||
{
|
||||
channel.sendMessage(toRespondWith).queue();
|
||||
}
|
||||
}
|
||||
|
||||
public void CallImmediate(String toRespondWith)
|
||||
{
|
||||
GlobalLog.Log(LogFilter.Response, "Sending immediate response: " + toRespondWith);
|
||||
if(toRespondWith.length() > discordMessageMax)
|
||||
{
|
||||
event.getChannel().sendMessage(toRespondWith.substring(0, kittyMessageMax) + "\n\nI think that's enough!");
|
||||
}
|
||||
else
|
||||
{
|
||||
event.getChannel().sendMessage(toRespondWith);
|
||||
}
|
||||
}
|
||||
|
||||
// This is for responding with a file rather than a String
|
||||
public void CallFile(File toRespondWith, String extension)
|
||||
{
|
||||
GlobalLog.Log(LogFilter.Response, "Sending file response");
|
||||
event.getChannel().sendFile(toRespondWith, "return." + extension).queue();
|
||||
}
|
||||
|
||||
public void CallInput(InputStream in, String extension)
|
||||
{
|
||||
GlobalLog.Log(LogFilter.Response, "Sending input stream response");
|
||||
event.getChannel().sendFile(in, "return." + extension).queue();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package dataStructures;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import core.ObjectBuilderFactory;
|
||||
import net.dv8tion.jda.core.entities.Member;
|
||||
import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent;
|
||||
|
||||
public class UserInput
|
||||
{
|
||||
// Variables
|
||||
public String key;
|
||||
public String args;
|
||||
public KittyUser [] mentions;
|
||||
private boolean isValid;
|
||||
public String message;
|
||||
|
||||
// NOTE(wisp): Designed to parse a string as it's constructed.
|
||||
// If a user enters the command "!ping some stuff here :D"
|
||||
// and if the command indicator is !, then...
|
||||
//
|
||||
// key=ping
|
||||
// args=some stuff here :D
|
||||
//
|
||||
// The CommandIndicator and spaces between the key and args are dropped.
|
||||
public UserInput(GuildMessageReceivedEvent event, KittyGuild guildContext)
|
||||
{
|
||||
String toParse = event.getMessage().getContentRaw();
|
||||
message = toParse;
|
||||
|
||||
key = "";
|
||||
args = "";
|
||||
|
||||
if(toParse.length() <= 0)
|
||||
return;
|
||||
|
||||
if(!event.getMessage().getMentionedMembers().isEmpty())
|
||||
mentions = FindMentionedUsers(event);
|
||||
|
||||
toParse = toParse.trim();
|
||||
String commandIndicator = guildContext.GetCommandIndicator();
|
||||
if(toParse.startsWith(commandIndicator))
|
||||
{
|
||||
int loc = FindFirstWhitespace(toParse);
|
||||
|
||||
if(loc <= 0)
|
||||
{
|
||||
key = toParse.substring(commandIndicator.length()).trim().toLowerCase();
|
||||
}
|
||||
else
|
||||
{
|
||||
key = toParse.substring(commandIndicator.length(), loc).trim().toLowerCase();
|
||||
args = toParse.substring(loc).trim();
|
||||
}
|
||||
|
||||
isValid = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
isValid = false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean IsValid()
|
||||
{
|
||||
return isValid;
|
||||
}
|
||||
|
||||
// Finds first whitespace in the string
|
||||
private int FindFirstWhitespace(String str)
|
||||
{
|
||||
for (int i = 0; i < str.length(); ++i)
|
||||
{
|
||||
if (Character.isWhitespace(str.charAt(i)))
|
||||
return i;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
private KittyUser[] FindMentionedUsers(GuildMessageReceivedEvent event)
|
||||
{
|
||||
List <Member> JDAMentions = event.getMessage().getMentionedMembers();
|
||||
KittyUser [] KittyMentions = new KittyUser[JDAMentions.size()];
|
||||
for(int i = 0; i < KittyMentions.length; i ++)
|
||||
{
|
||||
KittyMentions [i] = ObjectBuilderFactory.getCachedUser(event.getGuild().getId(), JDAMentions.get(i).getUser().getId());
|
||||
}
|
||||
return KittyMentions;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user