searchbot-discord/extensions/botlist.py

151 lines
4.9 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-02 18:11:15 +00:00
self.emoji = "\U0001F5F3"
2020-03-02 07:01:28 +00:00
# List Tokens
2020-02-29 05:53:29 +00:00
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-03-02 07:01:28 +00:00
2020-03-02 06:37:35 +00:00
# top.gg clent
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
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-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:
resp_json = await resp.json()
print(resp_json)
responses['dbots'] = resp_json
# 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:
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 != '':
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 05:43:19 +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:
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 != '':
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-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
# XXX This is really stupidly done, I'm going to find a way to redo it.
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-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()
2020-03-02 07:01:28 +00:00
print(responses) # TODO Look at responses and figure out error handling
2020-02-29 05:53:29 +00:00
await msg.edit(content="**Updated!**")
@tasks.loop(minutes=15.0)
async def update_stats(self):
"""Automatically updates statistics every 15 minutes."""
2020-03-02 07:01:28 +00:00
2020-02-29 05:53:29 +00:00
responses = await self._update_logic()
2020-03-02 07:01:28 +00:00
print(responses) # TODO See other todo
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))