Skip to content

Commit

Permalink
conversation: support uploading to Pastebin
Browse files Browse the repository at this point in the history
  • Loading branch information
Annika committed Jun 1, 2020
1 parent 2a9a5eb commit 87c1eb5
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
3 changes: 2 additions & 1 deletion config-example.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
"modules": [],
"commandCharacter": "~",
"superheroAPIKey": "",
"reversioRooms": []
"reversioRooms": [],
"pastebinAPIKey": ""
}
4 changes: 2 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
CONFIG_PATH = 'config.json'
CONFIG_VARS = ['username', 'password', 'websocketURL', 'loglevel', 'sysops', 'broadcastRank',
'addfactRank', 'hostgameRank', 'manageRank', 'roomRanksInOrder', 'rooms', 'separator', 'modules',
'commandCharacter', 'superheroAPIKey', 'reversioRooms']
'commandCharacter', 'superheroAPIKey', 'reversioRooms', 'pastebinAPIKey']

def loadConfig():
"""Loads configuration from CONFIG_PATH
Expand All @@ -29,5 +29,5 @@ def loadConfig():

username, password, websocketURL, loglevel, sysops, broadcastRank, addfactRank, \
hostgameRank, manageRank, roomRanksInOrder, rooms, separator, modules, \
commandCharacter, superheroAPIKey, reversioRooms = loadConfig()
commandCharacter, superheroAPIKey, reversioRooms, pastebinAPIKey = loadConfig()
# the order of these needs to be the same as in CONFIG_VARS
57 changes: 56 additions & 1 deletion conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import core
import random
import config
from pbwrap import Pastebin

############ conversation.py ############
## Gives conversation-starting prompts ##
Expand All @@ -15,7 +16,10 @@ def __init__(self):
self.commands = {
"fact": self.showSnippet, "topic": self.showSnippet, "addfact": self.manageSnippet,
"addtopic": self.manageSnippet, "deletefact": self.manageSnippet, "removefact": self.manageSnippet,
"deletetopic": self.manageSnippet, "removetopic": self.manageSnippet
"deletetopic": self.manageSnippet, "removetopic": self.manageSnippet, "countfacts": self.countSnippets,
"factcount": self.countSnippets, "counttopics": self.countSnippets, "topiccount": self.countSnippets,
"factlist": self.exportSnippets, "listfacts": self.exportSnippets, "topiclist": self.exportSnippets,
"listtopics": self.exportSnippets
}
self.factList = data.get("factList")
self.topicList = data.get("topicList")
Expand All @@ -41,6 +45,11 @@ def showSnippet(self, message):
message.respond(random.choice(snippetList[roomid]))

def manageSnippet(self, message):
"""Removes or adds a fact or topic
Arguments:
message {Message} -- the Message object that invoked the command
"""
if message.room and len(message.arguments) > 1:
room = message.room
snippet = ",".join(message.arguments[1:]).strip()
Expand Down Expand Up @@ -77,7 +86,53 @@ def manageSnippet(self, message):
self.topicList = snippetList
data.store("topicList", self.topicList)

def countSnippets(self, message):
"""Counts the number of snippets
Arguments:
message {Message} -- the Message object that invoked the command
"""
isFact = "fact" in message.arguments[0]
snippetList = self.factList if isFact else self.topicList
if message.room:
room = message.room
elif len(message.arguments) > 1:
room = message.connection.getRoomByName(message.arguments[1])
else:
return message.respond("You must specify a room.")

num = 0
if snippetList and room.id in snippetList.keys(): num = len(snippetList[room.id])
return message.respond(
"There " + ("is " if num == 1 else "are ") + str(num) +
(" fact" if isFact else " topic") + ("" if num == 1 else "s") +
" for the room " + room.id + "."
)

def exportSnippets(self, message):
"""Exports the snippets to Pastebin
Arguments:
message {Message} -- the Message object that invoked the command
"""
isFact = "fact" in message.arguments[0]
snippetList = self.factList if isFact else self.topicList
if message.room:
room = message.room
elif len(message.arguments) > 1:
room = message.connection.getRoomByName(message.arguments[1])
else:
return message.respond("You must specify a room.")
if not message.sender.can("addfact", room): return message.respond("Permission denied.")
if room.id not in snippetList.keys() or len(snippetList[room.id]) == 0:
return message.respond("There are no " + ("facts" if isFact else "topics") + " for the room " + room.id + ".")
pasteData = "\n".join(snippetList[room.id])
return message.respond(str(Pastebin(config.pastebinAPIKey).create_paste(
pasteData, # the data
1, # unlisted paste
"{type} for room {room}".format(room = room.id, type = "Facts" if isFact else "Topics") # title
)))

def __str__(self):
"""String representation of the Module
Expand Down
2 changes: 1 addition & 1 deletion core.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ def onMessage(self, rawMessage):
if message.challstr:
self.login(message.challstr)
elif message.type in ['chat', 'pm'] and message.body[0] == config.commandCharacter:
potentialCommand = message.body.split(' ')[0].strip(config.commandCharacter)
potentialCommand = message.body.split(' ')[0].strip(config.commandCharacter).lower()
if potentialCommand in self.commands:
self.commands[potentialCommand](message) # Invoke the command

Expand Down

0 comments on commit 87c1eb5

Please sign in to comment.