Skip to content
Browse files


  • Loading branch information
MrSpinne committed Aug 31, 2019
1 parent aa95d8b commit f7dd7aececc6ad96cfc2864099f9ce4254d67437
Showing with 57 additions and 41 deletions.
  1. +3 −3 docs/source/source.rst
  2. +28 −7 extensions/
  3. +24 −20 library/{ →}
  4. +2 −11
@@ -61,8 +61,8 @@ Exceptions
.. automodule:: library.exceptions


.. automodule:: library.statposter
.. automodule:: library.statsposter
@@ -20,6 +20,8 @@
from datadog import initialize, statsd
import dbl

from library import statsposter

class Stats(commands.Cog):
"""Post bot stats to Datadog and Discord bot lists.
@@ -37,6 +39,9 @@ class Stats(commands.Cog):
Datadog api key to post events to.
dd_app_key: :obj:`str`
Datadog app key to post events to.
statsposter: :obj:`statsposter.StatsPoster`
Used to post bot stats to the bot lists.
DiscordBots excluded.
dbl: :obj:`dbl.Client`
DiscordBots client. This is `None` if no configuration found.
dbl_api_key: :obj:`str`
@@ -67,6 +72,8 @@ def __init__(self, bot):
self.dd_api_key ="datadog", "api_key")
self.dd_app_key ="datadog", "app_key")

self.statsposter = statsposter.StatsPoster(

self.dbl = None
self.dbl_api_key ="discordbots", "api_key")
self.dbl_webhook_auth ="discordbots", "webhook_auth")
@@ -82,6 +89,7 @@ def __init__(self, bot):


def init_datadog(self):
"""Initialize connection to Datadog to post metrics to and add event listener.
@@ -117,12 +125,14 @@ def init_discordbots(self):
if self.dbl_api_key:
credentials = {"token": self.dbl_api_key,
"autopost": True,
"webhook_auth": self.dbl_webhook_auth,
"webhook_path": self.dbl_webhook_path,
"webhook_port": self.dbl_webhook_port}
credentials = {
"token": self.dbl_api_key,
"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):
@@ -142,8 +152,19 @@ def init_discordbots(self):
async def post_bot_lists(self):
"""Post bot stats to the bot lists which have been configured.
Currently available lists can be looked up from the config or the class attributes.
credentials = {
"divinediscordbots": self.bl_divinediscordbots,
"discordbotreviews": self.bl_discordbotreviews,
"mythicalbots": self.bl_mythicalbots,
"discordbotlist": self.bl_discordbotlist,
"discordboats": self.bl_discordboats,
"botsondiscord": self.bl_botsondiscord
await self.statsposter.post_all(**credentials)

def setup(bot):
"""Load the cog into the bot instance.
@@ -2,22 +2,11 @@
import json

class StatPoster:
class StatsPoster:
def __init__(self, bot): = bot
self.session = aiohttp.ClientSession(, raise_for_status=True)

async def discordbots(self, token):
data = json.dumps({
"server_count": len(
headers = {
"authorization": token,
"content-type": "application/json"
url = f"{}/stats"
await self._post(url, data, headers)

async def divinediscordbots(self, token):
data = json.dumps({
"server_count": len(
@@ -92,11 +81,26 @@ def __init__(self, bot):
except Exception as e:
raise Exception(e)

async def post_all(self, tokens):
await self.discordbots(tokens["discordbots"])
await self.divinediscordbots(tokens["divinediscordbots"])
await self.discordbotreviews(tokens["discordbotreviews"])
await self.mythicalbots(tokens["mythicalbots"])
await self.discordbotlist(tokens["discordbotlist"])
await self.discordboats(tokens["discordboats"])
await self.botsondiscord(tokens["botsondiscord"])
async def post_all(self, **kwargs):
"""Post the stats to the bot lists which have been configured.
divinediscordbots: :obj:`str`
Api key for
for bot_list, api_key in kwargs:
if api_key:
if bot_list == "divinediscordbots":
await self.divinediscordbots(api_key)
elif bot_list == "discordbotreviews":
await self.discordbotreviews(api_key)
elif bot_list == "mythicalbots":
await self.mythicalbots(api_key)
elif bot_list == "discordbotlist":
await self.discordbotlist(api_key)
elif bot_list == "discordboats":
await self.discordboats(api_key)
elif bot_list == "botsondiscord":
await self.botsondiscord(api_key)
@@ -1,6 +1,6 @@
import discord
from discord.ext import commands, tasks
from library import exceptions, checks, statposter
from library import exceptions, checks, statsposter

import psycopg2.extras
import psycopg2.sql
@@ -65,7 +65,7 @@ def connect_optionals(self):

if self.config["botlist"].get("discordbots"):
self.statposter = statposter.StatPoster(self)
self.statposter = statsposter.StatPoster(self)

def translate(self, string):
@@ -362,15 +362,6 @@ def edit_song(self, song_id, setting, value):
sheet.sheet1.resize(len(formatted) + 1)
sheet.values_update("List!A2", params={"valueInputOption": "RAW"}, body={"values": formatted})

async def post_stats(self):
"""Update status every 30 minutes"""
await self.statposter.post_all(**self.config["botlist"])
except Exception as e:
# TODO: Specify exception

if __name__ == "__main__":

0 comments on commit f7dd7ae

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