Skip to content
Permalink
Browse files

Stats webhooks added

  • Loading branch information...
MrSpinne committed Aug 31, 2019
1 parent ddb56ba commit ccaa0452288e275004df126a841f0b65c18ad292
Showing with 63 additions and 17 deletions.
  1. +7 −2 data/config.ini
  2. +52 −11 extensions/stats.py
  3. +2 −1 requirements.txt
  4. +2 −3 shiro.py
@@ -28,8 +28,13 @@ private_key_id =
api_key =
app_key =

[Botlist] # optional
discordbots =
[Discordbots] # optional
api_key =
webhook_auth =
webhook_path =
webhook_port =

[Botlists] # optional
divinediscordbots =
discordbotreviews =
mythicalbots =
@@ -16,8 +16,9 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""

from discord.ext import commands
from discord.ext import commands, tasks
from datadog import initialize, statsd
import dbl


class Stats(commands.Cog):
@@ -36,9 +37,16 @@ class Stats(commands.Cog):
Datadog api key to post events to.
dd_app_key: :obj:`str`
Datadog app key to post events to.
bl_discordbots: :obj:`str`
dbl: :obj:`dbl.Client`
DiscordBots client. This is `None` if no configuration found.
dbl_api_key: :obj:`str`
Api key for discordbots.org to post stats to.
Also used to enabled vote locking.
dbl_webhook_auth: :obj:`str`
Webhook authorization for discordbots.org.
dbl_webhook_path: :obj:`str`
Path of the webhook to use for discordbots.org.
dbl_webhook_port: :obj:`str`
Port to use the webhook for discordbots.org on.
bl_divinediscordbots: :obj:`str`
Api key for divinediscordbots.com to post stats to.
bl_discordbotreviews: :obj:`str`
@@ -58,7 +66,13 @@ def __init__(self, bot):
self.bot = bot
self.dd_api_key = self.bot.get_config("datadog", "api_key")
self.dd_app_key = self.bot.get_config("datadog", "app_key")
self.bl_discordbots = self.bot.get_config("botlists", "discordbots")

self.dbl = None
self.dbl_api_key = self.bot.get_config("discordbots", "api_key")
self.dbl_webhook_auth = self.bot.get_config("discordbots", "webhook_auth")
self.dbl_webhook_path = self.bot.get_config("discordbots", "webhook_path")
self.dbl_webhook_port = self.bot.get_config("discordbots", "webhook_port")

self.bl_divinediscordbots = self.bot.get_config("botlists", "divinediscordbots")
self.bl_discordbotreviews = self.bot.get_config("botlists", "discordbotreviews")
self.bl_mythicalbots = self.bot.get_config("botlists", "mythicalbots")
@@ -67,7 +81,7 @@ def __init__(self, bot):
self.bl_botsondiscord = self.bot.get_config("botlists", "botsondiscord")

self.init_datadog()
self.init_bot_lists()
self.init_discordbots()

def init_datadog(self):
"""Initialize connection to Datadog to post metrics to and add event listener.
@@ -78,7 +92,7 @@ def init_datadog(self):
The `on_socket_response` event is called on any event.
"""
if self.dd_api_key != "" and self.dd_app_key != "":
if self.dd_api_key and self.dd_app_key:
initialize(self.dd_api_key, self.dd_app_key)
self.bot.add_listener(self.track_events, "on_socket_response")

@@ -95,11 +109,38 @@ def init_datadog(self):
event = payload.get("t")
statsd.increment(event)

def init_bot_lists(self):
"""Initialize stats posting to the bot lists. This will also setup vote recognition.
def init_discordbots(self):
"""Initialize stats posting to the bot lists and vote recognition.
This also registers a webhook to track votes.
If DiscordBot credentials are not set, then it will be disabled.
"""
if self.dbl_api_key:
credentials = {"token": self.dbl_api_key,
"bot": self.bot,
"autopost": True,
"webhook_auth": self.dbl_webhook_auth,
"webhook_path": self.dbl_webhook_path,
"webhook_port": self.dbl_webhook_port}
self.dbl = dbl.Client(**credentials)

async def on_dbl_test(self, data):
"""Track votes from DiscordBots.org and save it to database.
This is automatically called by the event handler.
Parameters
----------
data: :obj:`dict`
Data with vote info.
"""
# TODO: Register vote to db

Only bot lists with credentials will be initialized.
If DiscordBot credentials are not set then vote locking will be disabled.
@tasks.loop(minutes=30.0)
async def post_bot_lists(self):
"""Post bot stats to the bot lists which have been configured.
"""

@@ -110,7 +151,7 @@ def setup(bot):
Parameters
----------
bot: :class:`commands.AutoShardedBot`
bot: :class:`discord.ext.commands.AutoShardedBot`
"""
bot.add_cog(Stats(bot))
@@ -13,4 +13,5 @@ aiohttp
gspread
oauth2client
PyOpenSSL
datadog
datadog
dblpy
@@ -26,7 +26,6 @@ def __init__(self):
signal.signal(signal.SIGTERM, self.shutdown)
self.db_connector, self.db_cursor, self.app_info, self.gspread, self.config = None, None, None, None, {}
self.sentry, self.lavalink, self.dbl, self.statposter, self.anilist = sentry_sdk, None, None, None, Pymoe.Anilist()
self.parse_config()

def get_config(self, option, key):
"""Get config value"""
@@ -36,7 +35,7 @@ def get_config(self, option, key):
else:
config = configparser.ConfigParser()
config.read("data/config.ini")
return config.get(option.lower(), key)
return config.get(option.capitalize(), key)

async def on_ready(self):
"""Get ready and start"""
@@ -376,4 +375,4 @@ def edit_song(self, song_id, setting, value):
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
shiro = Shiro()
shiro.run(shiro.config["discord"]["token"])
shiro.run(shiro.get_config("discord", "token"))

0 comments on commit ccaa045

Please sign in to comment.
You can’t perform that action at this time.