pingex
/
DiscordBot
Archived
1
0
Fork 0

League Of Discord, initial commit.

master
Pingex aka Raphaël 9 years ago
parent bb1fc6da4a
commit 61aaf6fb28

@ -30,6 +30,9 @@ dependencies {
compile "org.slf4j:slf4j-simple:1.7.9" compile "org.slf4j:slf4j-simple:1.7.9"
compile "org.apache.commons:commons-lang3:3.0" compile "org.apache.commons:commons-lang3:3.0"
compile "com.google.code.gson:gson:2.6.2" compile "com.google.code.gson:gson:2.6.2"
// LeagueOfDiscordModule
compile "com.robrua:orianna:2.4.3"
} }
jar { jar {

@ -22,3 +22,11 @@ enable = net.pingex.discordbot.HelloWorldModule
[permissions] [permissions]
; The location of the permissions file, defaulting to `permissions.json` if not specified ; The location of the permissions file, defaulting to `permissions.json` if not specified
file = permissions.json file = permissions.json
[leagueofdiscord]
; LoL Region to focus requests on. Invalid region will fallback on NA.
region = NA
; The API key Riot gave you. Empty key will disable the module.
; This key serves as an example and is invalid. You can get a key by visiting https://developer.riotgames.com (LoL account required)
apikey = a3cd6695-2174-4ea8-ac97-414f6dfc8826

@ -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();
}
}