League Of Discord, initial commit.
parent
bb1fc6da4a
commit
61aaf6fb28
@ -0,0 +1,122 @@
|
|||||||
|
package net.pingex.dbotm;
|
||||||
|
|
||||||
|
import com.robrua.orianna.api.core.RiotAPI;
|
||||||
|
import com.robrua.orianna.type.core.champion.ChampionStatus;
|
||||||
|
import com.robrua.orianna.type.core.common.Region;
|
||||||
|
import com.robrua.orianna.type.core.staticdata.Champion;
|
||||||
|
import net.pingex.discordbot.AbstractModule;
|
||||||
|
import net.pingex.discordbot.Command;
|
||||||
|
import net.pingex.discordbot.Configuration;
|
||||||
|
import net.pingex.discordbot.Controllable;
|
||||||
|
import sx.blah.discord.api.IDiscordClient;
|
||||||
|
import sx.blah.discord.handle.impl.events.MessageReceivedEvent;
|
||||||
|
import sx.blah.discord.util.DiscordException;
|
||||||
|
import sx.blah.discord.util.HTTP429Exception;
|
||||||
|
import sx.blah.discord.util.MissingPermissionsException;
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* League of Discord, all the stuff related to League of Legends !
|
||||||
|
* @version 0.1-dev
|
||||||
|
* @author Raphael "Pingex" NAAS
|
||||||
|
*/
|
||||||
|
@Controllable(name = "lol")
|
||||||
|
public class LeagueOfDiscordModule extends AbstractModule
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Loading Splace Image Unit Width
|
||||||
|
*/
|
||||||
|
private static final int LSI_UNIT_WIDTH = 308;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loading Splace Image Unit Height
|
||||||
|
*/
|
||||||
|
private static final int LSI_UNIT_HEIGHT = 560;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor doing all the basic stuff, like registering as a Listener to Discord, getting a logger, etc.
|
||||||
|
* @param client Discord Client instance used to register self.
|
||||||
|
*/
|
||||||
|
public LeagueOfDiscordModule(IDiscordClient client)
|
||||||
|
{
|
||||||
|
super(client);
|
||||||
|
|
||||||
|
|
||||||
|
// Region config
|
||||||
|
Region region;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
region = Region.valueOf(Configuration.getValue("leagueofdiscord", "region"));
|
||||||
|
logger.info("Selected region: " + region);
|
||||||
|
}
|
||||||
|
catch (IllegalArgumentException | NullPointerException e)
|
||||||
|
{
|
||||||
|
region = Region.NA;
|
||||||
|
logger.warning("Invalid region selected. Defaulting to NA !");
|
||||||
|
}
|
||||||
|
RiotAPI.setRegion(region);
|
||||||
|
|
||||||
|
// API Key config
|
||||||
|
if(Configuration.exists("leagueofdiscord", "apikey"))
|
||||||
|
RiotAPI.setAPIKey(Configuration.getValue("leagueofdiscord", "apikey"));
|
||||||
|
else
|
||||||
|
{
|
||||||
|
logger.severe("API Key not specified. Module shutdown.");
|
||||||
|
shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Command(description = "Returns this week F2P champions.")
|
||||||
|
public String fwr(MessageReceivedEvent event)
|
||||||
|
{
|
||||||
|
Map<Champion, ChampionStatus> champs = RiotAPI.getChampionStatuses(true);
|
||||||
|
|
||||||
|
StringBuffer toReturn = new StringBuffer("This week F2P champions:\n");
|
||||||
|
ArrayList<String> picMatrix = new ArrayList<>();
|
||||||
|
|
||||||
|
for(Map.Entry<Champion, ChampionStatus> i : champs.entrySet())
|
||||||
|
{
|
||||||
|
toReturn.append("* ").append(i.getKey().getName()).append(" - ").append(i.getKey().getTitle()).append("\n");
|
||||||
|
picMatrix.add("http://ddragon.leagueoflegends.com/cdn/img/champion/loading/" + i.getKey().getKey() + "_0.jpg");
|
||||||
|
}
|
||||||
|
|
||||||
|
// PICTURE PROCESSING
|
||||||
|
|
||||||
|
int lines = (int)Math.floor(picMatrix.size()/5.1+1);
|
||||||
|
BufferedImage result = new BufferedImage(LSI_UNIT_WIDTH*5, LSI_UNIT_HEIGHT*lines, BufferedImage.TYPE_INT_RGB);
|
||||||
|
Graphics g = result.createGraphics();
|
||||||
|
|
||||||
|
for(int i = 0; i < picMatrix.size(); i++)
|
||||||
|
{
|
||||||
|
int linepos = (int)Math.floor(i/5.0);
|
||||||
|
BufferedImage ins = null;
|
||||||
|
|
||||||
|
try { ins = ImageIO.read(new URL(picMatrix.get(i))); }
|
||||||
|
catch (IOException e) { toReturn.append("IO Error while processing the picture.").toString(); }
|
||||||
|
|
||||||
|
g.drawImage(ins, (i-linepos*5)*LSI_UNIT_WIDTH, linepos*LSI_UNIT_HEIGHT, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
File targetImage = File.createTempFile("DiscordBot", ".jpg");ImageIO.write(result, "jpg", targetImage);
|
||||||
|
client.getChannelByID(event.getMessage().getChannel().getID()).sendFile(targetImage);
|
||||||
|
}
|
||||||
|
catch (IOException e)
|
||||||
|
{
|
||||||
|
toReturn.append("IO Error while processing the picture.").toString();
|
||||||
|
}
|
||||||
|
catch (DiscordException | HTTP429Exception | MissingPermissionsException e)
|
||||||
|
{
|
||||||
|
toReturn.append("Error with Discord while processing the picture.").toString();
|
||||||
|
}
|
||||||
|
return toReturn.toString();
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue