Skip to content

Instantly share code, notes, and snippets.

@MitchBarnett
Last active February 6, 2019 02:41
Show Gist options
  • Save MitchBarnett/c4227a8b8d16b325b8448a4e42b359b8 to your computer and use it in GitHub Desktop.
Save MitchBarnett/c4227a8b8d16b325b8448a4e42b359b8 to your computer and use it in GitHub Desktop.
import ts3
import tkinter
from tkinter import messagebox
from configparser import ConfigParser
class MainWindow:
def __init__(self):
self.window = tkinter.Tk()
self.window.title("3CB teamspeak alerter")
self.alert_tech_help = tkinter.BooleanVar()
self.alert_waiting_room = tkinter.BooleanVar()
self.alert_radio_comms = tkinter.BooleanVar()
self.label_server_total = tkinter.Label(self.window, text="Server Total: 0")
self.label_tech_help = tkinter.Label(self.window, text="Tech Help: 0")
self.label_waiting_room = tkinter.Label(self.window, text="Waiting Room: 0")
self.label_radio_comms = tkinter.Label(self.window, text="Radio Comms: 0")
self.checkbox_tech_help = tkinter.Checkbutton(self.window, text='Alert?', var=self.alert_tech_help,
command=self.update_layout)
self.checkbox_waiting_room = tkinter.Checkbutton(self.window, text='Alert?', var=self.alert_waiting_room,
command=self.update_layout)
self.checkbox_radio_comms = tkinter.Checkbutton(self.window, text='Alert?', var=self.alert_radio_comms,
command=self.update_layout)
self.value_select_tech_help = tkinter.Spinbox(self.window, from_=1, to=10, width=5)
self.value_select_waiting_room = tkinter.Spinbox(self.window, from_=1, to=10, width=5)
self.value_select_radio_comms = tkinter.Spinbox(self.window, from_=1, to=10, width=5)
self.label_techHelp_threshold = tkinter.Label(self.window, text="threshold")
self.label_waiting_room_threshold = tkinter.Label(self.window, text="threshold")
self.label_radio_comms_threshold = tkinter.Label(self.window, text="threshold")
def update_layout(self):
self.label_server_total.grid(columnspan=2, row=0, column=0)
self.label_tech_help.grid(row=1, column=0)
self.label_waiting_room.grid(row=2, column=0)
self.label_radio_comms.grid(row=3, column=0)
self.checkbox_tech_help.grid(row=1, column=1)
self.checkbox_waiting_room.grid(row=2, column=1)
self.checkbox_radio_comms.grid(row=3, column=1)
if self.alert_tech_help.get():
self.value_select_tech_help.grid(row=1, column=2)
else:
self.value_select_tech_help.grid_forget()
if self.alert_waiting_room.get():
self.value_select_waiting_room.grid(row=2, column=2)
else:
self.value_select_waiting_room.grid_forget()
if self.alert_radio_comms.get():
self.value_select_radio_comms.grid(row=3, column=2)
else:
self.value_select_radio_comms.grid_forget()
def update_values(self, total=0, tech_help=0, waiting_room=0, radio_comms=0):
self.label_server_total.config(text="Server Total: " + str(total))
self.label_tech_help.config(text="Tech Help: " + str(tech_help))
self.label_waiting_room.config(text="Waiting Room: " + str(waiting_room))
self.label_radio_comms.config(text="Radio Comms: " + str(radio_comms))
self.window.update()
def do_alerts(self, tech_help=0, waiting_room=0, radio_comms=0):
if self.alert_tech_help.get() and tech_help >= int(self.value_select_tech_help.get()):
tkinter.messagebox.showinfo("Tech Help Alert", "The tech help channel has reached the threshold")
if self.alert_waiting_room.get() and waiting_room >= int(self.value_select_waiting_room.get()):
tkinter.messagebox.showinfo("Waiting Room Alert", "The waiting room channel has reached the threshold")
if self.alert_radio_comms.get() and radio_comms >= int(self.value_select_radio_comms.get()):
tkinter.messagebox.showinfo("Radio Comms Alert", "The radio comms channel has reached the threshold")
class Program:
def __init__(self):
self.main_window = MainWindow()
self.main_window.update_layout()
try:
self.server_connection = ts3.query.TS3ServerConnection("voice.3commandobrigade.com")
self.server_connection.exec_("use", sid=1)
except Exception as e:
print(e)
raise SystemExit
def check_clients(self):
clients = self.server_connection.query("clientlist").all()
tech_help = 0
waiting_room = 0
radio_comms = 0
total = 0
for client in clients:
if client["client_type"] == "0": # A real (non query client)
total += 1
if client["cid"] == "231": # In tech help channel
tech_help += 1
elif client["cid"] == "806": # In waiting room channel
waiting_room += 1
elif client["cid"] == "790": # In radio comms channel
radio_comms += 1
self.main_window.update_values(total, tech_help, waiting_room, radio_comms)
self.main_window.do_alerts(tech_help, waiting_room, radio_comms)
self.main_window.window.after(5000, self.check_clients)
def load_settings(self):
config = ConfigParser()
config.read('settings.ini')
config['settings'] = {'alert_tech_help': True,
'alert_waiting_room': False,
'alert_radio_comms': False,
'tech_help_threshold': 1,
'waiting_room_threshold': 3,
'radio_comms_threshold': 6}
settings = config['settings']
self.main_window.alert_tech_help.set(settings.getboolean('alert_tech_help'))
self.main_window.alert_waiting_room.set(config.getboolean('settings', 'alert_waiting_room'))
self.main_window.alert_radio_comms.set(config.getboolean('settings', 'alert_radio_comms'))
self.main_window.value_select_tech_help.delete(0, "end")
self.main_window.value_select_tech_help.insert(0, config.getint('settings', 'tech_help_threshold'))
self.main_window.value_select_waiting_room.delete(0, "end")
self.main_window.value_select_waiting_room.insert(0, config.getint('settings', 'waiting_room_threshold'))
self.main_window.value_select_radio_comms.delete(0, "end")
self.main_window.value_select_radio_comms.insert(0, config.getint('settings', 'radio_comms_threshold'))
print(config.get('settings', 'waiting_room_threshold'))
print(config.get('settings', 'radio_comms_threshold'))
self.main_window.update_layout()
def save_and_exit(self):
config = ConfigParser()
config['settings'] = {
'alert_tech_help': self.main_window.alert_tech_help.get(),
'alert_waiting_room': self.main_window.alert_waiting_room.get(),
'alert_radio_comms': self.main_window.alert_radio_comms.get(),
'tech_help_threshold': self.main_window.value_select_tech_help.get(),
'waiting_room_threshold': self.main_window.value_select_waiting_room.get(),
'radio_comms_threshold': self.main_window.value_select_radio_comms.get()
}
self.main_window.window.destroy()
with open('settings.ini', 'w') as f:
config.write(f)
def run(self):
self.load_settings()
self.check_clients()
self.main_window.window.protocol("WM_DELETE_WINDOW", self.save_and_exit)
self.main_window.window.mainloop()
if __name__ == "__main__":
program = Program()
program.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment