searchbot-discord/extensions/botlist.py

177 lines
6.0 KiB
Python
Raw Permalink 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 aiohttp
import dbl
2020-04-06 16:26:15 +00:00
import discord
from discord.ext import commands, tasks
2020-02-29 05:53:29 +00:00
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-21 15:05:20 +00:00
self.debug = bot.logging.debug
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
2020-04-08 22:37:22 +00:00
self.update_stats.start() # pylint: disable=no-member
2020-03-05 06:02:20 +00:00
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}```"
2020-04-07 23:56:01 +00:00
self.debug(content=log_msg, name='List Update')
2020-03-20 18:15:50 +00:00
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...**")
await self._update_logic()
2020-02-29 05:53:29 +00:00
await msg.edit(content="**Updated!**")
2020-03-20 19:25:19 +00:00
@tasks.loop(minutes=30.0)
2020-02-29 05:53:29 +00:00
async def update_stats(self):
"""Automatically updates statistics every 15 minutes."""
2020-03-19 18:16:22 +00:00
await self._update_logic()
2020-02-29 05:53:29 +00:00
2020-03-05 06:02:20 +00:00
def cog_unload(self):
2020-04-08 22:37:22 +00:00
self.update_stats.cancel() # pylint: disable=no-member
2020-03-05 06:02:20 +00:00
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))