searchbot-discord/extensions/botlist.py

177 lines
6 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):
2020-03-02 18:11:15 +00:00
2020-03-02 07:01:28 +00:00
# Main Stuff
2020-02-29 05:53:29 +00:00
self.bot = bot
self.request = bot.request
2020-03-20 18:15:50 +00:00
self.info = bot.logging.info
2020-03-02 18:11:15 +00:00
self.emoji = "\U0001F5F3"
2020-03-02 07:01:28 +00:00
# List Tokens
2020-03-19 18:16:22 +00:00
self.dbl_token = bot.config['BOTLISTS']['DBL']
self.dbots_token = bot.config['BOTLISTS']['DBOTS']
self.bod_token = bot.config['BOTLISTS']['BOD']
self.dblcom_token = bot.config['BOTLISTS']['DBLCOM']
2020-03-20 18:15:50 +00:00
self.blspace_token = bot.config['BOTLISTS']['BLSPACE']
self.dad_token = bot.config['BOTLISTS']['DAD']
2020-03-02 07:01:28 +00:00
2020-03-05 06:02:20 +00:00
# top.gg client
2020-02-29 05:53:29 +00:00
self.dbl_client = dbl.DBLClient(
2020-03-03 05:43:19 +00:00
self.bot, self.dbl_token)
2020-02-29 05:53:29 +00:00
2020-03-05 06:02:20 +00:00
# Start update loop
self.update_stats.start()
2020-02-29 05:53:29 +00:00
async def _update_logic(self):
"""Handles all statistic updating for various different bot lists."""
2020-03-02 06:37:35 +00:00
# Prerequisites
2020-02-29 05:53:29 +00:00
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-03-20 18:15:50 +00:00
dad_call = "https://api.discordapps.dev/api/v2/"
2020-02-29 05:53:29 +00:00
responses = {}
2020-03-02 07:01:28 +00:00
# Calls
2020-03-02 17:45:33 +00:00
# discord.bots.gg
2020-02-29 05:53:29 +00:00
if self.dbots_token != '':
2020-03-02 07:01:28 +00:00
# Call Prereqs
2020-02-29 05:53:29 +00:00
dbots_call += f"/bots/{self.bot.user.id}/stats"
dbots_data = {'guildCount': len(self.bot.guilds)}
dbots_headers = {'Authorization': self.dbots_token}
2020-03-02 07:01:28 +00:00
# Call Handling
async with self.request.post(dbots_call,
json=dbots_data,
2020-02-29 05:53:29 +00:00
headers=dbots_headers) as resp:
2020-03-19 22:32:00 +00:00
resp_json = await resp.json()
responses['dbots'] = resp_json
2020-02-29 05:53:29 +00:00
# bots.ondiscord.xyz
if self.bod_token != '':
2020-03-02 07:01:28 +00:00
# Call Prereqs
2020-02-29 05:53:29 +00:00
bod_call += f"/bots/{self.bot.user.id}/guilds"
bod_data = {'guildCount': len(self.bot.guilds)}
bod_headers = {'Authorization': self.bod_token}
2020-03-02 07:01:28 +00:00
# Call Handling
async with self.request.post(bod_call,
2020-02-29 05:53:29 +00:00
json=bod_data,
headers=bod_headers) as resp:
2020-03-19 22:32:00 +00:00
responses['bod'] = resp.status
2020-02-29 05:53:29 +00:00
2020-03-01 01:21:12 +00:00
# discordbotlist.com
if self.dblcom_token != '':
2020-03-02 07:01:28 +00:00
# Call Prereqs
2020-03-01 01:21:12 +00:00
dblcom_call += f"/bots/{self.bot.user.id}/stats"
dblcom_data = {'guilds': len(self.bot.guilds)}
2020-03-03 19:22:17 +00:00
dblcom_headers = {'Authorization': f"Bot {self.dblcom_token}"}
2020-03-02 07:01:28 +00:00
# Call Handling
async with self.request.post(dblcom_call,
2020-03-01 01:21:12 +00:00
json=dblcom_data,
headers=dblcom_headers) as resp:
2020-03-03 19:35:17 +00:00
responses['dblcom'] = resp.status
2020-03-01 01:21:12 +00:00
2020-02-29 05:53:29 +00:00
# top.gg
if self.dbl_token != '':
2020-03-02 07:01:28 +00:00
# NOTE top.gg has a lib so it gets different handling.
2020-02-29 05:53:29 +00:00
try:
resp = await self.dbl_client.post_guild_count()
responses['dbl'] = resp
except Exception as e:
responses['dbl'] = e
2020-03-20 18:15:50 +00:00
# discordapps.dev
if self.dad_token != '':
# Call Prereqs
dad_call += f"/bots/{self.bot.user.id}"
dad_data = {'bot': {'count': len(self.bot.guilds)}}
dad_headers = {'Authorization': self.dad_token}
# Call Handling
async with self.request.post(dad_call,
json=dad_data,
headers=dad_headers) as resp:
responses['dad'] = resp.status
log_msg = f"**Botlists updated!**\n\n```{responses}```"
await self.info(content=log_msg, name='List Update')
2020-03-02 07:01:28 +00:00
# Finalization
2020-02-29 05:53:29 +00:00
return responses
# TODO Move to Core, hide behind check for any existing token
@commands.command(aliases=['review'])
async def vote(self, ctx):
2020-03-02 06:37:35 +00:00
"""Review and vote for this bot on various botlists."""
2020-03-02 07:01:28 +00:00
# Header
msg = (
"**Thank you for wanting to help us out!**\n"
"You can find us on the following lists:\n\n"
)
2020-03-02 07:01:28 +00:00
# Links
2020-03-20 18:15:50 +00:00
# XXX This sucks
if self.dbots_token != '':
2020-03-02 17:45:33 +00:00
msg += f"_discord.bots.gg_ <https://discord.bots.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"
2020-03-20 18:15:50 +00:00
if self.bot.config['BOTLISTS']['TABFT_LINK']:
msg += f"_thereisabotforthat.com_ <{self.bot.config['BOTLISTS']['TABFT_LINK']}>\n"
if self.dad_token != '':
msg += f"_discordapps.dev_ <https://discordapps.dev/en-GB/bots/{self.bot.user.id}/>\n"
if self.blspace_token != '':
msg += f"_botlist.space_ <https://botlist.space/bot/{self.bot.user.id}\n>"
2020-03-02 07:01:28 +00:00
# Sending
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()
await msg.edit(content="**Updated!**")
@tasks.loop(minutes=15.0)
async def update_stats(self):
"""Automatically updates statistics every 15 minutes."""
2020-03-19 18:16:22 +00:00
2020-02-29 05:53:29 +00:00
responses = await self._update_logic()
2020-03-05 06:02:20 +00:00
def cog_unload(self):
self.update_stats.cancel()
2020-02-29 05:53:29 +00:00
def setup(bot):
2020-03-02 07:01:28 +00:00
"""Adds the cog to the bot."""
2020-03-02 04:58:05 +00:00
bot.add_cog(BotList(bot))