Added persistent join/part

This commit is contained in:
mrhanky 2017-08-22 18:02:33 +02:00
parent f33a17038a
commit e9f10db2c5
No known key found for this signature in database
GPG Key ID: 67D772C481CB41B8
2 changed files with 32 additions and 1 deletions

View File

@ -2,6 +2,7 @@
import os
import json
import logging
from collections import OrderedDict
import irc3
from dotenv import load_dotenv
@ -15,6 +16,19 @@ class Bot(irc3.IrcBot):
REPO_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
CONFIG_DIR = REPO_DIR if DEV else os.path.dirname(REPO_DIR)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.cfg_file = None
self.env_file = None
def load_config(self):
with open(self.cfg_file, 'r') as fp:
return json.load(fp, object_pairs_hook=OrderedDict)
def save_config(self, data):
with open(self.cfg_file, 'w') as fp:
json.dump(data, fp, indent=2)
@classmethod
def from_json(cls, cfg_file: str = 'config.json', env_file: str = '.env'):
cfg_file = os.path.join(cls.CONFIG_DIR, cfg_file)
@ -25,7 +39,10 @@ class Bot(irc3.IrcBot):
with open(cfg_file, 'r') as fp:
conf = json.load(fp)
return cls.from_config(conf)
bot = cls.from_config(conf)
bot.cfg_file = cfg_file
bot.env_file = env_file
return bot
@irc3.plugin

View File

@ -72,6 +72,13 @@ class Admin(Plugin):
self.bot.join(channel)
self.bot.notice(mask.nick, 'Joined channel {}'.format(channel))
conf = self.bot.load_config()
channels = conf.get('autojoins', [])
if channel not in channels:
channels.append(channel)
conf['autojoins'] = channels
self.bot.save_config(conf)
@command(permission='all_permissions')
def part(self, mask: IrcString, target: IrcString, args: Dict):
"""Let the bot part a given or the current channel
@ -83,6 +90,13 @@ class Admin(Plugin):
self.bot.join(channel)
self.bot.notice(mask.nick, 'Parted channel {}'.format(channel))
conf = self.bot.load_config()
channels = conf.get('autojoins', [])
if channel in channels:
channels.remove(channel)
conf['autojoins'] = channels
self.bot.save_config(conf)
@command(permission='all_permissions')
def cycle(self, mask: IrcString, target: IrcString, args: Dict):
"""Let the bot part and join a given or the current channel