You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Enso-Bot/cogs/botlists.py

78 lines
2.6 KiB
Python

# Ensō~Chan - A Multi Purpose Discord Bot That Has Everything Your Server Needs!
# Copyright (C) 2020 Hamothy
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import aiohttp
import dbl
import statcord
from decouple import config
from discord.ext import commands, tasks
statcord_auth = config("STATCORD_AUTH")
disforge_auth = config('DISFORGE_AUTH')
disc_bots_gg_auth = config('DISCORD_BOTS_BOTS_AUTH')
top_gg_auth = config('TOP_GG_AUTH')
async def post_bot_stats(self):
"""Update guild count on bot lists"""
async with aiohttp.ClientSession() as session:
await session.post(f"https://discord.bots.gg/api/v1/bots/{self.user.id}/stats",
data={"guildCount": {len(self.guilds)},
"Content-Type": "application/json"},
headers={'Authorization': disc_bots_gg_auth})
await session.post(f"https://disforge.com/api/botstats/{self.user.id}",
data={"servers": {len(self.guilds)}},
headers={'Authorization': disforge_auth})
class BotLists(commands.Cog):
"""Handles interactions with the top.gg API"""
def __init__(self, bot):
self.bot = bot
self.token = top_gg_auth
self.dblpy = dbl.DBLClient(self.bot, self.token)
self.key = f"statcord.com-{statcord_auth}"
self.api = statcord.Client(self.bot, self.key)
self.api.start_loop()
@tasks.loop(minutes=30, reconnect=True)
async def post_updates():
"""Post updates to botlists"""
await self.bot.wait_until_ready()
try:
await post_bot_stats(self.bot)
await self.dblpy.post_guild_count()
except Exception as e:
print(e)
else:
print("Server count posted successfully")
# Start the background task(s)
post_updates.start()
@commands.Cog.listener()
async def on_command(self, ctx):
self.api.command_run(ctx)
def setup(bot):
bot.add_cog(BotLists(bot))