2016-08-02 01:53:46 +12:00
|
|
|
from .utils import config
|
|
|
|
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()
|
|
|
|
|
|
|
|
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-08 12:56:24 +13:00
|
|
|
server_count = len(self.bot.guilds)
|
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
|
|
|
|
2017-06-07 20:30:19 +12:00
|
|
|
async def on_guild_join(self, _):
|
2016-09-29 12:39:34 +13:00
|
|
|
self.bot.loop.create_task(self.update())
|
2016-08-02 01:53:46 +12:00
|
|
|
|
2017-06-07 20:30:19 +12:00
|
|
|
async def on_guild_leave(self, _):
|
2016-09-29 12:39:34 +13:00
|
|
|
self.bot.loop.create_task(self.update())
|
2016-08-02 01:53:46 +12:00
|
|
|
|
|
|
|
async def on_ready(self):
|
2016-09-29 12:39:34 +13:00
|
|
|
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):
|
2017-03-06 15:45:44 +13:00
|
|
|
guild = member.guild
|
2017-06-07 20:30:19 +12:00
|
|
|
server_settings = self.bot.db.load('server_settings', key=str(guild.id))
|
2016-10-31 17:32:33 +13:00
|
|
|
|
|
|
|
try:
|
2017-03-08 19:43:40 +13:00
|
|
|
join_leave_on = server_settings['join_leave']
|
2017-03-08 17:28:30 +13:00
|
|
|
if join_leave_on:
|
2017-06-18 11:03:54 +12:00
|
|
|
# Get the notifications settings, get the welcome setting
|
|
|
|
notifications = self.bot.db.load('server_settings', key=guild.id, pluck='notifications') or {}
|
|
|
|
# Set our default to either the one set, or the default channel of the server
|
2017-08-01 13:39:10 +12:00
|
|
|
default_channel_id = notifications.get('default')
|
2017-06-18 11:03:54 +12:00
|
|
|
# If it is has been overriden by picarto notifications setting, use this
|
|
|
|
channel_id = notifications.get('welcome') or default_channel_id
|
2017-06-28 21:58:53 +12:00
|
|
|
# Get the message if it exists
|
|
|
|
join_message = self.bot.db.load('server_settings', key=guild.id, pluck='welcome_message')
|
|
|
|
if not join_message:
|
2017-06-28 22:18:09 +12:00
|
|
|
join_message = "Welcome to the '{server}' server {member}!"
|
2017-03-08 17:28:30 +13:00
|
|
|
else:
|
|
|
|
return
|
2017-03-08 21:19:47 +13:00
|
|
|
except (IndexError, TypeError, KeyError):
|
2016-10-31 17:32:33 +13:00
|
|
|
return
|
|
|
|
|
2017-03-08 20:55:22 +13:00
|
|
|
channel = guild.get_channel(int(channel_id))
|
2017-04-09 14:51:39 +12:00
|
|
|
try:
|
2017-06-28 22:11:16 +12:00
|
|
|
await channel.send(join_message.format(server=guild.name, member=member.mention))
|
2017-08-01 13:39:10 +12:00
|
|
|
except (discord.Forbidden, discord.HTTPException, AttributeError):
|
2017-04-09 14:51:39 +12:00
|
|
|
pass
|
2016-10-31 17:32:33 +13:00
|
|
|
|
|
|
|
async def on_member_remove(self, member):
|
2017-03-06 15:45:44 +13:00
|
|
|
guild = member.guild
|
2017-06-07 20:30:19 +12:00
|
|
|
server_settings = self.bot.db.load('server_settings', key=str(guild.id))
|
2016-10-31 17:32:33 +13:00
|
|
|
|
|
|
|
try:
|
2017-03-08 19:43:40 +13:00
|
|
|
join_leave_on = server_settings['join_leave']
|
2017-03-08 17:28:30 +13:00
|
|
|
if join_leave_on:
|
2017-06-18 11:03:54 +12:00
|
|
|
# Get the notifications settings, get the welcome setting
|
|
|
|
notifications = self.bot.db.load('server_settings', key=guild.id, pluck='notifications') or {}
|
|
|
|
# Set our default to either the one set, or the default channel of the server
|
2017-08-01 13:39:10 +12:00
|
|
|
default_channel_id = notifications.get('default')
|
2017-06-18 11:03:54 +12:00
|
|
|
# If it is has been overriden by picarto notifications setting, use this
|
|
|
|
channel_id = notifications.get('welcome') or default_channel_id
|
2017-06-28 21:58:53 +12:00
|
|
|
# Get the message if it exists
|
|
|
|
leave_message = self.bot.db.load('server_settings', key=guild.id, pluck='goodbye_message')
|
|
|
|
if not leave_message:
|
|
|
|
leave_message = "{member} has left the server, I hope it wasn't because of something I said :c"
|
2017-03-08 17:28:30 +13:00
|
|
|
else:
|
|
|
|
return
|
2017-03-08 21:19:47 +13:00
|
|
|
except (IndexError, TypeError, KeyError):
|
2016-10-31 17:32:33 +13:00
|
|
|
return
|
|
|
|
|
2017-03-08 20:55:22 +13:00
|
|
|
channel = guild.get_channel(int(channel_id))
|
2017-04-09 14:51:39 +12:00
|
|
|
try:
|
2017-06-30 07:50:12 +12:00
|
|
|
await channel.send(leave_message.format(server=guild.name, member=member.name))
|
2017-08-01 13:39:10 +12:00
|
|
|
except (discord.Forbidden, discord.HTTPException, AttributeError):
|
2017-04-09 14:51:39 +12:00
|
|
|
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))
|