Compare commits

...

1 Commits

Author SHA1 Message Date
mio 44a72253c6 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
2020-10-25 22:27:33 +00:00
6 changed files with 55 additions and 25 deletions

4
.gitignore vendored
View File

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

View File

@ -5,16 +5,16 @@ A very basic Python IRC bot script.
## 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
settings.
- Copy the `ramenkan/config.sample.yml` as `ramenkan/default.comfig.yml` and
change the settings as applicable.
- Run:
```
chmod +x ramenkan.py
nohup python3 ramenkan.py &
nohup python3 ramenkan.py >/dev/null 2>&1 &
```

35
itte.py
View File

@ -1,3 +1,4 @@
import argparse
import socket
import yaml
from time import sleep
@ -9,25 +10,40 @@ class Util:
"""Utility functions."""
def yml(self, yml_file):
"Open a YAML file and return a dictionary of values."
fh = open(yml_file, "r")
data = yaml.safe_load(fh)
fh.close()
"""Open a YAML file and return a dictionary of values."""
try:
fh = open(yml_file, "r")
data = yaml.safe_load(fh)
fh.close()
except TypeError:
exit("[debug][err] Cannot load YML file. Please check it exists.")
return data
def rand(self, lst):
"""Return a random item from a given list."""
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:
"""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
dictionary of config values."""
dictionary of config values. Looks for the file in a runtime path or in
the default location."""
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.channels = cfg["channels"]
self.bot_nick = cfg["bot_nick"]
@ -41,6 +57,11 @@ class IRC:
"""A routine that connects to a server, joins channels, and attaches
the request listener hook to a loop."""
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)
while 1:
data = self.receive()

View File

@ -16,16 +16,17 @@ class Ramen:
self.links = self.util.yml("ramenkan/links.yml")
self.photos = self.util.yml("ramenkan/photos.yml")
self.dishes = self.util.yml("ramenkan/dishes.yml")
# Init irc object
# Init irc object and load config
self.irc = IRC()
self.cfg = self.irc.config("ramenkan/config.yml")
self.cfg = self.irc.config("ramenkan/default.config.yml")
# Init mastodon object
self.masto = Mastodon(
api_base_url=self.cfg["mastodon"]["base_url"],
access_token=self.cfg["mastodon"]["access_token"],
client_id=self.cfg["mastodon"]["client_id"],
client_secret=self.cfg["mastodon"]["client_secret"]
)
if "mastodon" in self.cfg:
self.masto = Mastodon(
api_base_url=self.cfg["mastodon"]["base_url"],
access_token=self.cfg["mastodon"]["access_token"],
client_id=self.cfg["mastodon"]["client_id"],
client_secret=self.cfg["mastodon"]["client_secret"]
)
# Init request listeners
self.irc.run(self.add_listeners)
@ -43,8 +44,9 @@ class Ramen:
self.irc.listen(cxt, "rkveg", self.ramen_veggie)
self.irc.listen(cxt, "rklink", self.link)
self.irc.listen(cxt, "rkselfie", self.selfie)
self.irc.listen(cxt, "rktoot", self.toot)
self.irc.listen(cxt, "rkvtoot", self.toot_veggie)
if "mastodon" in self.cfg:
self.irc.listen(cxt, "rktoot", self.toot)
self.irc.listen(cxt, "rkvtoot", self.toot_veggie)
def quit(self, cxt):
"""Disconnect from the server and quit."""

View File

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

View File

@ -1,7 +1,7 @@
rollcall:
"一、二、三、らーめん缶!
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 どうぞめしあがれ。"
water: