Skip to content
Permalink
Browse files

Stats

Readme
  • Loading branch information...
MrSpinne committed Aug 31, 2019
1 parent 07c055a commit a42458e74115adf5e2e0a867146f9fa386e0503e
Showing with 125 additions and 21 deletions.
  1. +2 −0 .github/README.md
  2. +3 −1 extensions/stats.py
  3. +120 −20 library/statsposter.py
@@ -1,3 +1,5 @@
<img align="right" src="https://shiro.pro/images/apple-touch-icon.png" alt="Shiro Avatar" height="150" width="150">

# Shiro.py - Work in progress
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/d668927a72f14c19b23ca9a0ed71fb20)](https://www.codacy.com/app/MrSpinne/Shiro.py?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=MrSpinne/Shiro.py&amp;utm_campaign=Badge_Grade)
[![Build Status](https://travis-ci.org/MrSpinne/Shiro.py.svg?branch=master)](https://travis-ci.org/MrSpinne/Shiro.py)
@@ -135,7 +135,7 @@ def init_discordbots(self):
}
self.dbl = dbl.Client(**credentials)

async def on_dbl_test(self, data):
async def on_dbl_vote(self, data):
"""Track votes from DiscordBots.org and save it to database.
This is automatically called by the event handler.
@@ -146,6 +146,8 @@ def init_discordbots(self):
Data with vote info.
"""
# TODO: Register user to database
pass

@tasks.loop(minutes=30.0)
async def post_bot_lists(self):
@@ -1,57 +1,131 @@
import aiohttp
"""
Shiro Discord Bot - A fun related anime bot
Copyright (C) 2019 MrSpinne
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""

import json
import aiohttp


class StatsPoster:
"""Post stats of the bot to the bot lists.
Parameters
----------
bot: `discord.ext.commands.AutoShardedBot`
Bot instance the cog was loaded into.
Attributes
----------
bot: `discord.ext.commands.AutoShardedBot`
Bot instance the cog was loaded into.
session: `aiohttp.ClientSession`
Session to use to perform http requests.
"""

def __init__(self, bot):
self.bot = bot
self.session = aiohttp.ClientSession(loop=self.bot.loop, raise_for_status=True)
self.session = aiohttp.ClientSession(loop=self.bot.loop)

async def divinediscordbots(self, api_key):
"""Post stats to divinediscordbots.com.
async def divinediscordbots(self, token):
Posted stats: server count
Parameters
----------
api_key: :obj:`str`
Api key to post stats to bot page.
"""
data = json.dumps({
"server_count": len(self.bot.guilds)
})
headers = {
"authorization": token,
"authorization": api_key,
"content-type": "application/json"
}
url = f"https://divinediscordbots.com/bot/{self.bot.user.id}/stats"
await self._post(url, data, headers)
await self.post(url, data, headers)

async def discordbotreviews(self, api_key):
"""Post stats to discordbotreviews.xyz.
Stats posted: server count
Parameters
----------
api_key: :obj:`str`
Api key to post stats to bot page.
async def discordbotreviews(self, token):
"""
data = json.dumps({
"server_count": len(self.bot.guilds)
})
headers = {
"authorization": token,
"authorization": api_key,
"content-type": "application/json"
}
url = f"https://discordbotreviews.xyz/api/bot/{self.bot.user.id}/stats"
await self._post(url, data, headers)
await self.post(url, data, headers)

async def mythicalbots(self, api_key):
"""Post stats to mythical-bots.ml.
async def mythicalbots(self, token):
Stats posted: server count
Parameters
----------
api_key: :obj:`str`
Api key to post stats to bot page.
"""
data = json.dumps({
"server_count": len(self.bot.guilds)
})
headers = {
"authorization": token,
"authorization": api_key,
"content-type": "application/json"
}
url = f"https://mythicalbots.xyz/api/bot/{self.bot.user.id}"
await self._post(url, data, headers)
await self.post(url, data, headers)

async def discordbotlist(self, api_key):
"""Post stats to discordbotlist.com.
Stats posted: server count, user count, amount of voice connections
async def discordbotlist(self, token):
Parameters
----------
api_key: :obj:`str`
Api key to post stats to bot page.
"""
data = json.dumps({
"guilds": len(self.bot.guilds),
"users": len(self.bot.users),
"voice_connections": len(self.bot.lavalink.players)
})
headers = {
"authorization": f"Bot {token}",
"authorization": f"Bot {api_key}",
"content-type": "application/json"
}
url = f"https://discordbotlist.com/api/bots/{self.bot.user.id}/stats"
await self._post(url, data, headers)
await self.post(url, data, headers)

async def discordboats(self, token):
data = json.dumps({
@@ -62,7 +136,7 @@ def __init__(self, bot):
"content-type": "application/json"
}
url = f"https://discord.boats/api/V2/bot/{self.bot.user.id}"
await self._post(url, data, headers)
await self.post(url, data, headers)

async def botsondiscord(self, token):
data = json.dumps({
@@ -73,21 +147,47 @@ def __init__(self, bot):
"content-type": "application/json"
}
url = f"https://bots.ondiscord.xyz/bot-api/bots/{self.bot.user.id}/guilds"
await self._post(url, data, headers)
await self.post(url, data, headers)

async def post(self, url, data, headers):
"""Post data to specified url (bot list)
async def _post(self, url, data, headers):
This won't return a status code or raise an error.
Parameters
----------
url: :obj:`str`
Url to send data to.
data: :obj:`str`
A JSON formatted string with all data (like server count) to be sent.
headers: :obj:`dict`
All neccessary headers which are expected by the bot list.
Bot lists will always require an `Authorization` header.
"""
try:
await self.session.post(url, data=data, headers=headers)
except Exception as e:
raise Exception(e)
except Exception:
# FIXME: Catch specified exception and resolve status codes
pass

async def post_all(self, **kwargs):
"""Post the stats to the bot lists which have been configured.
Parameters
----------
divinediscordbots: :obj:`str`
Api key for
Api key for divinediscordbots.com to post stats to.
discordbotreviews: :obj:`str`
Api key for discordbotreviews.xyz to post stats to.
mythicalbots: :obj:`str`
Api key for mythical-bots.ml to post stats to.
discordbotlist: :obj:`str`
Api key for discordbotlist.com to post stats to.
discordboats: :obj:`str`
Api key for discord.boats to post stats to.
botsondiscord: :obj:`str`
Api key for bots.ondiscord.xyz to post stats to.
"""
for bot_list, api_key in kwargs:

0 comments on commit a42458e

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