2018-10-30 14:00:37 +13:00
|
|
|
from utils import config
|
2016-08-02 01:53:46 +12:00
|
|
|
import aiohttp
|
|
|
|
import logging
|
|
|
|
import json
|
2017-05-09 09:54:24 +12:00
|
|
|
import discord
|
2016-08-02 01:53:46 +12:00
|
|
|
|
|
|
|
log = logging.getLogger()
|
|
|
|
|
2018-05-24 17:09:42 +12:00
|
|
|
discord_bots_url = 'https://bots.discord.pw/api/bots/{}/stats'
|
|
|
|
discordbots_url = "https://discordbots.org/api/bots/{}/stats"
|
2016-08-19 13:56:41 +12:00
|
|
|
carbonitex_url = 'https://www.carbonitex.net/discord/data/botdata.php'
|
2016-08-02 01:53:46 +12:00
|
|
|
|
2016-08-07 10:20:23 +12:00
|
|
|
|
2016-08-02 01:53:46 +12:00
|
|
|
class StatsUpdate:
|
2018-05-24 17:09:42 +12:00
|
|
|
"""This is used purely to update stats information for the bot sites"""
|
2016-08-07 10:20:23 +12:00
|
|
|
|
2016-08-02 01:53:46 +12:00
|
|
|
def __init__(self, bot):
|
|
|
|
self.bot = bot
|
|
|
|
self.session = aiohttp.ClientSession()
|
2016-08-07 10:20:23 +12:00
|
|
|
|
2016-08-02 01:53:46 +12:00
|
|
|
def __unload(self):
|
2016-09-21 10:22:02 +12:00
|
|
|
self.bot.loop.create_task(self.session.close())
|
2016-08-02 01:53:46 +12:00
|
|
|
|
2016-09-29 12:39:34 +13:00
|
|
|
async def update(self):
|
2017-03-08 12:56:24 +13:00
|
|
|
server_count = len(self.bot.guilds)
|
2016-09-01 15:23:58 +12:00
|
|
|
|
2018-05-24 17:09:42 +12:00
|
|
|
# Carbonitex request
|
2016-08-19 13:56:41 +12:00
|
|
|
carbon_payload = {
|
|
|
|
'key': config.carbon_key,
|
2016-09-01 15:23:58 +12:00
|
|
|
'servercount': server_count
|
2016-08-19 13:56:41 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
async with self.session.post(carbonitex_url, data=carbon_payload) as resp:
|
|
|
|
log.info('Carbonitex statistics returned {} for {}'.format(resp.status, carbon_payload))
|
2016-10-31 17:32:33 +13:00
|
|
|
|
2018-05-24 17:09:42 +12:00
|
|
|
# Discord.bots.pw request
|
2016-08-02 01:53:46 +12:00
|
|
|
payload = json.dumps({
|
2016-09-01 15:23:58 +12:00
|
|
|
'server_count': server_count
|
2016-08-02 01:53:46 +12:00
|
|
|
})
|
|
|
|
|
|
|
|
headers = {
|
|
|
|
'authorization': config.discord_bots_key,
|
|
|
|
'content-type': 'application/json'
|
|
|
|
}
|
|
|
|
|
2018-05-24 17:09:42 +12:00
|
|
|
url = discord_bots_url.format(self.bot.user.id)
|
2016-08-02 01:53:46 +12:00
|
|
|
async with self.session.post(url, data=payload, headers=headers) as resp:
|
2016-08-19 13:56:41 +12:00
|
|
|
log.info('bots.discord.pw statistics returned {} for {}'.format(resp.status, payload))
|
2016-08-07 10:20:23 +12:00
|
|
|
|
2018-05-24 17:09:42 +12:00
|
|
|
# discordbots.com request
|
|
|
|
url = discordbots_url.format(self.bot.user.id)
|
|
|
|
payload = {
|
|
|
|
"server_count": server_count
|
|
|
|
}
|
|
|
|
|
|
|
|
headers = {
|
|
|
|
"Authorization": config.discordbots_key
|
|
|
|
}
|
|
|
|
async with self.session.post(url, data=payload, headers=headers) as resp:
|
|
|
|
log.info('discordbots.com statistics retruned {} for {}'.format(resp.status, payload))
|
|
|
|
|
2017-06-07 20:30:19 +12:00
|
|
|
async def on_guild_join(self, _):
|
2018-09-24 06:33:46 +12:00
|
|
|
await self.update()
|
2016-08-02 01:53:46 +12:00
|
|
|
|
2017-06-07 20:30:19 +12:00
|
|
|
async def on_guild_leave(self, _):
|
2018-09-24 06:33:46 +12:00
|
|
|
await self.update()
|
2016-08-02 01:53:46 +12:00
|
|
|
|
|
|
|
async def on_ready(self):
|
2018-09-24 06:33:46 +12:00
|
|
|
await self.update()
|
2016-08-02 01:53:46 +12:00
|
|
|
|
2016-10-31 17:32:33 +13:00
|
|
|
async def on_member_join(self, member):
|
2019-01-28 15:58:39 +13:00
|
|
|
query = """
|
|
|
|
SELECT
|
|
|
|
COALESCE(welcome_alerts, default_alerts) AS channel,
|
|
|
|
welcome_msg AS msg
|
|
|
|
FROM
|
|
|
|
guilds
|
|
|
|
WHERE
|
|
|
|
welcome_notifications = True
|
|
|
|
AND
|
|
|
|
id = $1
|
|
|
|
AND
|
|
|
|
COALESCE(welcome_alerts, default_alerts) IS NOT NULL
|
|
|
|
"""
|
|
|
|
settings = await self.bot.db.fetchrow(query, member.guild.id)
|
2019-01-28 16:22:55 +13:00
|
|
|
if settings:
|
|
|
|
message = settings['msg'] or "Welcome to the '{server}' server {member}!"
|
|
|
|
channel = member.guild.get_channel(settings['channel'])
|
|
|
|
try:
|
|
|
|
await channel.send(message.format(server=member.guild.name, member=member.mention))
|
|
|
|
except (discord.Forbidden, discord.HTTPException, AttributeError):
|
|
|
|
pass
|
2016-10-31 17:32:33 +13:00
|
|
|
|
|
|
|
async def on_member_remove(self, member):
|
2019-01-28 15:58:39 +13:00
|
|
|
query = """
|
|
|
|
SELECT
|
|
|
|
COALESCE(goodbye_alerts, default_alerts) AS channel,
|
|
|
|
goodbye_msg AS msg
|
|
|
|
FROM
|
|
|
|
guilds
|
|
|
|
WHERE
|
|
|
|
welcome_notifications = True
|
|
|
|
AND
|
|
|
|
id = $1
|
|
|
|
AND
|
|
|
|
COALESCE(goodbye_alerts, default_alerts) IS NOT NULL
|
|
|
|
"""
|
|
|
|
settings = await self.bot.db.fetchrow(query, member.guild.id)
|
2019-01-28 16:22:55 +13:00
|
|
|
if settings:
|
|
|
|
message = settings['msg'] or "{member} has left the server, I hope it wasn't because of something I said :c"
|
|
|
|
channel = member.guild.get_channel(settings['channel'])
|
|
|
|
try:
|
|
|
|
await channel.send(message.format(server=member.guild.name, member=member.mention))
|
|
|
|
except (discord.Forbidden, discord.HTTPException, AttributeError):
|
|
|
|
pass
|
2016-10-31 17:32:33 +13:00
|
|
|
|
2016-08-07 10:20:23 +12:00
|
|
|
|
2016-08-02 01:53:46 +12:00
|
|
|
def setup(bot):
|
|
|
|
bot.add_cog(StatsUpdate(bot))
|