Bot broadcast his startup. Woah!
parent
902b9bfb6d
commit
97b4e13fc5
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue