start outsourcing commands

This commit is contained in:
Lukas 2021-10-07 14:39:05 +02:00
parent 78f82d64e5
commit 890d2c47a4
2 changed files with 109 additions and 69 deletions

View File

@ -18,7 +18,7 @@ import os.path
import ts3
# Self
from src import util, gecko
from src import util, gecko, commands
class TSbot:
@ -214,23 +214,6 @@ class TSbot:
self.pipeOut(msg)
self.running = False
def notifyAdmin(self):
"""
Ping every available admin.
"""
clients = self.bot.clientlist()
clients = [client for client in clients if client["client_type"] != "1"]
for client in clients:
cldbid = client["client_database_id"]
clid = client["clid"]
if self.isadmin(cldbid):
msg = "<Keep this chat open to use commands>"
self.bot.sendtextmessage(targetmode=1, target=clid, msg=msg)
if not self.whitelisted:
time.sleep(1)
def kickall(self, msg):
"""
TODO
@ -312,32 +295,6 @@ class TSbot:
except Exception as e:
self.pipeOut(e, lvl="error")
def list(self, what, invkr_id):
"""
Message the invoker of the function either a list of channels or a list of clients.
"""
mydict = {}
if what == "channel":
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":
clients = self.bot.clientlist()
for client in clients:
mydict[client["client_nickname"]] = {"clid": client["clid"], "cldbid": client["client_database_id"]}
mydict = dict(sorted(mydict.items()))
msg = json.dumps(mydict)
else:
msg = None
self.bot.sendtextmessage(targetmode=1, target=invkr_id, msg=msg)
def isqueryclient(self, clid):
"""
Check if the given client-uid is a query client.
@ -406,7 +363,7 @@ class TSbot:
self.pipeOut(f"command: {command} | parameter: {parameter} | invkr_id: {invkr_id}")
if command == ".admin":
self.notifyAdmin()
commands.notifyAdmin(self)
elif command == ".annoy":
try:
@ -435,38 +392,17 @@ class TSbot:
pass
elif command == ".follow":
if not parameter[0]:
err = "Please use the command like this: .follow TARGET"
self.bot.sendtextmessage(targetmode=1, target=invkr_id, msg=err)
return
target = parameter[0]
try:
clients = self.bot.clientfind(pattern=target)
clids = [client["clid"] for client in clients]
if len(clids) == 1:
cid = self.bot.clientinfo(clids[0])["cid"]
self.bot.clientmove(clid=self.myid, cid=cid)
else:
self.pipeOut(f"Found multiple matching clients using pattern {target}:{clids}")
return
except ts3.query.TS3QueryError as e:
self.pipeOut(f"No client found using pattern {target},"
f"returned error:\n{e}", lvl="ERROR")
return
commands.follow(self, invkr_id, parameter)
elif command == ".info":
# TODO implement more then just the runtime
msg = f"Runtime: {util.getRuntime(self.started)}"
self.bot.sendtextmessage(targetmode=1, target=invkr_id, msg=msg)
commands.info(self, invkr_id)
elif command == ".kickall":
self.kickall("test") # TODO
elif command == ".list":
try:
self.list(parameter[0], invkr_id)
commands.msglist(self, invkr_id, parameter)
except IndexError:
err = "Please use the command like this: .list channel/clients"
self.bot.sendtextmessage(targetmode=1, target=invkr_id, msg=err)

104
src/commands.py Normal file
View File

@ -0,0 +1,104 @@
"""
TBD
"""
__author__ = "Lukas Mahler"
__version__ = "0.0.0"
__date__ = "07.10.2021"
__email__ = "m@hler.eu"
__status__ = "Development"
# Default
import json
import time
# Custom
import ts3
# Self
import util
def follow(self, invkr_id, parameter):
"""
"""
if not parameter[0]:
err = "Please use the command like this: .follow TARGET"
self.bot.sendtextmessage(targetmode=1, target=invkr_id, msg=err)
return
target = parameter[0]
try:
clients = self.bot.clientfind(pattern=target)
clids = [client["clid"] for client in clients]
if len(clids) == 1:
cid = self.bot.clientinfo(clids[0])["cid"]
self.bot.clientmove(clid=self.myid, cid=cid)
else:
self.pipeOut(f"Found multiple matching clients using pattern {target}:{clids}")
return
except ts3.query.TS3QueryError as e:
self.pipeOut(f"No client found using pattern {target},"
f"returned error:\n{e}", lvl="ERROR")
return
def info(self, invkr_id):
# TODO implement more then just the runtime
msg = f"Runtime: {util.getRuntime(self.started)}"
self.bot.sendtextmessage(targetmode=1, target=invkr_id, msg=msg)
def msglist(self, invkr_id, parameter):
"""
Message the invoker of the function either a list of channels or a list of clients.
"""
what = parameter[0]
mydict = {}
if what == "channel":
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":
clients = self.bot.clientlist()
for client in clients:
mydict[client["client_nickname"]] = {"clid": client["clid"], "cldbid": client["client_database_id"]}
mydict = dict(sorted(mydict.items()))
msg = json.dumps(mydict)
else:
msg = None
self.bot.sendtextmessage(targetmode=1, target=invkr_id, msg=msg)
def notifyAdmin(self):
"""
Ping every available admin.
"""
clients = self.bot.clientlist()
clients = [client for client in clients if client["client_type"] != "1"]
for client in clients:
cldbid = client["client_database_id"]
clid = client["clid"]
if self.isadmin(cldbid):
msg = "<Keep this chat open to use commands>"
self.bot.sendtextmessage(targetmode=1, target=clid, msg=msg)
if not self.whitelisted:
time.sleep(1)
if __name__ == "__main__":
exit()