1
0
Fork 0
mirror of synced 2024-05-15 18:12:31 +12:00
Bonfire/cogs/events.py

145 lines
4.5 KiB
Python
Raw Normal View History

import aiohttp
import logging
import json
2017-05-09 09:54:24 +12:00
import discord
2019-02-24 09:13:10 +13:00
from utils import config
from discord.ext import commands
log = logging.getLogger()
discord_bots_url = "https://bots.discord.pw/api/bots/{}/stats"
2018-05-24 17:09:42 +12:00
discordbots_url = "https://discordbots.org/api/bots/{}/stats"
carbonitex_url = "https://www.carbonitex.net/discord/data/botdata.php"
2016-08-07 10:20:23 +12:00
2019-02-24 09:13:10 +13:00
class StatsUpdate(commands.Cog):
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
def __init__(self, bot):
self.bot = bot
self.session = aiohttp.ClientSession()
2016-08-07 10:20:23 +12:00
2019-02-24 09:13:10 +13:00
def cog_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)
2018-05-24 17:09:42 +12:00
# Carbonitex request
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
2018-05-24 17:09:42 +12:00
# Discord.bots.pw request
payload = json.dumps({"server_count": server_count})
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)
async with self.session.post(url, data=payload, headers=headers) as resp:
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}
2018-05-24 17:09:42 +12:00
headers = {"Authorization": config.discordbots_key}
2018-05-24 17:09:42 +12:00
async with self.session.post(url, data=payload, headers=headers) as resp:
log.info(
"discordbots.com statistics retruned {} for {}".format(
resp.status, payload
)
)
2018-05-24 17:09:42 +12:00
2019-02-28 13:03:15 +13:00
@commands.Cog.listener()
2017-06-07 20:30:19 +12:00
async def on_guild_join(self, _):
await self.update()
2019-02-28 13:03:15 +13:00
@commands.Cog.listener()
2017-06-07 20:30:19 +12:00
async def on_guild_leave(self, _):
await self.update()
2019-02-28 13:03:15 +13:00
@commands.Cog.listener()
async def on_ready(self):
await self.update()
2019-02-28 13:03:15 +13:00
@commands.Cog.listener()
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,
2019-02-18 09:13:23 +13:00
join_role as role,
welcome_notifications as notify
2019-01-28 15:58:39 +13:00
FROM
guilds
WHERE
id = $1
"""
settings = await self.bot.db.fetchrow(query, member.guild.id)
if settings:
message = settings["msg"] or "Welcome to the '{server}' server {member}!"
2019-02-18 09:13:23 +13:00
if settings["notify"]:
try:
channel = member.guild.get_channel(settings["channel"])
await channel.send(
message.format(server=member.guild.name, member=member.mention)
)
2019-02-18 09:13:23 +13:00
# Forbidden for if the channel has send messages perms off
# HTTP Exception to catch any weird happenings
# Attribute Error catches when a channel is set, but that channel doesn't exist any more
except (discord.Forbidden, discord.HTTPException, AttributeError):
pass
try:
role = member.guild.get_role(settings["role"])
await member.add_roles(role)
except (discord.Forbidden, discord.HTTPException, AttributeError):
pass
2016-10-31 17:32:33 +13:00
2019-02-28 13:03:15 +13:00
@commands.Cog.listener()
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
2019-02-18 09:13:23 +13:00
goodbye_notifications = True
2019-01-28 15:58:39 +13:00
AND
id = $1
AND
COALESCE(goodbye_alerts, default_alerts) IS NOT NULL
"""
settings = await self.bot.db.fetchrow(query, member.guild.id)
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
def setup(bot):
bot.add_cog(StatsUpdate(bot))