Skip to content

Commit

Permalink
Add module template and debug core
Browse files Browse the repository at this point in the history
  • Loading branch information
Annika committed May 25, 2020
1 parent c27d19c commit a093d04
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
2 changes: 1 addition & 1 deletion config-example.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
"roomRanksInOrder": ["+", "%", "@", "*", "#"],
"rooms": [],
"separator": ",",
"modules": {},
"modules": [],
"commandCharacter": "~"
}
20 changes: 17 additions & 3 deletions core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import websocket
import requests
import json
import importlib

################## core.py #####################
## core functionality of Expecto Botronum ##
Expand Down Expand Up @@ -66,7 +67,7 @@ def updateAuth(self, authDict):
authDict {dictionary} -- dictionary of the changes to the auth list
"""
for key in authDict.keys():
if self.auth[key]:
if key in self.auth:
self.auth[key] += authDict[key]
else:
self.auth[key] = authDict[key]
Expand Down Expand Up @@ -234,6 +235,20 @@ def __init__(self, raw, connection):
log("DEBUG: Message() of unknown type {type}: {raw}".format(type = self.type, raw = raw))
if self.body:
self.arguments = self.body.split(config.separator)

def respond(self, response):
"""Responds to the message, in a room or in PMs
If the user cannot broadcast and the command wasn't in PMs or it's not a message that can be responded to, does nothing
Arguments:
response {string} -- the response to be sent
"""
log("DEBUG: responding " + response)
if self.room and self.sender.can("broadcast", self.room):
self.room.say(response)
elif self.sender and not self.room:
self.sender.PM(response)

######################
## Connection Class ##
Expand All @@ -253,7 +268,7 @@ def __init__(self):
self.roomList = []
self.commands = {}
for module in config.modules:
self.commands.update(module.commands)
self.commands.update(importlib.import_module(module).Module().commands)
# Note: if multiple modules have the same command then the later module will overwrite the earlier.
log("I: Connection(): Loaded the following commands: " + ", ".join(self.commands.keys()))

Expand Down Expand Up @@ -362,7 +377,6 @@ def whisper(self, userid, message):
"""
self.websocket.send("|/pm {user}, {message}".format(user = userid, message = message))


if __name__ == "__main__":
connection = Connection()
log("I: core.py: opening websocket...")
Expand Down
19 changes: 19 additions & 0 deletions module_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
############## module_template.py #############
## serves as a template for the Module class ##
## to base other module files off of ##
## by Annika ##
###############################################

class Module:
"""Represents a module, which may contain several commands
"""
def __init__(self):
self.commands = {"command": self.exampleCommand, "alias": self.exampleCommand, "ping": self.exampleCommand}

def exampleCommand(self, message):
"""Example command: replies "Pong!"
Arguments:
message {Message} -- the Message object that invoked the command
"""
message.respond("Pong!")
4 changes: 3 additions & 1 deletion v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ This document is also hosted in [Google Docs](https://docs.google.com/document/d
- The message type. One of: `‘init’`, `‘title’`, `‘users’`, `‘html’`, `‘uhtml’`, `‘uhtmlchange’`, **`‘join’`**, `‘leave’`, `‘namechange`, **`‘chat’`**, `‘battle’`, **`‘popup’`**, **`‘pm’`**, ‘`usercount’`, `‘nametaken’`, **`‘challstr’`**, `‘updateuser’`, `‘updatesearch’`, `‘updatechallenges’`, `‘queryresponse’`, or `‘tournaments’`. (This is a complete representation of all PS! protocol messages - only the **bold** types are required for Expecto Botronum’s function.)
- `.challstr`
- if the message type is `‘challstr’`, this is the challstr.
- `.respond(message)`
- responds to the message: in the room if the User can broadcast or in PMs if the command was in PMs
- `Room`
- `.say(message)`
- sends the message to the room
Expand Down Expand Up @@ -108,7 +110,7 @@ This document is also hosted in [Google Docs](https://docs.google.com/document/d
- `.commands`
- a dictionary mapping commands to functions.
- Example for `superhero.py`: `{‘superhero’: self.superhero, ‘sh’: self.superhero}`
- Private methods for executing the commands
- Methods for executing the commands
- `Command`
- I don't yet know if I want commands to be proper objects from a `Command` class or just methods of `Module`.
- If I did write a `Command` object, I'd want to have documentation, required perms, execution method.
Expand Down

0 comments on commit a093d04

Please sign in to comment.