pingex
/
DiscordBot
Archived
1
0
Fork 0

Bot broadcast his startup. Woah!

master
Pingex aka Raphaël 9 years ago
parent 902b9bfb6d
commit 97b4e13fc5

@ -6,6 +6,9 @@ commandPrefix = !
; Bot Owner UID, obtainable using /perm:getMyUID ; Bot Owner UID, obtainable using /perm:getMyUID
owner = XXXXXXXXXXXXXXXXXX owner = XXXXXXXXXXXXXXXXXX
; This bot name
name = My awesome bot
[discord] [discord]
; Change next value to true if the account is a bot, thus logins using a token. ; Change next value to true if the account is a bot, thus logins using a token.
; Don't forget to uncomment token then ; Don't forget to uncomment token then
@ -30,3 +33,8 @@ region = NA
; The API key Riot gave you. Empty key will disable the module. ; 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) ; 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 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%

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