change to event type monitoring / new commands doing channel related stuff
This commit is contained in:
parent
9092e68ed4
commit
4a5c3a4552
167
myTS3.py
167
myTS3.py
|
@ -3,13 +3,14 @@ TBD
|
|||
"""
|
||||
|
||||
__author__ = "Lukas Mahler"
|
||||
__version__ = "0.1.0"
|
||||
__date__ = "19.09.2021"
|
||||
__version__ = "0.2.0"
|
||||
__date__ = "23.09.2021"
|
||||
__email__ = "m@hler.eu"
|
||||
__status__ = "Development"
|
||||
|
||||
# Default
|
||||
import os
|
||||
import json
|
||||
from time import sleep
|
||||
|
||||
# Custom
|
||||
|
@ -33,7 +34,7 @@ class MyTeamspeakBot:
|
|||
|
||||
self.myid = None
|
||||
self.running = True
|
||||
self.intro = "< Keep this chat open to use commands. >"
|
||||
self.intro = "<Keep this chat open to use commands>"
|
||||
|
||||
print(f"* Trying to connect to: {self.host}:{self.port}")
|
||||
|
||||
|
@ -57,28 +58,32 @@ class MyTeamspeakBot:
|
|||
# Find my client id
|
||||
me = self.bot.clientfind(pattern=self.nickname)
|
||||
if len(me) == 1:
|
||||
self.myid = me["clid"][0]
|
||||
self.myid = me[0]["clid"]
|
||||
else:
|
||||
raise ValueError("x Can't find my own client id.")
|
||||
|
||||
''' if you want to move the Bot to a certain channel (instead of the defualt channel, you can do: '''
|
||||
# ts3conn.clientmove(clid=selfid,cid=129)
|
||||
# self.bot.clientmove(clid=self.myid,cid=129)
|
||||
|
||||
# Subscribe to a certain channel
|
||||
# Subscribe to a server movement events
|
||||
self.bot.servernotifyregister(event="server")
|
||||
|
||||
|
||||
# Subscribe to privat chat messages
|
||||
self.bot.servernotifyregister(event="textprivate")
|
||||
|
||||
# Subscribe to chat channel messages
|
||||
self.bot.servernotifyregister(event="textchannel")
|
||||
|
||||
# Subscribe to channel movement events
|
||||
# ts3conn.servernotifyregister(event="channel",id_=0)
|
||||
# self.bot.servernotifyregister(event="channel", id_=0)
|
||||
|
||||
# Notify connected admins
|
||||
sleep(5)
|
||||
self.notifyAdmin()
|
||||
|
||||
# ----------- LOOP HERE -------------
|
||||
while self.running:
|
||||
# ts3conn.send_keepalive()
|
||||
# self.bot.send_keepalive()
|
||||
print("* Waiting for a new Event...")
|
||||
self.bot.version()
|
||||
|
||||
|
@ -91,36 +96,46 @@ class MyTeamspeakBot:
|
|||
except ts3.query.TS3TimeoutError:
|
||||
pass
|
||||
else:
|
||||
print(f"* Got Event | length={len(event[0])}")
|
||||
event_type = event.event
|
||||
print(f"* Got Event | length={len(event[0])} | {event_type}")
|
||||
|
||||
if len(event[0]) > 15:
|
||||
if event[0]["reasonid"] == "0":
|
||||
print(f"* Client [{event[0]['client_nickname']}] connected.")
|
||||
# Client Connect
|
||||
if event_type == "notifycliententerview":
|
||||
print(f"* Client [{event[0]['client_nickname']}] connected.")
|
||||
|
||||
# Check if the connector is a ServerQuery or not
|
||||
if not self.isqueryclient(event[0]["client_unique_identifier"]):
|
||||
print(f"* {event[0]}")
|
||||
# Check if the connector is an Admin
|
||||
if self.isadmin(event[0]["client_database_id"]):
|
||||
self.bot.sendtextmessage(targetmode=1, target=event[0]["clid"], msg=self.intro)
|
||||
else:
|
||||
pass
|
||||
# Check if the connector is a ServerQuery or not
|
||||
if not self.isqueryclient(event[0]["client_unique_identifier"]):
|
||||
print(f"* {event[0]}")
|
||||
# Check if the connector is an Admin
|
||||
if self.isadmin(event[0]["client_database_id"]):
|
||||
self.bot.sendtextmessage(targetmode=1, target=event[0]["clid"], msg=self.intro)
|
||||
else:
|
||||
pass
|
||||
else:
|
||||
pass
|
||||
|
||||
# Message Event
|
||||
elif len(event[0]) == 6:
|
||||
# Client disconnected
|
||||
elif event_type == "notifyclientleftview":
|
||||
print(f"* Clientid [{event[0]['clid']}] disconnected.")
|
||||
pass
|
||||
|
||||
# Text Message
|
||||
elif event_type == "notifytextmessage":
|
||||
msg = event[0]["msg"]
|
||||
invkr = event[0]["invokername"]
|
||||
print(f'* From: "{invkr}"\nMessage: "{msg}"')
|
||||
self.lookupcommand(msg, invkr)
|
||||
invkr_id = event[0]["invokerid"]
|
||||
print(f'* From: "{invkr}" | Message: "{msg}"')
|
||||
self.lookupcommand(msg, invkr_id)
|
||||
|
||||
def stop(self, invkr):
|
||||
else:
|
||||
print(f"* Unknown Event: {event.__dict__}")
|
||||
|
||||
def stop(self, invkr_id):
|
||||
"""
|
||||
|
||||
"""
|
||||
msg = "I'm out, bye bye!"
|
||||
self.bot.sendtextmessage(targetmode=1, target=invkr, msg=msg)
|
||||
self.bot.sendtextmessage(targetmode=1, target=invkr_id, msg=msg)
|
||||
self.running = False
|
||||
|
||||
def notifyAdmin(self):
|
||||
|
@ -143,6 +158,7 @@ class MyTeamspeakBot:
|
|||
for clid in clients:
|
||||
try:
|
||||
self.bot.clientpoke(msg=msg, clid=clid)
|
||||
# TODO
|
||||
except:
|
||||
pass
|
||||
|
||||
|
@ -186,9 +202,44 @@ class MyTeamspeakBot:
|
|||
i += 1
|
||||
return None
|
||||
|
||||
# def openmsg():
|
||||
def createChannel(self, name, permanent=False):
|
||||
"""
|
||||
|
||||
# def subscribemsg():
|
||||
"""
|
||||
if permanent:
|
||||
new = self.bot.channelcreate(channel_name=name, channel_flag_permanent="1")
|
||||
else:
|
||||
new = self.bot.channelcreate(channel_name=name)
|
||||
|
||||
return new[0]["cid"]
|
||||
|
||||
def editChannelname(self, cid, name):
|
||||
"""
|
||||
|
||||
"""
|
||||
self.bot.channeledit(cid=cid, channel_name=name)
|
||||
|
||||
def list(self, what, invkr_id):
|
||||
"""
|
||||
|
||||
"""
|
||||
if what == "channel":
|
||||
mydict = {}
|
||||
channels = self.bot.channellist()
|
||||
for channel in channels:
|
||||
order = channel["channel_order"]
|
||||
mydict[order] = [channel["channel_name"], channel["cid"]]
|
||||
|
||||
mydict = dict(sorted(mydict.items()))
|
||||
msg = json.dumps(mydict)
|
||||
|
||||
elif what == "clients":
|
||||
msg = self.bot.clientlist()
|
||||
# TODO
|
||||
else:
|
||||
msg = None
|
||||
|
||||
self.bot.sendtextmessage(targetmode=1, target=invkr_id, msg=msg)
|
||||
|
||||
@staticmethod
|
||||
def isqueryclient(cluid):
|
||||
|
@ -196,7 +247,7 @@ class MyTeamspeakBot:
|
|||
Check if the given client-uid is a query client
|
||||
"""
|
||||
|
||||
# client = ts3conn.clientlist(uid=uid)
|
||||
# client = self.bot.clientlist(uid=uid)
|
||||
# print(client[0])
|
||||
# if client[0]["client_type"] == "1":
|
||||
if cluid == "ServerQuery":
|
||||
|
@ -225,7 +276,7 @@ class MyTeamspeakBot:
|
|||
print("* ISADMIN: False")
|
||||
return False
|
||||
|
||||
def lookupcommand(self, msg, invkr):
|
||||
def lookupcommand(self, msg, invkr_id):
|
||||
"""
|
||||
|
||||
"""
|
||||
|
@ -234,8 +285,7 @@ class MyTeamspeakBot:
|
|||
commandstring = msg.split(" ")
|
||||
command = commandstring[0]
|
||||
parameter = commandstring[1:]
|
||||
print(f"* {command}")
|
||||
print(f"* {parameter}")
|
||||
print(f"* command: {command} / parameter: {parameter} / invkr_id: {invkr_id}")
|
||||
|
||||
if command == "!annoy":
|
||||
try:
|
||||
|
@ -244,14 +294,38 @@ class MyTeamspeakBot:
|
|||
self.poke(msg=msg, usr=target)
|
||||
except IndexError:
|
||||
err = "Please use the command like this: !annoy TARGET MESSAGE"
|
||||
self.bot.sendtextmessage(targetmode=1, target=invkr, msg=err)
|
||||
self.bot.sendtextmessage(targetmode=1, target=invkr_id, msg=err)
|
||||
pass
|
||||
|
||||
elif command == "!kickall":
|
||||
self.kickall("test")
|
||||
self.kickall("test") # TODO
|
||||
|
||||
elif command == "!test":
|
||||
cid = self.createChannel("Test")
|
||||
self.bot.sendtextmessage(targetmode=1, target=invkr_id, msg=cid)
|
||||
|
||||
elif command == "!btc":
|
||||
self.editChannelname(200, f"[cspacerBTC]Bitcoin: {50000}€") # TODO
|
||||
|
||||
elif command == "!eth":
|
||||
self.editChannelname(201, f"[cspacerETH]Ethereum: {3000}€") # TODO
|
||||
|
||||
elif command == "!list":
|
||||
try:
|
||||
self.list(parameter[0], invkr_id)
|
||||
except IndexError:
|
||||
err = "Please use the command like this: !list channel/clients"
|
||||
self.bot.sendtextmessage(targetmode=1, target=invkr_id, msg=err)
|
||||
pass
|
||||
|
||||
elif command == "!follow":
|
||||
pass # TODO
|
||||
|
||||
elif command == "!rename":
|
||||
pass # TODO
|
||||
|
||||
elif command == "!stop" or command == "!quit" or command == "!q":
|
||||
self.stop(invkr)
|
||||
self.stop(invkr_id)
|
||||
|
||||
elif command == "!pingall":
|
||||
try:
|
||||
|
@ -259,12 +333,12 @@ class MyTeamspeakBot:
|
|||
self.poke(msg=msg)
|
||||
except IndexError:
|
||||
err = "Please use the command like this: !pingall MESSAGE"
|
||||
self.bot.sendtextmessage(targetmode=1, target=invkr, msg=err)
|
||||
self.bot.sendtextmessage(targetmode=1, target=invkr_id, msg=err)
|
||||
pass
|
||||
|
||||
else:
|
||||
err = "Unknown Command:[{0}]".format(command)
|
||||
self.bot.sendtextmessage(targetmode=1, target=invkr, msg=err)
|
||||
err = f"Unknown Command: [{command}]"
|
||||
self.bot.sendtextmessage(targetmode=1, target=invkr_id, msg=err)
|
||||
|
||||
def printable_clientinfo(self, client):
|
||||
"""
|
||||
|
@ -309,16 +383,17 @@ def main():
|
|||
raise FileNotFoundError("could not locate .env file")
|
||||
dotenv.load_dotenv(dotenv_file)
|
||||
dotend_keys = dotenv.dotenv_values()
|
||||
if not {"HOST", "PORT", "USER", "PWD", "SID"} <= dotend_keys.keys():
|
||||
|
||||
if not {"_HOST", "_PORT", "_USER", "_PWD", "_SID"} <= dotend_keys.keys():
|
||||
raise ValueError("missing keys in your .env file.")
|
||||
|
||||
# Config
|
||||
conf = dict(host=os.getenv("HOST"),
|
||||
port=os.getenv("PORT"),
|
||||
user=os.getenv("USER"),
|
||||
pwd=os.getenv("PWD"),
|
||||
sid=os.getenv("SID"),
|
||||
name=os.getenv("NAME"))
|
||||
conf = dict(host=os.getenv("_HOST"),
|
||||
port=os.getenv("_PORT"),
|
||||
user=os.getenv("_USER"),
|
||||
pwd=os.getenv("_PWD"),
|
||||
sid=os.getenv("_SID"),
|
||||
name=os.getenv("_NAME"))
|
||||
|
||||
# Start the Bot Instance
|
||||
abot = MyTeamspeakBot(conf)
|
||||
|
|
Loading…
Reference in New Issue