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 (str or None) – 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:dict

Examples

>>> import pprint
>>> pprint.pprint(Packet(key="key", value="value").json)
{'key': 'key', 'value': 'value'}

Message Packet

class cactusbot.packets.message.MessageComponent

MessagePacket component.

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.
class cactusbot.packets.message.MessagePacket(*message, user='', role=1, action=False, target=None)

Bases: cactusbot.packets.packet.Packet

Packet to store messages.

Parameters:
  • message (dict, tuple, str, or MessageComponent) –

    Message content components.

    dict should contain "type", "data", and "text" keys.

    tuple will be interpreted as (type, data, text). If not supplied, text will be equivalent to data.

    str will be interpreted as a component with type text.

  • 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 (str or None) – 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 in args and kwargs.

Return type:

MessagePacket

classmethod from_json(json)

Convert MessagePacket JSON into an object.

Parameters:json (dict) – The JSON dictionary to convert.
Returns:
Return type:MessagePacket

Examples

>>> 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:

MessagePacket

Examples

>>> MessagePacket.join(MessagePacket("a"), MessagePacket("b"), MessagePacket("c")).text
'abc'
>>> MessagePacket.join(MessagePacket("a"), MessagePacket("b"), MessagePacket("c"), separator='-').text
'a-b-c'
json

JSON representation of the packet.

Returns:Object attributes, in a JSON-compatible format.
Return type:dict

Examples

>>> 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:MessagePacket

Note

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 (int or None) –

    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:

list of :obj:`MessagePacket`s

Examples

>>> 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']
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:

MessagePacket

Note

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.'
text

Pure text representation of the packet.

Returns:Joined text of every component.
Return type:str

Examples

>>> MessagePacket("Hello, world! ", ("emoji", "😃")).text
'Hello, world! 😃'

Ban Packet

class cactusbot.packets.ban.BanPacket(user, duration=0)

Bases: cactusbot.packets.packet.Packet

Packet 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:dict

Examples

>>> import pprint
>>> pprint.pprint(BanPacket("Stanley", 60).json)
{'duration': 60, 'user': 'Stanley'}

Event Packet

class cactusbot.packets.event.EventPacket(event_type, user, success=True, streak=1)

Bases: cactusbot.packets.packet.Packet

Packet 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:dict

Examples

>>> import pprint
>>> pprint.pprint(EventPacket("follow", "Stanley").json)
{'event': 'follow', 'streak': 1, 'success': True, 'user': 'Stanley'}