Skip to content
Permalink
Browse files

Stats

  • 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/stats.py
  3. +24 −20 library/{statposter.py → statsposter.py}
  4. +2 −11 shiro.py
@@ -61,8 +61,8 @@ Exceptions
.. automodule:: library.exceptions
:members:

Statposter
**********
Statsposter
***********

.. automodule:: library.statposter
.. automodule:: library.statsposter
:members:
@@ -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 = self.bot.get_config("datadog", "api_key")
self.dd_app_key = self.bot.get_config("datadog", "app_key")

self.statsposter = statsposter.StatsPoster(self.bot)

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")
@@ -82,6 +89,7 @@ def __init__(self, bot):

self.init_datadog()
self.init_discordbots()
self.post_bot_lists.start()

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,
"bot": self.bot,
"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,
"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):
@@ -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.
"""
pass
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):
self.bot = bot
self.session = aiohttp.ClientSession(loop=self.bot.loop, raise_for_status=True)

async def discordbots(self, token):
data = json.dumps({
"server_count": len(self.bot.guilds)
})
headers = {
"authorization": token,
"content-type": "application/json"
}
url = f"https://discordbots.org/api/bots/{self.bot.user.id}/stats"
await self._post(url, data, headers)

async def divinediscordbots(self, token):
data = json.dumps({
"server_count": len(self.bot.guilds)
@@ -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.
Parameters
----------
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):
self.update_songs_list.start()

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

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})

@tasks.loop(minutes=15)
async def post_stats(self):
"""Update status every 30 minutes"""
try:
await self.statposter.post_all(**self.config["botlist"])
except Exception as e:
self.sentry.capture_exception(e)
# TODO: Specify exception


if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)

0 comments on commit f7dd7ae

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