From 75d0d701ab34432ee1d2d82fdd30254edaa4e3ab Mon Sep 17 00:00:00 2001 From: Matthew Cech Date: Sun, 14 Apr 2019 23:58:47 -0700 Subject: [PATCH 1/3] + Added alpha implementation for overlay images --- src/utils/ImageOverlayBuilder.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/utils/ImageOverlayBuilder.java b/src/utils/ImageOverlayBuilder.java index 2ad0546..068e95d 100644 --- a/src/utils/ImageOverlayBuilder.java +++ b/src/utils/ImageOverlayBuilder.java @@ -5,9 +5,9 @@ import java.awt.image.DataBufferByte; import java.awt.image.RenderedImage; import java.io.File; import java.io.IOException; +import java.nio.ByteBuffer; import java.util.Calendar; import java.util.Iterator; - import javax.imageio.IIOException; import javax.imageio.IIOImage; import javax.imageio.ImageIO; @@ -169,6 +169,7 @@ public class ImageOverlayBuilder final int y = ((i / PIXEL_BYTE_LENGTH) / baseWidth); // If we're within the bounds of where the overlay picture should go... + // ...then get bytes and manually apply alpha overlay. if(x > left && x < right && y > top && y < bottom) { final int overlayX = x - left; @@ -178,8 +179,21 @@ public class ImageOverlayBuilder if(overlayX < 0 || overlayY < 0 || overlayX >= baseWidth || overlayY >= baseHeight) continue; + byte[] overlayBytes = ByteBuffer.allocate(4).putInt(imageOverlay.getRGB(overlayX, overlayY)).array(); + byte[] baseBytes = ByteBuffer.allocate(4).putInt(imageBase.getRGB(x, y)).array(); + float overlayT = (overlayBytes[0] & 0xFF) / 255.0f; + + // Java has a restriction where we can't actually manupulate unsigned bytes apparently(?) so + // what this does is convert the specific bytes to ints in order to manipuate them, then takes the + // byte buffer and parses the far right byte out of the int after manipulating the value. + byte[] output = new byte[4]; + output[0] = (byte)0b11111111; + output[1] = ByteBuffer.allocate(4).putInt((int) ((baseBytes[1] & 0xFF) - (overlayT * ((baseBytes[1] & 0xFF) - (overlayBytes[1] & 0xFF))))).array()[3]; + output[2] = ByteBuffer.allocate(4).putInt((int) ((baseBytes[2] & 0xFF) - (overlayT * ((baseBytes[2] & 0xFF) - (overlayBytes[2] & 0xFF))))).array()[3]; + output[3] = ByteBuffer.allocate(4).putInt((int) ((baseBytes[3] & 0xFF) - (overlayT * ((baseBytes[3] & 0xFF) - (overlayBytes[3] & 0xFF))))).array()[3]; + // Re-write bytes as overlay - imageBase.setRGB(x, y, imageOverlay.getRGB(overlayX, overlayY)); + imageBase.setRGB(x, y, ByteBuffer.wrap(output).getInt()); } } From 9f4aac3e219c42f4f61bd91942606b11de7617e4 Mon Sep 17 00:00:00 2001 From: Alex Stewart Date: Mon, 15 Apr 2019 00:14:14 -0700 Subject: [PATCH 2/3] +Added Discord Role System Added system to allow users to edit roles that the admins of each server gives access to --- commands.config | 2 + locCommands.config | 4 +- locStrings.config | 13 +++++- src/commands/CommandAddGuildRole.java | 40 ++++++++++++++++++ src/commands/CommandAllowedGuildRole.java | 34 +++++++++++++++ src/core/ObjectBuilderFactory.java | 51 +++++++++++++++++++++-- src/dataStructures/KittyGuild.java | 9 ++-- src/utils/AdminControl.java | 29 +++++++++++++ 8 files changed, 173 insertions(+), 9 deletions(-) create mode 100644 src/commands/CommandAddGuildRole.java create mode 100644 src/commands/CommandAllowedGuildRole.java create mode 100644 src/utils/AdminControl.java diff --git a/commands.config b/commands.config index 4d9a56c..56f8bd9 100644 --- a/commands.config +++ b/commands.config @@ -33,3 +33,5 @@ help=1 buildHelp=1 invite=1 shutdown=1 +addguildrole=1 +allowedguildrole=1 diff --git a/locCommands.config b/locCommands.config index f60a1b9..dbb7feb 100644 --- a/locCommands.config +++ b/locCommands.config @@ -12,9 +12,11 @@ choose= poll= rpstart= bet= +addguildrole= perish, thenperish= teey= stats= +allowedguildrole= beans= eightball, 8ball= vote= @@ -35,4 +37,4 @@ buildHelp= invite= shutdown= - + diff --git a/locStrings.config b/locStrings.config index d0ce6f8..d64a2a4 100644 --- a/locStrings.config +++ b/locStrings.config @@ -49,6 +49,11 @@ StarkInfo=Snaps your icon or the icon of a friend you mentioned [CommandYeet] YeetInfo=Yeet yourself or yeet a friend with @! +[CommandAllowedGuildRole] +AllowedGuildRoleInfo= +AllowedGuildRoleDuplicate=Can't add the same role twice! +AllowedGuildRoleSuccess=Added %s to the allowed roles! + [CommandPollVote] PollVoteNotValidNumber=That's not a vaild number! PollVoteInfo=Vote in a poll with the choice number, won't work if no poll is running, you can't change your vote once you have cast it! Be careful! @@ -166,6 +171,12 @@ WolframError=Something went wrong! WolframInfo=Will query wolframalpha with your question and give a full image output of the answer WolframNoArgs=You need to provide some arguments! +[CommandAddGuildRole] +AddGuildRoleInfo= +AddGuildRoleNotAllowed=You are not allowed to add %s! +AddGuildRoleSuccess=Added %s to %s +AddGuildRoleFailure=Failed to add %s to %s + [CommandMap] MapInfo=Generates a map! You can pass additional information if you want with the flags `-s-w-h`. If one of the fields isn't provided, its default will be used. Note that adjusting the width and height impacts the map outcomes.\n\nDefault seed: Random,\nDefault Width: 35(max %s),\nDefault Height: 25(max %s) MapSeed=Seed @@ -174,4 +185,4 @@ MapVersion=Using mapgen v0.1 MapWidth=Width MapHeight=Height - + diff --git a/src/commands/CommandAddGuildRole.java b/src/commands/CommandAddGuildRole.java new file mode 100644 index 0000000..3c20873 --- /dev/null +++ b/src/commands/CommandAddGuildRole.java @@ -0,0 +1,40 @@ +package commands; + +import core.Command; +import core.LocStrings; +import dataStructures.KittyChannel; +import dataStructures.KittyGuild; +import dataStructures.KittyRating; +import dataStructures.KittyRole; +import dataStructures.KittyUser; +import dataStructures.Response; +import dataStructures.UserInput; + +public class CommandAddGuildRole extends Command +{ + public CommandAddGuildRole(KittyRole level, KittyRating rating) { super(level, rating); } + + @Override + public String HelpText() { return LocStrings.Stub("AddGuildRoleInfo"); }; + + @Override + public void OnRun(KittyGuild guild, KittyChannel channel, KittyUser user, UserInput input, Response res) + { + String role = input.args.split(" ")[0]; + if(guild.allowedRole.contains(role)) + { + if(guild.control.addRole(user.discordID, role)) + { + res.Call(String.format(LocStrings.Stub("AddGuildRoleSuccess"), role, user.name)); + } + else + { + res.Call(String.format(LocStrings.Stub("AddGuildRoleFailure"), role, user.name)); + } + } + else + { + res.Call(String.format(LocStrings.Stub("AddGuildRoleNotAllowed"), role)); + } + } +} \ No newline at end of file diff --git a/src/commands/CommandAllowedGuildRole.java b/src/commands/CommandAllowedGuildRole.java new file mode 100644 index 0000000..e752e26 --- /dev/null +++ b/src/commands/CommandAllowedGuildRole.java @@ -0,0 +1,34 @@ +package commands; + +import core.Command; +import core.LocStrings; +import dataStructures.KittyChannel; +import dataStructures.KittyGuild; +import dataStructures.KittyRating; +import dataStructures.KittyRole; +import dataStructures.KittyUser; +import dataStructures.Response; +import dataStructures.UserInput; + +public class CommandAllowedGuildRole extends Command +{ + public CommandAllowedGuildRole(KittyRole level, KittyRating rating) { super(level, rating); } + + @Override + public String HelpText() { return LocStrings.Stub("AllowedGuildRoleInfo"); } + + @Override + public void OnRun(KittyGuild guild, KittyChannel channel, KittyUser user, UserInput input, Response res) + { + String role = input.args.split(" ")[0]; + if(guild.allowedRole.contains(role)) + { + res.Call(LocStrings.Stub("AllowedGuildRoleDuplicate")); + } + else + { + guild.allowedRole.add(role); + res.Call(String.format(LocStrings.Stub("AllowedGuildRoleSuccess"), role)); + } + } +} \ No newline at end of file diff --git a/src/core/ObjectBuilderFactory.java b/src/core/ObjectBuilderFactory.java index 730ddaf..a186f28 100644 --- a/src/core/ObjectBuilderFactory.java +++ b/src/core/ObjectBuilderFactory.java @@ -3,10 +3,51 @@ package core; import java.util.HashMap; import java.util.concurrent.Semaphore; -import commands.*; -import dataStructures.*; +import commands.CommandAddGuildRole; +import commands.CommandAllowedGuildRole; +import commands.CommandBeansShow; +import commands.CommandBetBeans; +import commands.CommandBlurry; +import commands.CommandBoop; +import commands.CommandChangeIndicator; +import commands.CommandChoose; +import commands.CommandColiru; +import commands.CommandDoWork; +import commands.CommandEightBall; +import commands.CommandGiveBeans; +import commands.CommandHelp; +import commands.CommandHelpBuilder; +import commands.CommandInfo; +import commands.CommandInvite; +import commands.CommandJDoodle; +import commands.CommandMap; +import commands.CommandPerish; +import commands.CommandPing; +import commands.CommandPollManage; +import commands.CommandPollResults; +import commands.CommandPollShow; +import commands.CommandPollVote; +import commands.CommandRPEnd; +import commands.CommandRPG; +import commands.CommandRPStart; +import commands.CommandRating; +import commands.CommandRole; +import commands.CommandRoll; +import commands.CommandShutdown; +import commands.CommandStark; +import commands.CommandStats; +import commands.CommandTeey; +import commands.CommandTweet; +import commands.CommandWolfram; +import commands.CommandYeet; +import dataStructures.KittyChannel; +import dataStructures.KittyGuild; +import dataStructures.KittyRating; +import dataStructures.KittyRole; +import dataStructures.KittyUser; import net.dv8tion.jda.core.entities.Member; import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent; +import utils.AdminControl; import utils.GlobalLog; import utils.LogFilter; @@ -95,7 +136,7 @@ public class ObjectBuilderFactory // look up the guild. String uid = event.getGuild().getId(); - // ince we're lazily initialized, we can synchronize w/ the + // once we're lazily initialized, we can synchronize w/ the // guildCache object now instead of having to use a mutex. KittyGuild guild = null; synchronized (guildCache) @@ -108,7 +149,7 @@ public class ObjectBuilderFactory else { // Construct a new guild with defaults - guild = new KittyGuild(uid); + guild = new KittyGuild(uid, new AdminControl(event.getGuild())); DatabaseManager.instance.Register(guild); guildCache.put(uid, guild); } @@ -322,11 +363,13 @@ public class ObjectBuilderFactory manager.Register(LocCommands.Stub("rating"), new CommandRating(KittyRole.Admin, KittyRating.Safe)); manager.Register(LocCommands.Stub("indicator"), new CommandChangeIndicator(KittyRole.Admin, KittyRating.Safe)); + manager.Register(LocCommands.Stub("allowedguildrole"), new CommandAllowedGuildRole(KittyRole.Admin, KittyRating.Safe)); manager.Register(LocCommands.Stub("poll"), new CommandPollManage(KittyRole.Mod, KittyRating.Safe)); manager.Register(LocCommands.Stub("givebeans"), new CommandGiveBeans(KittyRole.Mod, KittyRating.Safe)); manager.Register(LocCommands.Stub("rpg"), new CommandRPG(KittyRole.Mod, KittyRating.Safe)); + manager.Register(LocCommands.Stub("addguildrole"), new CommandAddGuildRole(KittyRole.General, KittyRating.Safe)); manager.Register(LocCommands.Stub("teey"), new CommandTeey(KittyRole.General, KittyRating.Safe)); manager.Register(LocCommands.Stub("perish, thenperish"), new CommandPerish(KittyRole.General, KittyRating.Safe)); manager.Register(LocCommands.Stub("yeet"), new CommandYeet(KittyRole.General, KittyRating.Safe)); diff --git a/src/dataStructures/KittyGuild.java b/src/dataStructures/KittyGuild.java index c35791e..4a9ad22 100644 --- a/src/dataStructures/KittyGuild.java +++ b/src/dataStructures/KittyGuild.java @@ -2,6 +2,7 @@ package dataStructures; import java.util.ArrayList; import core.DatabaseTrackedObject; +import utils.AdminControl; // Context for a given guild for kittybot. Primarily designed to hold guild-specific settings. public class KittyGuild extends DatabaseTrackedObject @@ -12,17 +13,19 @@ public class KittyGuild extends DatabaseTrackedObject public boolean polling; public String poll; public ArrayList hasVoted = new ArrayList(); + public ArrayList allowedRole = new ArrayList(); public ArrayList choices = new ArrayList(); + public AdminControl control; private String commandIndicator; // Default content for a guild - public KittyGuild(String uniqueID) + public KittyGuild(String uniqueID, AdminControl adminControl) { super(uniqueID); - + control = adminControl; this.uniqueID = uniqueID; - this.contentRating = KittyRating.Safe; + this.contentRating = KittyRating.Safe; this.polling = false; SetCommandIndicator("!"); } diff --git a/src/utils/AdminControl.java b/src/utils/AdminControl.java new file mode 100644 index 0000000..7124f75 --- /dev/null +++ b/src/utils/AdminControl.java @@ -0,0 +1,29 @@ +package utils; + +import net.dv8tion.jda.core.entities.Guild; +import net.dv8tion.jda.core.managers.GuildController; + +public class AdminControl +{ + private GuildController guildCon; + private Guild guild; + + public AdminControl(Guild guild) + { + this.guild = guild; + guildCon = guild.getController(); + } + + public boolean addRole(String memberID, String roleName) + { + try + { + guildCon.addRolesToMember(guild.getMemberById(memberID), guild.getRolesByName(roleName, true)).complete(); + } + catch(Exception e) + { + return false; + } + return true; + } +} From 469fe0922eaf9c86f7ba1662936c7c8bee9df03d Mon Sep 17 00:00:00 2001 From: Alex Stewart Date: Mon, 15 Apr 2019 14:57:11 -0700 Subject: [PATCH 3/3] =Added array input to AllowedGuildRole Users can now enter in multiple roles by separating them with a comma in the command --- locStrings.config | 2 +- src/commands/CommandAllowedGuildRole.java | 21 +++++++++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/locStrings.config b/locStrings.config index d64a2a4..cc2fd23 100644 --- a/locStrings.config +++ b/locStrings.config @@ -172,7 +172,7 @@ WolframInfo=Will query wolframalpha with your question and give a full image out WolframNoArgs=You need to provide some arguments! [CommandAddGuildRole] -AddGuildRoleInfo= +AddGuildRoleInfo=Add a role to yourself! AddGuildRoleNotAllowed=You are not allowed to add %s! AddGuildRoleSuccess=Added %s to %s AddGuildRoleFailure=Failed to add %s to %s diff --git a/src/commands/CommandAllowedGuildRole.java b/src/commands/CommandAllowedGuildRole.java index e752e26..f71697e 100644 --- a/src/commands/CommandAllowedGuildRole.java +++ b/src/commands/CommandAllowedGuildRole.java @@ -20,15 +20,20 @@ public class CommandAllowedGuildRole extends Command @Override public void OnRun(KittyGuild guild, KittyChannel channel, KittyUser user, UserInput input, Response res) { - String role = input.args.split(" ")[0]; - if(guild.allowedRole.contains(role)) + String [] roles = input.args.split(","); + String role; + for(int i = 0; i < roles.length; i++) { - res.Call(LocStrings.Stub("AllowedGuildRoleDuplicate")); - } - else - { - guild.allowedRole.add(role); - res.Call(String.format(LocStrings.Stub("AllowedGuildRoleSuccess"), role)); + role = roles[i].trim(); + if(guild.allowedRole.contains(role)) + { + res.Call(LocStrings.Stub("AllowedGuildRoleDuplicate")); + } + else + { + guild.allowedRole.add(role); + res.Call(String.format(LocStrings.Stub("AllowedGuildRoleSuccess"), role)); + } } } } \ No newline at end of file