Skip to content

Commit be7ec47

Browse files
authored
Merge pull request #46 from AuroraBTH/master
Version 1 of general actions
2 parents 9fa3ece + 8f42fcf commit be7ec47

5 files changed

Lines changed: 112 additions & 1 deletion

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ done/
1313
config.php
1414
build/
1515
.venv
16+
__pycache__/
17+
data/marvinMorning_date.txt

data/.gitignore

Whitespace-only changes.

main.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
import marvin
4747
import marvin_actions
48-
48+
import marvin_general_actions
4949

5050
#
5151
# General stuff about this program
@@ -193,8 +193,11 @@ def main():
193193
options = parseOptions()
194194
marvin.setConfig(options)
195195
marvin_actions.setConfig(options)
196+
marvin_general_actions.setConfig(options)
196197
actions = marvin_actions.getAllActions()
198+
general_actions = marvin_general_actions.getAllGeneralActions()
197199
marvin.registerActions(actions)
200+
marvin.registerGeneralActions(general_actions)
198201
marvin.connectToServer()
199202
marvin.mainLoop()
200203

marvin.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
# All actions to check for incoming messages
4444
ACTIONS = []
45+
GENERAL_ACTIONS = []
4546

4647
# Keep a log of the latest messages
4748
IRCLOG = None
@@ -71,6 +72,14 @@ def registerActions(actions):
7172
print(" - " + action.__name__)
7273
ACTIONS.extend(actions)
7374

75+
def registerGeneralActions(actions):
76+
"""
77+
Register general actions to use.
78+
"""
79+
print("Adding general actions:")
80+
for action in actions:
81+
print(" - " + action.__name__)
82+
GENERAL_ACTIONS.extend(actions)
7483

7584
def connectToServer():
7685
"""
@@ -283,3 +292,9 @@ def checkMarvinActions(words):
283292
if msg:
284293
sendPrivMsg(msg, words[2])
285294
break
295+
else:
296+
for action in GENERAL_ACTIONS:
297+
msg = action(set(row), row, raw)
298+
if msg:
299+
sendPrivMsg(msg, words[2])
300+
break

marvin_general_actions.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#! /usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
Make general actions for Marvin, one function for each action.
6+
"""
7+
import time
8+
import datetime
9+
import json
10+
import random
11+
12+
# Load all strings from file
13+
with open("marvin_strings.json", encoding="utf-8") as f:
14+
STRINGS = json.load(f)
15+
16+
# Configuration loaded
17+
CONFIG = None
18+
19+
def setConfig(config):
20+
"""
21+
Keep reference to the loaded configuration.
22+
"""
23+
global CONFIG
24+
CONFIG = config
25+
26+
27+
def getString(key, key1=None):
28+
"""
29+
Get a string from the string database.
30+
"""
31+
data = STRINGS[key]
32+
if isinstance(data, list):
33+
res = data[random.randint(0, len(data) - 1)]
34+
elif isinstance(data, dict):
35+
if key1 is None:
36+
res = data
37+
else:
38+
res = data[key1]
39+
if isinstance(res, list):
40+
res = res[random.randint(0, len(res) - 1)]
41+
elif isinstance(data, str):
42+
res = data
43+
44+
return res
45+
46+
47+
def getAllGeneralActions():
48+
"""
49+
Return all general actions as an array.
50+
"""
51+
return [
52+
marvinMorning
53+
]
54+
55+
56+
def marvinMorning(row, asList=None, asStr=None):
57+
"""
58+
Marvin says Good morning after someone else says it
59+
"""
60+
phrases = [
61+
"morgon",
62+
"godmorgon",
63+
"god morgon",
64+
"morrn",
65+
"morn"
66+
]
67+
68+
morning_phrases = [
69+
"Godmorgon! :-)",
70+
"Morgon allesammans",
71+
"Morgon gott folk",
72+
"Guten morgen",
73+
"Morgon"
74+
]
75+
76+
for phrase in phrases:
77+
if phrase in row:
78+
msg = random.choice(morning_phrases)
79+
80+
try:
81+
file = open('data/marvinMorning_date.txt', 'r+')
82+
file_data = file.read()
83+
84+
if not file_data == str(datetime.date.today()):
85+
f = open('data/marvinMorning_date.txt', 'w')
86+
f.write(str(datetime.date.today()))
87+
return msg
88+
except IOError:
89+
file = open('data/marvinMorning_date.txt', 'w')
90+
file.write(str(datetime.date.today()))
91+
return msg

0 commit comments

Comments
 (0)