Dynamic modification#

This document describes how the framework can be modified dynamically.

Modifying the shilling list#

See Dynamic mod. for more information about the functions mentioned below.

While the shilling list can be defined statically (pre-defined) by creating a list and using the servers parameter in the ACCOUNT instances (see Quickstart), the framework also allows the objects to be added or removed dynamically from the user’s program after the framework has already been started and initialized.

Dynamically adding objects#

Objects can be dynamically added using the daf.core.add_object() coroutine function. The function can be used to add the following object types:

Messages

daf.message.TextMESSAGE

daf.message.VoiceMESSAGE

daf.message.DirectMESSAGE

Note

Messages can also be added thru the daf.guild.GUILD.add_message() / daf.guild.USER.add_message() method.

Caution

The guild must already be added to the framework, otherwise this method will fail.

...
my_guild = daf.GUILD(guild.id, logging=True)
await daf.add_object(my_guild, account)
await my_guild.add_message(daf.TextMESSAGE(...))
...
"""
The example shows how to dynamically add objects to the framework
after it had already been run.
"""
from datetime import timedelta
import daf


async def user_task():
    # Returns the client to send commands to discord, for more info about client see https://docs.pycord.dev/en/master/api.html?highlight=discord%20client#discord.Client
    account = daf.ACCOUNT(token="ASDASJDAKDK", is_user=False)
    
    guild = daf.GUILD(snowflake=123456)
    
    data_to_shill = ( "Hello World", daf.discord.Embed(title="Example Embed", color=daf.discord.Color.blue(), description="This is a test embed") )
    text_msg = daf.TextMESSAGE(None, timedelta(seconds=5), data_to_shill, [12345, 6789], "send", timedelta(seconds=0))

    # Dynamically add account
    await daf.add_object(account)
    
    # Dynamically add guild
    await account.add_server(guild) 
    # await daf.add_object(guild, account)

    # Dynamically add message
    await guild.add_message(text_msg)
    # await daf.add_object(text_msg, guild)


############################################################################################
if __name__ == "__main__":
    daf.run(user_callback=user_task)  

Dynamically removing objects#

As the framework supports dynamically adding new objects to the shilling list, it also supports dynamically removing those objects. Objects can be removed with the daf.core.remove_object().

"""
The following example uses a predefined list of messages to shill.
When the user task is run, it removes the message from the shill list dynamically.
"""
import daf

accounts = [
    account := daf.ACCOUNT(
        token="SDSADSDA87sd87",
        is_user=False,
        servers=[
            daf.GUILD(snowflake=213323123, messages=[]) # No messages as not needed for this demonstration
        ]
    )
]


async def user_task():
    guild = account.servers[0]
    await daf.remove_object(guild)


############################################################################################
if __name__ == "__main__":
    daf.run(user_callback=user_task, accounts=accounts)  
    

Modifying objects#

Some objects in the framework can be dynamically updated thru the .update() method. The principle is the same for all objects that support this and what this method does is it updates the original parameters that can be passed during object creation.

Warning

This completely resets the state of the object you are updating, meaning that if you do call the .update() method, the object will act like it was recreated.

For example if I wanted to change the shilling period of a daf.message.TextMESSAGE, I would call the daf.message.TextMESSAGE.update() method in the following way:

... # Other code
# Fixed sending period of 5 seconds
my_message = daf.message.TextMESSAGE(
                                        start_period=None,
                                        end_period=timedelta(seconds=5),
                                        ... # Other parameters
                                    )


await daf.add_object(my_message, some_GUILD_object)

# Randomized sending period between 3 and 5 seconds
await my_message.update(start_period=timedelta(seconds=3))
... # Other code

For a full list of objects that support .update search “.update” in the search bar or click on the image below.

../_images/search_update_method.png
"""
The following example shows how to update the object to run on
new parameters.
It is generally not recommended to use this if there is a better way,
for example if you want to change the message, it's better to just create
a new message object, however the only way to update SQL manager for logging is via the
update method.
"""
from datetime import timedelta
import asyncio
import daf


message = daf.TextMESSAGE(None, timedelta(seconds=5), "Hello", [123455, 1425215])
accounts = [
    daf.ACCOUNT( token="SKJDSKAJDKSJDKLAJSKJKJKGSAKGJLKSJG",
                 is_user=False,
                 servers=[daf.GUILD(1234567, [message])] )
]


async def user_task():
    # Will send "Hello" every 5 seconds
    await asyncio.sleep(10)
    await message.update(end_period=timedelta(seconds=20), data="World")
    # Will now send "World" every 20 seconds


if __name__ == "__main__":
    daf.run(user_callback=user_task, accounts=accounts)