diff --git a/myTS3.py b/myTS3.py index e59b736..637116f 100644 --- a/myTS3.py +++ b/myTS3.py @@ -4,7 +4,7 @@ TBD __author__ = "Lukas Mahler" __version__ = "0.0.0" -__date__ = "24.10.2021" +__date__ = "30.10.2021" __email__ = "m@hler.eu" __status__ = "Development" @@ -36,16 +36,17 @@ class TSbot: self.pwd = conf["Authentication"]["pwd"] self.sgid_admin = conf["Groups"]["admins"] self.sgid_mute = conf["Groups"]["mute"] + self.crypto = conf["Misc"]["crypto"] self.nickname = conf["Misc"]["nickname"] self.whitelisted = conf["Misc"]["whitelisted"] # Initialize self - self.gecko = gecko.GeckoAPI() self.log = log self.myid = None self.running = True self.version = __version__ self.started = time.time() + self.gecko = gecko.GeckoAPI() self.crypto_update = self.started - 1800 self.pipeOut(f"Trying to connect to: {self.host}:{self.port}") @@ -117,11 +118,12 @@ class TSbot: self.bot.version() # Auto-update crypto price channels every 30 minutes - if self.crypto_update + 1800 < time.time(): - self.crypto_update = time.time() + if self.crypto: + if self.crypto_update + 1800 < time.time(): + self.crypto_update = time.time() + + commands.ticker(self, "") - self.lookupcommand(".btc", self.myid) - self.lookupcommand(".dot", self.myid) else: pass @@ -228,7 +230,7 @@ class TSbot: self.bot.clientpoke(msg=msg, clid=clid) # TODO - def poke(self, msg=None, n=10, delay=0.2, usr='all'): + def poke(self, msg=None, n=1, delay=0.2, usr='all'): """ ping a single or multiple users for n times including a msg. """ @@ -386,24 +388,24 @@ class TSbot: """ Every message starting with '.' gets passed and evaluated in this function. Commands in this function are sorted alphabetically. + Parameters in brackets are optional. Command Parameter 1 Parameter 2 --------------------------------------------------------- - .admin + .admin / .notifyAdmin .annoy target message - .btc / .eth - .dot / .ada .follow target .help / .h .info clid .kickall message - .list channel/clients/groups + .list channel/clients/commands/groups .mute target .pingall message .rename nickname .roll .stop / .quit / .q .test + .ticker (symbol) .unmute target """ @@ -412,33 +414,13 @@ class TSbot: parameter = commandstring[1:] self.pipeOut(f"command: {command} | parameter: {parameter} | invkr_id: {invkr_id}") - if command == ".admin": + # ??? passable = {"self": self, "invkr_id": invkr_id, "parameter": parameter} + + if command == ".admin" or command == ".notifyAdmin": commands.notifyAdmin(self) elif command == ".annoy": - try: - target = parameter[0] - msg = parameter[1] - 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_id, msg=err) - - elif command == ".btc" or command == ".eth": - channelname = f"[cspacerC1]BTC: {self.gecko.getSymbol('Bitcoin', decimal=0)} | " \ - f"ETH: {self.gecko.getSymbol('ethereum', decimal=0)}" - try: - self.editChannelname(200, channelname) - except ts3.query.TS3QueryError: - pass - - elif command == ".dot" or command == ".ada": - channelname = f"[cspacerC2]DOT: {self.gecko.getSymbol('polkadot', decimal=2)} | " \ - f"ADA: {self.gecko.getSymbol('cardano', decimal=2)}" - try: - self.editChannelname(201, channelname) - except ts3.query.TS3QueryError: - pass + commands.annoy(self, invkr_id, parameter) elif command == ".follow": commands.follow(self, invkr_id, parameter) @@ -453,41 +435,30 @@ class TSbot: self.kickall("test") # TODO elif command == ".list": - try: - commands.msglist(self, invkr_id, parameter) - except IndexError: - err = "Please use the command like this: .list channel/clients/groups" - self.bot.sendtextmessage(targetmode=1, target=invkr_id, msg=err) + commands.list(self, invkr_id, parameter) elif command == ".mute": commands.mute(self, invkr_id, parameter) elif command == ".pingall": - try: - msg = parameter[0] - self.poke(msg=msg) - except IndexError: - err = "Please use the command like this: .pingall MESSAGE" - self.bot.sendtextmessage(targetmode=1, target=invkr_id, msg=err) + commands.pingall(self, invkr_id, parameter) elif command == ".rename": - try: - self.nickname = parameter[0] - self.bot.clientupdate(client_nickname=self.nickname) - except IndexError: - err = "Please use the command like this: .rename NAME" - self.bot.sendtextmessage(targetmode=1, target=invkr_id, msg=err) + commands.rename(self, invkr_id, parameter) elif command == ".roll": pass # TODO needs permission for everyone + targemode implementation elif command == ".stop" or command == ".quit" or command == ".q": - self.stop(invkr_id) + commands.quit(self, invkr_id) elif command == ".test": cid = self.createChannel("Test") self.bot.sendtextmessage(targetmode=1, target=invkr_id, msg=cid) + elif command == ".ticker": + commands.ticker(self, parameter) + elif command == ".unmute": commands.unmute(self, invkr_id, parameter) diff --git a/src/commands.py b/src/commands.py index c66d48a..f5fbbc3 100644 --- a/src/commands.py +++ b/src/commands.py @@ -4,7 +4,7 @@ TBD __author__ = "Lukas Mahler" __version__ = "0.0.0" -__date__ = "24.10.2021" +__date__ = "30.10.2021" __email__ = "m@hler.eu" __status__ = "Development" @@ -22,11 +22,26 @@ import ts3 from src import util -def follow(self, invkr_id, parameter): +def annoy(self, invkr_id, parameter): """ """ + if not parameter: + err = "Please use the command like this: .annoy TARGET MESSAGE" + self.bot.sendtextmessage(targetmode=1, target=invkr_id, msg=err) + return + + target = parameter[0] + msg = parameter[1] + self.poke(n=10, msg=msg, usr=target) + + +def follow(self, invkr_id, parameter): + """ + TODO sticky folgen + """ + if not parameter: err = "Please use the command like this: .follow TARGET" self.bot.sendtextmessage(targetmode=1, target=invkr_id, msg=err) @@ -42,6 +57,7 @@ def help(self, invkr_id): """ """ + # Find all functions in this submodule cmds = [f[0] for f in inspect.getmembers(sys.modules[__name__], inspect.isfunction)] msg = f"\n\nList of commands:\n---------------------\n.{f'{chr(10)}.'.join(cmds)}" @@ -52,17 +68,22 @@ def info(self, invkr_id): """ """ - # TODO implement more then just the runtime + msg = f"\n\nRuntime: {util.getRuntime(self.started)}\n" \ f"Version: {self.version}" self.bot.sendtextmessage(targetmode=1, target=invkr_id, msg=msg) -def msglist(self, invkr_id, parameter): +def list(self, invkr_id, parameter): """ - Message the invoker of the function either a list of channels or a list of clients. + Message the invoker of the function either a list of channels, clients, commands or server groups. """ + if not parameter: + err = "Please use the command like this: .list channel/clients/commands/groups/" + self.bot.sendtextmessage(targetmode=1, target=invkr_id, msg=err) + return + what = parameter[0] mydict = {} @@ -82,6 +103,10 @@ def msglist(self, invkr_id, parameter): mydict = dict(sorted(mydict.items())) msg = json.dumps(mydict) + elif what == "commands": + help(self, invkr_id) + return + elif what == "groups": groups = self.bot.servergrouplist() for group in groups: @@ -89,7 +114,7 @@ def msglist(self, invkr_id, parameter): msg = json.dumps(mydict) else: - msg = None + msg = f"The parameter [{what}] is not supported." self.bot.sendtextmessage(targetmode=1, target=invkr_id, msg=msg) @@ -116,9 +141,23 @@ def mute(self, invkr_id, parameter): return +def pingall(self, invkr_id, parameter): + """ + + """ + + if not parameter: + err = "Please use the command like this: .pingall MESSAGE" + self.bot.sendtextmessage(targetmode=1, target=invkr_id, msg=err) + return + + msg = parameter[0] + self.poke(msg=msg) + + def notifyAdmin(self): """ - Ping every available admin. + Message every available admin. """ clients = self.bot.clientlist() @@ -134,6 +173,55 @@ def notifyAdmin(self): time.sleep(1) +def quit(self, invkr_id): + """ + + """ + self.stop(invkr_id) + + +def rename(self, invkr_id, parameter): + """ + Rename the Bot to the given Nickname + """ + + if not parameter: + err = "Please use the command like this: .rename NICKNAME" + self.bot.sendtextmessage(targetmode=1, target=invkr_id, msg=err) + return + + self.nickname = parameter[0] + self.bot.clientupdate(client_nickname=self.nickname) + + +def ticker(self, parameter): + """ + Refresh the Crypto Ticker channels, + this is very specific to my use, you can disable this in the config. + """ + + if parameter: + symbol = parameter[0].lower() + else: + symbol = "all" + + if symbol in ["btc", "bitcoin", "eth", "ethereum", "all"]: + channelname = f"[cspacerC1]BTC: {self.gecko.getSymbol('bitcoin', decimal=0)} | " \ + f"ETH: {self.gecko.getSymbol('ethereum', decimal=0)}" + try: + self.editChannelname(200, channelname) + except ts3.query.TS3QueryError: + pass + + if symbol in ["dot", "polkadot", "ada", "cardano", "all"]: + channelname = f"[cspacerC2]DOT: {self.gecko.getSymbol('polkadot', decimal=2)} | " \ + f"ADA: {self.gecko.getSymbol('cardano', decimal=2)}" + try: + self.editChannelname(201, channelname) + except ts3.query.TS3QueryError: + pass + + def unmute(self, invkr_id, parameter): """ Remove the mute group to a user. diff --git a/src/template.toml b/src/template.toml index 4dd2d54..2200916 100644 --- a/src/template.toml +++ b/src/template.toml @@ -13,4 +13,5 @@ mute = "0" [Misc] nickname = "myTS3-Bot" -whitelisted = false \ No newline at end of file +whitelisted = false +crypto = false \ No newline at end of file