diff --git a/modules/watcher.py b/modules/watcher.py index ae3acf2..025f9ca 100755 --- a/modules/watcher.py +++ b/modules/watcher.py @@ -25,13 +25,14 @@ def setup(bot): "api_url TEXT PRIMARY KEY," "name TEXT DEFAULT 'Anonymous'," "last_post INTEGER," - "time_since TEXT)") + "time_since TEXT," + "channel TEXT)") con.commit() else: for thread in watching: if get_thread_url(thread[0]) in bot.memory["watcher"].keys(): continue - t = WatcherThread(bot, thread[0], thread[1], thread[2], thread[3]) + t = WatcherThread(bot, *thread) t.start() bot.memory["watcher"][get_thread_url(thread[0])] = t con.close() @@ -79,7 +80,7 @@ def get_thread_url(api_url): @commands("watch") -@example(".watch https://boards.4chan.org/qst/thread/2") +@example(".watch https://boards.4chan.org/qst/thread/2 OPName") def watch(bot, trigger): """ A thread watcher for 4chan. @@ -101,12 +102,14 @@ def watch(bot, trigger): last_post = get_last_post(thread, name) time_since = get_time() - t = WatcherThread(bot, api_url, name, last_post, time_since) + t = WatcherThread(bot, api_url, name,last_post, time_since,trigger.channel) t.start() bot.memory["watcher"][url] = t - bot.db.execute("INSERT INTO watcher(api_url, name, last_post, time_since)" - " VALUES(?,?,?,?)", (api_url, name, last_post, time_since)) + bot.db.execute( + "INSERT INTO watcher(api_url, name, last_post, time_since, channel) " + "VALUES(?,?,?,?,?)", + (api_url, name, last_post, time_since, trigger.channel)) bot.msg("[\x0304Watcher\x03] Watching thread: \x0307" + url) @@ -136,12 +139,13 @@ def removeThread(bot, url): class WatcherThread(threading.Thread): - def __init__(self, bot, api_url, name, last_post, time_since): + def __init__(self, bot, api_url, name, last_post, time_since, channel): threading.Thread.__init__(self) self.stop = threading.Event() self.period = 20 - self._bot = bot + self.bot = bot + self.channel = channel self.api_url = api_url self.name = name self.last_post = last_post @@ -161,10 +165,10 @@ class WatcherThread(threading.Thread): continue if res.status_code == 404: - msg = "[\x0304Watcher\x03] Thread deleted: " \ - + f"\x0307{get_thread_url(self.api_url)}" - self._bot.msg(msg) - removeThread(self.bot, api_url) + msg = "[\x0304Watcher\x03] Thread deleted: " + msg += f"\x0307{get_thread_url(self.api_url)}" + self.bot.msg(self.channel, msg) + removeThread(self.bot, self.api_url) self.stop.set() continue @@ -173,18 +177,18 @@ class WatcherThread(threading.Thread): thread = res.json() if thread["posts"][0].get("closed"): - msg = "[\x0304Watcher\x03] Thread closed: " \ - + f"\x0307{get_thread_url(self.api_url)}" - self._bot.msg(msg) - removeThread(self.bot, api_url) + msg = "[\x0304Watcher\x03] Thread closed: " + msg += f"\x0307{get_thread_url(self.api_url)}" + self.bot.msg(self.channel, msg) + removeThread(self.bot, self.api_url) self.stop.set() continue new_last_post = get_last_post(thread, self.name) if new_last_post > self.last_post: self.last_post = new_last_post - msg = "[\x0304Watcher\x03] New post from \x0308" \ - + f"{self.name}\x03: \x0307{get_thread_url(self.api_url)}" - + f"#{self.last_post}" - self._bot.msg(msg) - + print(self.last_post) + msg = "[\x0304Watcher\x03] New post from \x0308" + msg += f"{self.name}\x03: \x0307{get_thread_url(self.api_url)}" + msg += f"#{self.last_post}" + self.bot.msg(self.channel, msg)