2016-04-19 17:40:59 +00:00
|
|
|
"""FokaBot related functions"""
|
2016-05-20 16:53:09 +00:00
|
|
|
import re
|
2016-10-02 20:48:14 +00:00
|
|
|
|
|
|
|
from common import generalUtils
|
|
|
|
from common.constants import actions
|
|
|
|
from common.ripple import userUtils
|
|
|
|
from constants import fokabotCommands
|
|
|
|
from constants import serverPackets
|
|
|
|
from objects import glob
|
2016-05-20 16:53:09 +00:00
|
|
|
|
|
|
|
# Tillerino np regex, compiled only once to increase performance
|
|
|
|
npRegex = re.compile("^https?:\\/\\/osu\\.ppy\\.sh\\/b\\/(\\d*)")
|
2016-04-19 17:40:59 +00:00
|
|
|
|
|
|
|
def connect():
|
2016-11-17 18:13:06 +00:00
|
|
|
"""
|
|
|
|
Connect FokaBot to Bancho
|
|
|
|
|
|
|
|
:return:
|
|
|
|
"""
|
2018-02-14 16:44:37 +00:00
|
|
|
glob.BOT_NAME = userUtils.getUsername(999)
|
2016-04-19 17:40:59 +00:00
|
|
|
token = glob.tokens.addToken(999)
|
2016-09-02 15:16:22 +00:00
|
|
|
token.actionID = actions.IDLE
|
2016-10-01 19:19:03 +00:00
|
|
|
glob.streams.broadcast("main", serverPackets.userPanel(999))
|
|
|
|
glob.streams.broadcast("main", serverPackets.userStats(999))
|
2016-06-17 15:43:49 +00:00
|
|
|
|
2016-04-19 17:40:59 +00:00
|
|
|
def disconnect():
|
2016-11-17 18:13:06 +00:00
|
|
|
"""
|
|
|
|
Disconnect FokaBot from Bancho
|
|
|
|
|
|
|
|
:return:
|
|
|
|
"""
|
2016-04-19 17:40:59 +00:00
|
|
|
glob.tokens.deleteToken(glob.tokens.getTokenFromUserID(999))
|
|
|
|
|
|
|
|
def fokabotResponse(fro, chan, message):
|
|
|
|
"""
|
2016-11-17 18:13:06 +00:00
|
|
|
Check if a message has triggered FokaBot
|
2016-04-19 17:40:59 +00:00
|
|
|
|
2016-11-17 18:13:06 +00:00
|
|
|
:param fro: sender username
|
|
|
|
:param chan: channel name (or receiver username)
|
|
|
|
:param message: chat mesage
|
|
|
|
:return: FokaBot's response or False if no response
|
2016-04-19 17:40:59 +00:00
|
|
|
"""
|
|
|
|
for i in fokabotCommands.commands:
|
|
|
|
# Loop though all commands
|
2017-08-11 21:17:33 +00:00
|
|
|
if re.compile("^{}( (.+)?)?$".format(i["trigger"])).match(message.strip()):
|
2016-04-19 17:40:59 +00:00
|
|
|
# message has triggered a command
|
|
|
|
|
|
|
|
# Make sure the user has right permissions
|
2016-09-02 15:45:10 +00:00
|
|
|
if i["privileges"] is not None:
|
2016-05-28 18:26:26 +00:00
|
|
|
# Rank = x
|
2016-10-02 20:48:14 +00:00
|
|
|
if userUtils.getPrivileges(userUtils.getID(fro)) & i["privileges"] == 0:
|
2016-04-19 17:40:59 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
# Check argument number
|
|
|
|
message = message.split(" ")
|
|
|
|
if i["syntax"] != "" and len(message) <= len(i["syntax"].split(" ")):
|
|
|
|
return "Wrong syntax: {} {}".format(i["trigger"], i["syntax"])
|
|
|
|
|
|
|
|
# Return response or execute callback
|
2016-09-02 15:45:10 +00:00
|
|
|
if i["callback"] is None:
|
2016-04-19 17:40:59 +00:00
|
|
|
return i["response"]
|
|
|
|
else:
|
|
|
|
return i["callback"](fro, chan, message[1:])
|
|
|
|
|
|
|
|
# No commands triggered
|
2016-09-02 10:41:19 +00:00
|
|
|
return False
|