2016-08-02 01:53:46 +12:00
|
|
|
from .utils import config
|
|
|
|
import aiohttp
|
|
|
|
import logging
|
|
|
|
import json
|
|
|
|
|
|
|
|
log = logging.getLogger()
|
|
|
|
|
|
|
|
discord_bots_url = 'https://bots.discord.pw/api'
|
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:
|
|
|
|
"""This is used purely to update stats information for carbonitex and botx.discord.pw"""
|
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-17 18:21:30 +13:00
|
|
|
# Currently disabled
|
|
|
|
return
|
2016-09-01 15:23:58 +12:00
|
|
|
server_count = 0
|
2016-09-29 12:39:34 +13:00
|
|
|
data = await config.get_content('bot_data')
|
|
|
|
|
|
|
|
for entry in data:
|
|
|
|
server_count += entry.get('server_count')
|
2016-09-01 15:23:58 +12:00
|
|
|
|
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
|
|
|
|
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'
|
|
|
|
}
|
|
|
|
|
2016-08-19 13:56:41 +12:00
|
|
|
url = '{}/bots/{}/stats'.format(discord_bots_url, 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
|
|
|
|
2016-08-02 01:53:46 +12:00
|
|
|
async def on_server_join(self, server):
|
2017-03-17 18:21:30 +13:00
|
|
|
return
|
2016-09-29 12:39:34 +13:00
|
|
|
r_filter = {'shard_id': config.shard_id}
|
|
|
|
server_count = len(self.bot.servers)
|
|
|
|
member_count = len(set(self.bot.get_all_members()))
|
|
|
|
entry = {'server_count': server_count, 'member_count': member_count, "shard_id": config.shard_id}
|
|
|
|
# Check if this was successful, if it wasn't, that means a new shard was added and we need to add that entry
|
|
|
|
if not await config.update_content('bot_data', entry, r_filter):
|
|
|
|
await config.add_content('bot_data', entry, r_filter)
|
|
|
|
self.bot.loop.create_task(self.update())
|
2016-08-02 01:53:46 +12:00
|
|
|
|
|
|
|
async def on_server_leave(self, server):
|
2017-03-17 18:21:30 +13:00
|
|
|
return
|
2016-09-29 12:39:34 +13:00
|
|
|
r_filter = {'shard_id': config.shard_id}
|
|
|
|
server_count = len(self.bot.servers)
|
|
|
|
member_count = len(set(self.bot.get_all_members()))
|
|
|
|
entry = {'server_count': server_count, 'member_count': member_count, "shard_id": config.shard_id}
|
|
|
|
# Check if this was successful, if it wasn't, that means a new shard was added and we need to add that entry
|
|
|
|
if not await config.update_content('bot_data', entry, r_filter):
|
|
|
|
await config.add_content('bot_data', entry, r_filter)
|
|
|
|
self.bot.loop.create_task(self.update())
|
2016-08-02 01:53:46 +12:00
|
|
|
|
|
|
|
async def on_ready(self):
|
2017-03-17 18:21:30 +13:00
|
|
|
return
|
2016-09-29 12:39:34 +13:00
|
|
|
r_filter = {'shard_id': config.shard_id}
|
|
|
|
server_count = len(self.bot.servers)
|
|
|
|
member_count = len(set(self.bot.get_all_members()))
|
|
|
|
entry = {'server_count': server_count, 'member_count': member_count, "shard_id": config.shard_id}
|
|
|
|
# Check if this was successful, if it wasn't, that means a new shard was added and we need to add that entry
|
|
|
|
if not await config.update_content('bot_data', entry, r_filter):
|
|
|
|
await config.add_content('bot_data', entry, r_filter)
|
|
|
|
self.bot.loop.create_task(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):
|
|
|
|
server = member.server
|
2017-03-17 18:21:30 +13:00
|
|
|
server_settings = await config.get_content('server_settings', server.id)
|
2016-10-31 17:32:33 +13:00
|
|
|
|
|
|
|
try:
|
2017-03-17 18:21:30 +13:00
|
|
|
join_leave_on = server_settings['join_leave']
|
|
|
|
if join_leave_on:
|
|
|
|
channel_id = server_settings.get('notification_channel') or member.server.id
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
except (IndexError, TypeError, KeyError):
|
2016-10-31 17:32:33 +13:00
|
|
|
return
|
|
|
|
|
|
|
|
channel = server.get_channel(channel_id)
|
|
|
|
await self.bot.send_message(channel, "Welcome to the '{0.server.name}' server {0.mention}!".format(member))
|
|
|
|
|
|
|
|
async def on_member_remove(self, member):
|
|
|
|
server = member.server
|
2017-03-17 18:21:30 +13:00
|
|
|
server_settings = await config.get_content('server_settings', server.id)
|
2016-10-31 17:32:33 +13:00
|
|
|
|
|
|
|
try:
|
2017-03-17 18:21:30 +13:00
|
|
|
join_leave_on = server_settings['join_leave']
|
|
|
|
if join_leave_on:
|
|
|
|
channel_id = server_settings.get('notification_channel') or member.server.id
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
except (IndexError, TypeError, KeyError):
|
2016-10-31 17:32:33 +13:00
|
|
|
return
|
|
|
|
|
|
|
|
channel = server.get_channel(channel_id)
|
2017-03-17 18:21:30 +13:00
|
|
|
await self.bot.send_message(channel, "{0} has left the server, I hope it wasn't because of something I said :c".format(member.display_name))
|
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))
|