import discord
import requests
import time
import json
from ...schemas import ATC
from discord.ext import commands, tasks
from .. import AutoModPluginBlueprint, ShardedBotInstance
from datetime import datetime, timezone, timedelta, date
class Controller():
def __init__(self, id, callsign, controller_name, frequency):
self.id = id
self.callsign = callsign
self.controller_name = controller_name
self.frequency = frequency
def get_id(self):
return self.id
def get_callsign(self):
return self.callsign
def get_controller_name(self):
return self.controller_name
def get_frequency(self):
return self.frequency
class ATCAnnoucementsPlugin(AutoModPluginBlueprint):
"""Plugin to display when new ATC comes online."""
def __init__(self, bot: ShardedBotInstance) -> None:
super().__init__(bot)
self.ATCAnnoucements.start()
@tasks.loop(seconds=5)
async def ATCAnnoucements(self):
online_2_cs = []
online_2_obj = []
cs = []
callsign_prefix = self.bot.config.callsign_prefix
positions = ["DEL", "GND", "TWR", "APP", "DEP", "CTR", "FSS"]
atc_channel = self.bot.get_channel(self.bot.config.atc_channel)
request = requests.get('https://data.vatsim.net/v3/vatsim-data.json').json()
controllers = request['controllers']
online_exists = False
controllers_counter = 0
embed = discord.Embed(
title="ATC Annoucement",
color = discord.Colour.blue()
)
for a in controllers:
id = a['cid']
callsign = a['callsign']
controller_name = a['name']
frequency = a['frequency']
for i in callsign_prefix:
if i == callsign[:len(i)] and callsign[-3:] in positions:
controller_obj = Controller(id, callsign, controller_name, frequency)
online_2_obj.append(controller_obj)
online_2_cs.append(callsign)
for i in online_2_obj:
for controller in self.bot.db.atc.find({}):
controller_cs = controller["callsign"]
controller_id = controller["_id"]
cs.append(controller_cs)
if i.get_callsign() not in cs:
online_exists = True
controllers_counter += 1
controllers = self.bot.db.atc.insert(ATC(i.get_id(), i.get_callsign(), i.get_controller_name(), i.get_frequency()))
embed.add_field(
name=f":id: Callsign:{' '} `{i.get_callsign()}`{' '} |{' '} :man: Controller {' '}`{i.get_controller_name()}`{' '} | {' '} :radio: Frequency {' '}`{i.get_frequency()}`",
value=f"\uFEFF",
inline=False
)
if online_exists:
embed.set_footer(
text=f"Number of new controllers: {controllers_counter}"
)
await atc_channel.send(embed=embed)
for i in cs:
if i not in online_2_cs:
self.bot.db.atc.delete_one( { callsign: f"{i}" } )
embed = discord.Embed(
title=f"ATC Annoucement",
description=f"Controller just went offline. :cry:",
color = discord.Colour.blue()
)
embed.add_field(
name=f":id: {i}",
value=f"\uFEFF",
inline=False
)
print(f"Deleted offline atc")
await atc_channel.send(embed=embed)
async def setup(bot: ShardedBotInstance) -> None:
await bot.register_plugin(ATCAnnoucementsPlugin(bot))