-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfunction_template.py
More file actions
180 lines (139 loc) · 6.61 KB
/
function_template.py
File metadata and controls
180 lines (139 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import string
import datetime
import urllib
from bs4 import BeautifulSoup
from utils import *
import logging
log = logging.getLogger("function")
class function_template(object):
def __init__(self):
self.name = None
# Register the function types, COMMAND, KEY, NATURAL and STATUS
# Command functions parse text messages for the functions commands.
# - If no match is found the function is ignored.
# Natural & Status functions parse all text & status messages respectively.
# Key functions handle all messages sent by a user/channel if they have a key lock.
# - If the user/channel does not have key lock the function is ignored.
# All functions should return True if they do anything, e.g. send messages, update data files.
self.type = ["command"]
# Specify the functions priority from 100 to 1, one being the highest priority
self.priority = 50
# Specify the functions triggerable commands. (Not needed for natural functions.)
self.commands = []
# Specify if the function requires auth status, can also be handled manually if individual sub commands need to be restricted.
self.restricted = False
# Specify the functions function, this is the description shown in the help list.
self.function_string = "Function template."
# The functions help string, shown when running help on a specific function.
# Multiple lines can be separated with the newline (\n) character.
# The short trigger can be specified with {t} and will be dynamically replaced when the the help string is shown via the help function.
self.help_string = None
# Specify if the function blocks any other functions that come after itself and are triggerable with the same parameters.
# Not used with KEY type functions since they are always blocking.
self.blocking = True
# The functions run count, incremented every time the function is triggered.
self.run_count = 0
# Used when the function is of type KEY to check if the lock is currently active.
self.key_lock = []
# Used to hide the function from the help list.
self.hidden = False
# Disable a function for specific period of time.
self.disabled = None #{disabled_by:"", time:""}
def load(self, bot):
# log.debug(color.blue + "Function load: " + color.clear + self.name)
self._bot = bot
return self.name;
def unload(self, bot):
# log.debug(color.blue + "Function unload: " + color.clear + self.name)
return self.name;
def main(self, bot, msg_data, func_type):
function_name = string.split(self.name,".")[0]
irc.sendMSG("Function not setup, still using template: " + function_name, bot.master_channel)
return True
# Load and save function JSON data files
# - Files are stored in ./_functiondata/
# - The optional default argument specifies the data to create the file with if no file is found
# - If None is specified no file will be created (defaults to None)
def loadFunctionDataFile(self, filename, default = None):
function_name = string.split(self.name,".")[0]
directory = '_functiondata/' + str(function_name)
data = self._bot.openDataFile('_'+ filename, directory, default)
return data
def saveDataToDataFile(self, data, filename):
function_name = string.split(self.name,".")[0]
directory = '_functiondata/' + str(function_name)
self._bot.saveDataFile(data, '_'+ filename, directory)
# Load a file and split it into a list of lines.
# - Ignores empty lines and lines commented with a hash (#)
def loadMessagesFile(self, filename):
filename = './_data/' + filename
lines1 = open(filename).read().splitlines()
lines2 = [line for line in lines1 if not line.startswith('#')]
lines3 = [line for line in lines2 if line]
return lines3
# Load and parse a JSON data file
# - Returns the files data or None if no data/file is found
def loadDataFile(self, filename):
directory = '_data/'
data = self._bot.openDataFile(filename, directory)
return data
def hasKeyLockFor(self, entity):
if entity in self.key_lock:
return True
return False
def addToKeyLock(self, entity):
log.info(color.cyan + "Adding " + entity + " to key lock" + color.clear)
self.key_lock.append(entity)
def removeFromKeyLock(self, entity):
log.info(color.cyan + "Removing key lock for " + entity + color.clear)
self.key_lock.append(entity)
def colorizer(message):
message = message.replace("&00", color.irc_boldwhite)
message = message.replace("&01", color.irc_black)
message = message.replace("&02", color.irc_blue)
message = message.replace("&03", color.irc_green)
message = message.replace("&04", color.irc_boldred)
message = message.replace("&05", color.irc_red)
message = message.replace("&06", color.irc_violet)
message = message.replace("&07", color.irc_yellow)
message = message.replace("&08", color.irc_boldyellow)
message = message.replace("&09", color.irc_boldgreen)
message = message.replace("&10", color.irc_cyan)
message = message.replace("&11", color.irc_boldcyan)
message = message.replace("&12", color.irc_boldblue)
message = message.replace("&13", color.irc_boldviolet)
message = message.replace("&14", color.irc_boldblack)
message = message.replace("&15", color.irc_white)
message = message.replace("&b", color.irc_bold)
# message = message.replace("&i", color.irc_italic)
# message = message.replace("&u", color.irc_underline)
message = message.replace("&c", color.irc_clear)
return message
def parseTimeDelta(str_input):
value = str_input
if str_input.endswith(("s","m","h","d")):
value = str_input[:-1]
if not value.isalnum():
return False
value = int(value)
if str_input.endswith("s"):
return datetime.timedelta(seconds = value)
elif str_input.endswith("h"):
return datetime.timedelta(hours = value)
elif str_input.endswith("d"):
return datetime.timedelta(days = value)
else:
return datetime.timedelta(minutes = value)
return False
def pageFromList(page_list, page_index, page_size):
start = (page_index - 1) * page_size
end = start + page_size
if start >= len(page_list):
start = 0
end = page_size
elif end >= len(page_list):
end = len(page_list)
return page_list[start:end]