Packets¶
Base Packet¶
-
class
cactusbot.packets.packet.Packet(packet_type=None, **kwargs)¶ Base packet.
May be used for packets which only require static attributes.
Parameters: - packet_type (
strorNone) – The name for the packet type. If not specified, the class name is used. - **kwargs – Packet attributes.
-
json¶ JSON representation of the packet.
Returns: Object attributes, in a JSON-compatible format. Return type: dictExamples
>>> import pprint >>> pprint.pprint(Packet(key="key", value="value").json) {'key': 'key', 'value': 'value'}
- packet_type (
Message Packet¶
-
class
cactusbot.packets.message.MessageComponent¶ MessagePacketcomponent.Valid Types:
Type Description Sample Data text Plaintext of any length. Hello, world. emoji Single emoji. 🌵 tag Single user tag or mention. Username url URL. https://google.com variable Key to be replaced with live values. %ARGS% Parameters: - type (
str) – Component type. - data (
str) – Component data. - text (
str) – Text representation of the component.
- type (
-
class
cactusbot.packets.message.MessagePacket(*message, user='', role=1, action=False, target=None)¶ Bases:
cactusbot.packets.packet.PacketPacket to store messages.
Parameters: - message (
dict,tuple,str, orMessageComponent) –Message content components.
dictshould contain"type","data", and"text"keys.tuplewill be interpreted as(type, data, text). If not supplied,textwill be equivalent todata.strwill be interpreted as a component withtypetext. - user (
str) – The sender of the MessagePacket. - role (
int) – The role ID of the sender. - action (
bool) – Whether or not the message was sent in action form. - target (
strorNone) – The single user target of the message.
-
copy(*args, **kwargs)¶ Return a copy of
self.Parameters: - *args – If any are provided, will entirely override
self.message. - **kwargs – Each will override class attributes provided in
__init__().
Returns: Copy of
self, with replaced attributes as specified inargsandkwargs.Return type: MessagePacket- *args – If any are provided, will entirely override
-
classmethod
from_json(json)¶ Convert
MessagePacketJSON into an object.Parameters: json ( dict) – The JSON dictionary to convert.Returns: Return type: MessagePacketExamples
>>> MessagePacket.from_json({ ... 'action': False, ... 'message': [{'type': 'text', ... 'data': 'Hello, world! ', ... 'text': 'Hello, world! '}, ... {'data': '😃', 'text': '😃', 'type': 'emoji'}], ... 'role': 1, ... 'target': None, ... 'user': '' ... }).text 'Hello, world! 😃'
-
classmethod
join(*packets, separator='')¶ Join multiple message packets together.
Parameters: - *packets (
MessagePacket) – The packets to join. - separator (str) – The string to place between every packet.
Returns: Packet containing joined contents.
Return type: MessagePacketExamples
>>> MessagePacket.join(MessagePacket("a"), MessagePacket("b"), MessagePacket("c")).text 'abc'
>>> MessagePacket.join(MessagePacket("a"), MessagePacket("b"), MessagePacket("c"), separator='-').text 'a-b-c'
- *packets (
-
json¶ JSON representation of the packet.
Returns: Object attributes, in a JSON-compatible format. Return type: dictExamples
>>> import pprint >>> pprint.pprint(MessagePacket("Hello, world! ", ("emoji", "😃")).json) {'action': False, 'message': [{'data': 'Hello, world! ', 'text': 'Hello, world! ', 'type': 'text'}, {'data': '😃', 'text': '😃', 'type': 'emoji'}], 'role': 1, 'target': None, 'user': ''}
-
replace(**values)¶ Replace text in packet.
Parameters: values ( dict) – The text to replace.Returns: self, with replaced text.Return type: MessagePacketNote
Modifies the object itself. Does not return a copy.
Examples
>>> packet = MessagePacket("Hello, world!") >>> packet.replace(world="universe").text 'Hello, universe!'
>>> packet = MessagePacket("Hello, world!") >>> packet.replace(**{ ... "Hello": "Goodbye", ... "world": "Python 2" ... }).text 'Goodbye, Python 2!'
-
split(separator=' ', maximum=None)¶ Split into multiple MessagePackets, based on a separator.
Parameters: - separator (
str, default ‘ ‘) – The characters to split the string with. - maximum (
intorNone) –The maximum number of splits to perform.
If less than the total number of potential splits, will result in a list of length maximum + 1. Otherwise, will perform all splits.
If
None, will perform all splits.
Returns: Return type: listof :obj:`MessagePacket`sExamples
>>> packet = MessagePacket("0 1 2 3 4 5 6 7") >>> [component.text for component in packet.split()] ['0', '1', '2', '3', '4', '5', '6', '7']
>>> packet = MessagePacket("0 1 2 3 4 5 6 7") >>> [component.text for component in packet.split("2")] ['0 1 ', ' 3 4 5 6 7']
>>> packet = MessagePacket("0 1 2 3 4 5 6 7") >>> [component.text for component in packet.split(maximum=3)] ['0', '1', '2', '3 4 5 6 7']
- separator (
-
sub(pattern, repl)¶ Perform regex substitution on packet.
Parameters: - pattern (
str) – Regular expression to match. - repl –
The replacement for the pattern.
Accepts the same argument types as
re.sub().
Returns: self, with replaced patterns.Return type: MessagePacketNote
Modifies the object itself. Does not return a copy.
Examples
>>> packet = MessagePacket("I would like 3 ", ("emoji", "😃"), "s.") >>> packet.sub(r"\d+", "<number>").text 'I would like <number> 😃s.'
- pattern (
-
text¶ Pure text representation of the packet.
Returns: Joined textof every component.Return type: strExamples
>>> MessagePacket("Hello, world! ", ("emoji", "😃")).text 'Hello, world! 😃'
- message (
Ban Packet¶
-
class
cactusbot.packets.ban.BanPacket(user, duration=0)¶ Bases:
cactusbot.packets.packet.PacketPacket to store bans.
Parameters: - user (
str) – User identifier. - duration (
int, optional) –The length of time for which the ban lasts, in seconds.
If set to
0, the ban lasts for an unlimited amount of time.
-
json¶ JSON representation of the packet.
Returns: Object attributes, in a JSON-compatible format. Return type: dictExamples
>>> import pprint >>> pprint.pprint(BanPacket("Stanley", 60).json) {'duration': 60, 'user': 'Stanley'}
- user (
Event Packet¶
-
class
cactusbot.packets.event.EventPacket(event_type, user, success=True, streak=1)¶ Bases:
cactusbot.packets.packet.PacketPacket to store events.
Parameters: - event_type (
str) – Event type. - user (
str) – User identifier. - success (
bool) – Whether or not the event was positive or successful.
-
json¶ JSON representation of the packet.
Returns: Object attributes, in a JSON-compatible format. Return type: dictExamples
>>> import pprint >>> pprint.pprint(EventPacket("follow", "Stanley").json) {'event': 'follow', 'streak': 1, 'success': True, 'user': 'Stanley'}
- event_type (