Add cli flag for specifying config file

- Add cli flag for specifying config file
  (This allows running multiple running instances)
- Fix channel joins by waiting for server reply (thanks benharri)
- Remove toot commands from help message (unannounced feature)
- Update readme
trunk 0.1
mio 2020-10-25 22:04:16 +00:00
parent 16be6d9dda
commit 8d262f2fb1
6 changed files with 55 additions and 25 deletions

4
.gitignore vendored
View File

@ -4,5 +4,5 @@ __pycache__/
*.swp *.swp
nohup.out nohup.out
*/config.yml *.config.yml
ramenkan/debug.log *.log

View File

@ -5,16 +5,16 @@ A very basic Python IRC bot script.
## Example: ramenkan ## Example: ramenkan
- Install dependencies: Mastodon.py pyyaml - Install dependencies: `pip install Mastodon.py pyyaml`
- Copy the `ramenkan/config.sample.yml` as `ramenkan/comfig.yml` and change - Copy the `ramenkan/config.sample.yml` as `ramenkan/default.config.yml` and
settings. change the settings as applicable.
- Run: - Run:
``` ```
chmod +x ramenkan.py chmod +x ramenkan.py
nohup python3 ramenkan.py & nohup python3 ramenkan.py >/dev/null 2>&1 &
``` ```

29
itte.py
View File

@ -1,3 +1,4 @@
import argparse
import socket import socket
import yaml import yaml
from time import sleep from time import sleep
@ -9,25 +10,40 @@ class Util:
"""Utility functions.""" """Utility functions."""
def yml(self, yml_file): def yml(self, yml_file):
"Open a YAML file and return a dictionary of values." """Open a YAML file and return a dictionary of values."""
try:
fh = open(yml_file, "r") fh = open(yml_file, "r")
data = yaml.safe_load(fh) data = yaml.safe_load(fh)
fh.close() fh.close()
except TypeError:
exit("[debug][err] Cannot load YML file. Please check it exists.")
return data return data
def rand(self, lst): def rand(self, lst):
"""Return a random item from a given list.""" """Return a random item from a given list."""
return lst[randint(0, len(lst)-1)] return lst[randint(0, len(lst)-1)]
def cli_flags(self):
"""Parse command line flags."""
self.argp = argparse.ArgumentParser()
self.argp.add_argument("-c", "--config", help="Config file")
return self.argp.parse_args()
class IRC: class IRC:
"""Methods for basic IRC communication.""" """Methods for basic IRC communication."""
def config(self, conf): def config(self, def_conf):
"""Load runtime settings from a YAML config file, and returns a """Load runtime settings from a YAML config file, and returns a
dictionary of config values.""" dictionary of config values. Looks for the file in a runtime path or in
the default location."""
self.util = Util() self.util = Util()
cfg = self.util.yml(conf) # Check for runtime config locatiion
flags = self.util.cli_flags()
if flags.config != "":
cfg = self.util.yml(flags.config)
else:
cfg = self.util.yml(def_conf)
self.server = (cfg["server"]["host"], cfg["server"]["port"]) self.server = (cfg["server"]["host"], cfg["server"]["port"])
self.channels = cfg["channels"] self.channels = cfg["channels"]
self.bot_nick = cfg["bot_nick"] self.bot_nick = cfg["bot_nick"]
@ -41,6 +57,11 @@ class IRC:
"""A routine that connects to a server, joins channels, and attaches """A routine that connects to a server, joins channels, and attaches
the request listener hook to a loop.""" the request listener hook to a loop."""
self.connect(self.server, self.bot_nick) self.connect(self.server, self.bot_nick)
# Wait for server to reply before joining channels
svr_greet = self.receive()
while ("001 " + self.bot_nick) not in svr_greet:
sleep(1)
svr_greet = self.receive()
self.join_channels(self.channels) self.join_channels(self.channels)
while 1: while 1:
data = self.receive() data = self.receive()

View File

@ -16,10 +16,11 @@ class Ramen:
self.links = self.util.yml("ramenkan/links.yml") self.links = self.util.yml("ramenkan/links.yml")
self.photos = self.util.yml("ramenkan/photos.yml") self.photos = self.util.yml("ramenkan/photos.yml")
self.dishes = self.util.yml("ramenkan/dishes.yml") self.dishes = self.util.yml("ramenkan/dishes.yml")
# Init irc object # Init irc object and load config
self.irc = IRC() self.irc = IRC()
self.cfg = self.irc.config("ramenkan/config.yml") self.cfg = self.irc.config("ramenkan/default.config.yml")
# Init mastodon object # Init mastodon object
if "mastodon" in self.cfg:
self.masto = Mastodon( self.masto = Mastodon(
api_base_url=self.cfg["mastodon"]["base_url"], api_base_url=self.cfg["mastodon"]["base_url"],
access_token=self.cfg["mastodon"]["access_token"], access_token=self.cfg["mastodon"]["access_token"],
@ -43,6 +44,7 @@ class Ramen:
self.irc.listen(cxt, "rkveg", self.ramen_veggie) self.irc.listen(cxt, "rkveg", self.ramen_veggie)
self.irc.listen(cxt, "rklink", self.link) self.irc.listen(cxt, "rklink", self.link)
self.irc.listen(cxt, "rkselfie", self.selfie) self.irc.listen(cxt, "rkselfie", self.selfie)
if "mastodon" in self.cfg:
self.irc.listen(cxt, "rktoot", self.toot) self.irc.listen(cxt, "rktoot", self.toot)
self.irc.listen(cxt, "rkvtoot", self.toot_veggie) self.irc.listen(cxt, "rkvtoot", self.toot_veggie)

View File

@ -4,7 +4,7 @@ server:
port: 6667 port: 6667
channels: channels:
- "#rktest" - "#bots"
bot_nick: "ramenkan" bot_nick: "ramenkan"
@ -18,3 +18,10 @@ req_prefix: "!"
# Print messages to stdout # Print messages to stdout
debug: False debug: False
# Mastodon account
# mastodon:
# base_url: "https://example.com"
# access_token: ""
# client_secret: ""
# client_id: ""

View File

@ -1,7 +1,7 @@
rollcall: rollcall:
"一、二、三、らーめん缶! "一、二、三、らーめん缶!
Hello, I am a ramen vending machine. Please type a code for service: Hello, I am a ramen vending machine. Please type a code for service:
!help !ramen !vramen !rklink !rkselfie !rktoot !rkvtoot !help !ramen !vramen !rklink !rkselfie
- Support: +81 012-700-1MIO どうぞめしあがれ。" - Support: +81 012-700-1MIO どうぞめしあがれ。"
water: water: