Skip to content

Commit

Permalink
Changes for Commands Update
Browse files Browse the repository at this point in the history
  • Loading branch information
olijeffers0n committed Nov 29, 2021
1 parent 5c9a65f commit 7e93d85
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 44 deletions.
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ It should also install all the dependencies, but if not you will have to install

## Usage:
```py
from rustplus import RustSocket
from rustplus import RustSocket, CommandOptions, Command

rust_socket = RustSocket("IPADDRESS", "PORT", 64BITSTEAMID, PLAYERTOKEN)
#Registering the Command Options in order to listen for commands
options = CommandOptions(prefix="!")

rust_socket = RustSocket("IPADDRESS", "PORT", 64BITSTEAMID, PLAYERTOKEN, command_options=options)
#See below for more information on the above ^^

#Connects to the server's websocket
Expand All @@ -49,10 +52,7 @@ team_info = await rust_socket.getTeamInfo()
team_chat = await rust_socket.getTeamChat()

#Sending a Team Chat message:
status = await rust_socket.sendTeamMessage("Yo! I sent this with Rust+.py")

#Get Camera Image:
camera_image = await rust_socket.getCameraFrame("CAMID",FRAMENO)
await rust_socket.sendTeamMessage("Yo! I sent this with Rust+.py")

#Get Map Image:
rust_map = await rust_socket.getMap(addIcons = True, addEvents = True, addVendingMachines= True, overrideImages = {})
Expand All @@ -76,6 +76,14 @@ tc_contents = await rust_socket.getTCStorageContents(ENTITYID, MERGESTACKS : boo
#Getting Current Map Events
events = await rust_socket.getCurrentEvents()

#Registering a command listener, which will listen for the command 'hi' with the prefix we defined earlier
@socket.command
async def hi(command : Command):
await rust_socket.sendTeamMessage(f"Hi {command.sender_name}, This is an automated reply from RustPlus.py!")

#Used to just stop a script from ending. Use this if you are using commands
await rust_socket.hang()

await rust_socket.closeConnection()
```

Expand Down
2 changes: 1 addition & 1 deletion rustplus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

from .api import RustSocket
from .exceptions import ClientError, ImageError, ServerNotResponsiveError, ClientNotConnectedError, PrefixNotDefinedError, EventsNotEnabledError
from .exceptions import ClientError, ImageError, ServerNotResponsiveError, ClientNotConnectedError, PrefixNotDefinedError, CommandsNotEnabledError
from .commands import CommandOptions, Command

__name__ = "rustplus"
Expand Down
39 changes: 7 additions & 32 deletions rustplus/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from .rustplus_pb2 import *
from .structures import RustTime, RustInfo, RustMap, RustMarker, RustChatMessage, RustSuccess, RustTeamInfo, RustTeamMember, RustTeamNote, RustEntityInfo, RustContents, RustItem
from ..utils import MonumentNameToImage, TimeParser, CoordUtil, ErrorChecker, IdToName, MapMarkerConverter
from ..exceptions import ImageError, ServerNotResponsiveError, ClientNotConnectedError, EventsNotEnabledError
from ..exceptions import ImageError, ServerNotResponsiveError, ClientNotConnectedError, CommandsNotEnabledError
from ..commands import CommandOptions, RustCommandHandler

class RustSocket:
Expand Down Expand Up @@ -67,6 +67,8 @@ async def __listener(self) -> None:
if app_message.broadcast.teamMessage.message.message == "":
if app_message.response.seq not in self.ignored_responses:
self.responses[app_message.response.seq] = app_message
else:
self.ignored_responses.remove(app_message.response.seq)
else:
message = RustChatMessage(app_message.broadcast.teamMessage.message)

Expand Down Expand Up @@ -230,19 +232,6 @@ async def __sendTeamChatMessage(self, message) -> RustSuccess:

return RustSuccess(0,"Success")

async def __getCameraFrame(self, id, frame):

cameraPacket = AppCameraFrameRequest()
cameraPacket.identifier = id
cameraPacket.frame = frame

request = self.__initProto()
request.getCameraFrame.CopyFrom(cameraPacket)

app_message = await self.__sendAndRecieve(request)

return app_message

async def __getTeamInfo(self):

request = self.__initProto()
Expand Down Expand Up @@ -393,20 +382,6 @@ async def sendTeamMessage(self, message : str) -> RustSuccess:

return await self.__sendTeamChatMessage(message)

async def getCameraFrame(self, id : str, frame : int) -> Image:
"""
Returns a low quality jpeg image from a camera in-game
"""

returnData = await self.__getCameraFrame(id,frame)

try:
image = Image.open(BytesIO(returnData.response.cameraFrame.jpgImage))
except:
raise ImageError("Invalid Bytes Recieved")

return image

async def getTeamInfo(self) -> RustTeamInfo:
"""
Returns an AppTeamInfo object of the players in your team, as well as a lot of data about them
Expand Down Expand Up @@ -464,16 +439,16 @@ async def getCurrentEvents(self) -> List[RustMarker]:

return await self.__getCurrentEvents()

def event(self, coro) -> None:
"""A Decorator that registers an event listener"""
def command(self, coro) -> None:
"""A Decorator that registers an commands listener"""

if self.command_handler is None:
raise EventsNotEnabledError("Events have not been enabled in the constructor")
raise CommandsNotEnabledError("Commands have not been enabled in the constructor")

self.command_handler.register_command(coro.__name__, coro)

async def hang(self) -> None:
"""This Will permanently put your script into a state of 'hanging'. Only do this in scripts using events"""
"""This Will permanently put your script into a state of 'hanging'. Only do this in scripts using commands"""

while True:
await asyncio.sleep(1)
5 changes: 3 additions & 2 deletions rustplus/commands/command.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
class Command:

def __init__(self, sender_name, sender_steamid, message, time ) -> None:
def __init__(self, sender_name, sender_steamid, time, command, args) -> None:

self.sender_name = sender_name
self.sender_steamid = sender_steamid
self.message = message
self.time = time
self.command = command
self.args = args
2 changes: 1 addition & 1 deletion rustplus/commands/command_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ async def run_command(self, message : RustChatMessage) -> None:

coro = getattr(self, command)

await coro(Command(message.name, message.steamId, message.message, message.time))
await coro(Command(message.name, message.steamId, message.time, command, message.message.split(" ")[1:]))
2 changes: 1 addition & 1 deletion rustplus/exceptions/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .exceptions import ClientError, ImageError, ServerNotResponsiveError, ClientNotConnectedError, PrefixNotDefinedError, EventsNotEnabledError
from .exceptions import ClientError, ImageError, ServerNotResponsiveError, ClientNotConnectedError, PrefixNotDefinedError, CommandsNotEnabledError
2 changes: 1 addition & 1 deletion rustplus/exceptions/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ class PrefixNotDefinedError(Error):
"""Raised when a prefix is not given"""
pass

class EventsNotEnabledError(Error):
class CommandsNotEnabledError(Error):
"""Raised when events are not enabled"""
pass

0 comments on commit 7e93d85

Please sign in to comment.