diff --git a/config.example.ini b/config.example.ini index 7c3c4ac..9e4d1e0 100644 --- a/config.example.ini +++ b/config.example.ini @@ -6,6 +6,9 @@ commandPrefix = ! ; Bot Owner UID, obtainable using /perm:getMyUID owner = XXXXXXXXXXXXXXXXXX +; This bot name +name = My awesome bot + [discord] ; Change next value to true if the account is a bot, thus logins using a token. ; Don't forget to uncomment token then @@ -29,4 +32,9 @@ 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 \ No newline at end of file +apikey = a3cd6695-2174-4ea8-ac97-414f6dfc8826 + +[botstatus] +; Text channels containing this string sequence as MOTD will receive all bot logs. +; An empty value will disable +botlog_detection = %botlog% \ No newline at end of file diff --git a/src/main/java/net/pingex/dbotm/BotStatusModule.java b/src/main/java/net/pingex/dbotm/BotStatusModule.java new file mode 100644 index 0000000..064567a --- /dev/null +++ b/src/main/java/net/pingex/dbotm/BotStatusModule.java @@ -0,0 +1,63 @@ +package net.pingex.dbotm; + +import net.pingex.discordbot.AbstractModule; +import net.pingex.discordbot.Configuration; +import sx.blah.discord.api.EventSubscriber; +import sx.blah.discord.api.IDiscordClient; +import sx.blah.discord.handle.impl.events.GuildCreateEvent; +import sx.blah.discord.handle.obj.IChannel; +import sx.blah.discord.util.DiscordException; +import sx.blah.discord.util.HTTP429Exception; +import sx.blah.discord.util.MissingPermissionsException; + +/** + * Status of the Bot, ie. starting up and so on + * @version 0.1-dev + * @author Raphael "Pingex" NAAS + */ +public class BotStatusModule extends AbstractModule +{ + /** + * Whether the bot should broadcast his logs + */ + private boolean enableBotBroadcast = true; + + /** + * 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 BotStatusModule(IDiscordClient client) + { + super(client); + + if(!Configuration.exists("botstatus", "botlog_detection") || Configuration.getValue("botstatus", "botlog_detection").isEmpty()) + { + logger.warning("Bot won't log anything to Discord channel per configuration value."); + enableBotBroadcast = false; + } + + if(!Configuration.exists("general", "name") || Configuration.getValue("general", "name").isEmpty()) + Configuration.setValue("general", "name", client.getOurUser().getName()); + } + + /** + * Broadcast the starting up event to botlog channels. + */ + @EventSubscriber + public void broadcastStartup(GuildCreateEvent event) + { + if(enableBotBroadcast) + for(IChannel channel : event.getGuild().getChannels()) + if (channel.getTopic().contains(Configuration.getValue("botstatus", "botlog_detection"))) + try + { + channel.sendMessage(Configuration.getValue("general", "name") + " (DiscordBot version " + + (Configuration.class.getPackage().getImplementationVersion() != null ? Configuration.class.getPackage().getImplementationVersion() : "UNKNOWN") + + ") started up and joined this server !"); + } + catch (MissingPermissionsException | HTTP429Exception | DiscordException e) + { + logger.warning("Failed to send message to channel #" + channel.getID() + ": " + e.getMessage()); + } + } +}