1
0
Fork 0
mirror of synced 2024-06-02 02:34:32 +12:00
Bonfire/cogs/events.py

97 lines
3.1 KiB
Python
Raw Normal View History

from .utils import config
import aiohttp
import logging
import json
2017-05-09 09:54:24 +12:00
import discord
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-07 10:20:23 +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
def __init__(self, bot):
self.bot = bot
self.session = aiohttp.ClientSession()
2016-08-07 10:20:23 +12:00
def __unload(self):
self.bot.loop.create_task(self.session.close())
async def update(self):
2017-03-08 12:56:24 +13:00
server_count = len(self.bot.guilds)
2016-08-19 13:56:41 +12:00
carbon_payload = {
'key': config.carbon_key,
'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
payload = json.dumps({
'server_count': server_count
})
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)
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, _):
self.bot.loop.create_task(self.update())
2017-06-07 20:30:19 +12:00
async def on_guild_leave(self, _):
self.bot.loop.create_task(self.update())
async def on_ready(self):
self.bot.loop.create_task(self.update())
2016-10-31 17:32:33 +13:00
async def on_member_join(self, member):
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:
join_leave_on = server_settings['join_leave']
2017-03-08 17:28:30 +13:00
if join_leave_on:
2017-06-07 20:30:19 +12:00
channel_id = server_settings.get('notifications_channel') or member.guild.id
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
channel = guild.get_channel(int(channel_id))
2017-04-09 14:51:39 +12:00
try:
await channel.send("Welcome to the '{0.guild.name}' server {0.mention}!".format(member))
except (discord.Forbidden, discord.HTTPException):
pass
2016-10-31 17:32:33 +13:00
async def on_member_remove(self, member):
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:
join_leave_on = server_settings['join_leave']
2017-03-08 17:28:30 +13:00
if join_leave_on:
2017-06-07 20:30:19 +12:00
channel_id = server_settings.get('notifications_channel') or member.guild.id
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
channel = guild.get_channel(int(channel_id))
2017-04-09 14:51:39 +12:00
try:
await channel.send("{0} has left the server, I hope it wasn't because of something I said :c".format(member.display_name))
except (discord.Forbidden, discord.HTTPException):
pass
2016-10-31 17:32:33 +13:00
2016-08-07 10:20:23 +12:00
def setup(bot):
bot.add_cog(StatsUpdate(bot))