searchbot-discord/extensions/botlist.py

123 lines
4.3 KiB
Python
Raw Normal View History

2020-02-29 05:53:29 +00:00
# -*- coding: utf-8 -*-
# Botlist Reporting
# Reports statistics to various botlists.
# Not useful for redist instances.
'''Botlist Cog'''
import discord
import aiohttp
from discord.ext import commands, tasks
import dbl
2020-03-02 04:58:05 +00:00
class BotList(commands.Cog, name='Bot List'):
"""Provides various utilities for handling BotList stuff."""
2020-02-29 05:53:29 +00:00
def __init__(self, bot):
self.bot = bot
self.request = bot.request
self.dbl_token = bot.config['DBL']
self.dbots_token = bot.config['DBOTS']
self.bod_token = bot.config['BOD']
2020-03-01 01:21:12 +00:00
self.dblcom_token = bot.config['DBLCOM']
2020-02-29 05:53:29 +00:00
self.dbl_client = dbl.DBLClient(
self.bot, self.dbots_token)
async def _update_logic(self):
"""Handles all statistic updating for various different bot lists."""
dbots_call = "https://discord.bots.gg/api/v1"
bod_call = "https://bots.ondiscord.xyz/bot-api/"
2020-03-01 01:21:12 +00:00
dblcom_call = "https://discordbotlist.com/api"
2020-02-29 05:53:29 +00:00
responses = {}
# bots.discord.gg
if self.dbots_token != '':
dbots_call += f"/bots/{self.bot.user.id}/stats"
dbots_data = {'guildCount': len(self.bot.guilds)}
dbots_headers = {'Authorization': self.dbots_token}
async with self.request.post(dbots_call,
json=dbots_data,
headers=dbots_headers) as resp:
resp_json = await resp.json()
print(resp_json)
responses['dbots'] = resp_json
# bots.ondiscord.xyz
if self.bod_token != '':
bod_call += f"/bots/{self.bot.user.id}/guilds"
bod_data = {'guildCount': len(self.bot.guilds)}
bod_headers = {'Authorization': self.bod_token}
async with self.request.post(bod_call,
json=bod_data,
headers=bod_headers) as resp:
resp_json = await resp.json()
print(resp_json)
responses['bod'] = resp_json
2020-03-01 01:21:12 +00:00
# discordbotlist.com
if self.dblcom_token != '':
dblcom_call += f"/bots/{self.bot.user.id}/stats"
dblcom_data = {'guilds': len(self.bot.guilds)}
dblcom_headers = {'Authorization': self.dblcom_token}
async with self.request.post(dblcom_call,
json=dblcom_data,
headers=dblcom_headers) as resp:
resp_json = await resp.json()
print(resp_json)
responses['dblcom'] = resp_json
2020-02-29 05:53:29 +00:00
# top.gg
if self.dbl_token != '':
try:
resp = await self.dbl_client.post_guild_count()
responses['dbl'] = resp
except Exception as e:
responses['dbl'] = e
# Finishing up
return responses
# TODO Move to Core, hide behind check for any existing token
@commands.command(aliases=['review'])
async def vote(self, ctx):
"""Review and vote for us on various botlists!"""
msg = (
"**Thank you for wanting to help us out!**\n"
"You can find us on the following lists:\n\n"
)
if self.dbots_token != '':
msg += f"_bots.discord.gg_ <https://bots.discord.gg/bots/{self.bot.user.id}/>\n"
if self.bod_token != '':
msg += f"_bots.ondiscord.xyz_ <https://bots.ondiscord.xyz/bots/{self.bot.user.id}/>\n"
if self.dblcom_token != '':
msg += f"_discordbotlist.com_ <https://discordbotlist.com/bots/{self.bot.user.id}/>\n"
if self.dbl_token != '':
msg += f"_top.gg_ <https://top.gg/bot/{self.bot.user.id}/>\n"
await ctx.send(msg)
2020-02-29 05:53:29 +00:00
@commands.command()
@commands.is_owner()
2020-02-29 05:53:29 +00:00
async def listupdate(self, ctx):
"""Updates statistics on botlists."""
2020-02-29 20:26:57 +00:00
msg = await ctx.send("<a:loading:393852367751086090> **Updating...**")
2020-02-29 05:53:29 +00:00
responses = await self._update_logic()
print(responses)
await msg.edit(content="**Updated!**")
@tasks.loop(minutes=15.0)
async def update_stats(self):
"""Automatically updates statistics every 15 minutes."""
2020-02-29 05:53:29 +00:00
responses = await self._update_logic()
print(responses)
def setup(bot):
2020-03-02 04:58:05 +00:00
bot.add_cog(BotList(bot))