Dynamic modification (core)

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(...))
...
 1"""
 2The example shows how to dynamically add objects to the framework
 3after it had already been run.
 4"""
 5from datetime import timedelta
 6import daf
 7
 8
 9async def user_task():
10    # 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
11    account = daf.ACCOUNT(token="ASDASJDAKDK", is_user=False)
12    guild = daf.GUILD(snowflake=123456)
13    data_to_shill = daf.TextMessageData(
14        "Hello World",
15        daf.discord.Embed(title="Example Embed", color=daf.discord.Color.blue(), description="This is a test embed")
16    )
17    text_msg = daf.TextMESSAGE(
18        data=data_to_shill,
19        channels=[12345, 6789],
20        period=daf.FixedDurationPeriod(timedelta(minutes=1))
21    )
22
23    # Dynamically add account
24    await daf.add_object(account)
25    
26    # Dynamically add guild
27    await account.add_server(guild) 
28    # await daf.add_object(guild, account)
29
30    # Dynamically add message
31    await guild.add_message(text_msg)
32    # await daf.add_object(text_msg, guild)
33
34
35############################################################################################
36if __name__ == "__main__":
37    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().

 1"""
 2The following example uses a predefined list of messages to shill.
 3When the user task is run, it removes the message from the shill list dynamically.
 4"""
 5import daf
 6
 7accounts = [
 8    account := daf.ACCOUNT(
 9        token="SDSADSDA87sd87",
10        is_user=False,
11        servers=[
12            daf.GUILD(snowflake=213323123, messages=[]) # No messages as not needed for this demonstration
13        ]
14    )
15]
16
17
18async def user_task():
19    guild = account.servers[0]
20    await daf.remove_object(guild)
21
22
23############################################################################################
24if __name__ == "__main__":
25    daf.run(user_callback=user_task, accounts=accounts)  
26    

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:

 1"""
 2The following example shows how to update the object to run on
 3new parameters.
 4It is generally not recommended to use this if there is a better way,
 5for example if you want to change the message, it's better to just create
 6a new message object, however the only way to update SQL manager for logging is via the
 7update method.
 8"""
 9from datetime import timedelta
10import asyncio
11import daf
12
13
14message = daf.TextMESSAGE(
15    data=daf.TextMessageData("Hello"),
16    channels=[123455, 1425215],
17    period=daf.FixedDurationPeriod(timedelta(seconds=5))
18)
19
20accounts = [
21    daf.ACCOUNT(
22        token="SKJDSKAJDKSJDKLAJSKJKJKGSAKGJLKSJG",
23        is_user=False,
24        servers=[
25            daf.GUILD(1234567, [message])
26        ]
27    )
28]
29
30
31async def user_task():
32    # Will send "Hello" every 5 seconds
33    await asyncio.sleep(10)
34    await message.update(
35        period=daf.FixedDurationPeriod(timedelta(seconds=20)),
36    )
37    # Will send "Hello" every 20 seconds
38
39if __name__ == "__main__":
40    daf.run(user_callback=user_task, accounts=accounts)  

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