errbot icon indicating copy to clipboard operation
errbot copied to clipboard

Persistence of objects in plugins

Open raspbeguy opened this issue 5 years ago • 0 comments

I am...

  • [ ] Reporting a bug
  • [ ] Suggesting a new feature
  • [ ] Requesting help with running my bot
  • [x] Requesting help writing plugins
  • [ ] Here about something else

I am running...

  • Errbot version: Errbot fork https://github.com/bisco2/errbot/tree/1380-slixmpp that supports slixmpp
  • OS version: Debian 10
  • Python version: 3.7.3
  • Using a virtual environment: yes

Issue description

I wrote a soundbox plugin that enables you to play sounds from a given set. Find the full code here https://gist.github.com/raspbeguy/b65e26430af9f6a5a308f7cc1a1556c2

A sound is a small object:

class Sound():
    def __init__(self, filename, aliases):
        self.filename = filename
        self.aliases = aliases
        self.counter = 0

    def play(self, sounds_path):
        playsound("{}/{}".format(sounds_path,self.filename))
        self.counter += 1

    def addAliases(self, aliases):
        self.aliases = list(set(self.aliases + aliases))

    def getAliases(self):
        return self.aliases

    def getFilename(self):
        return self.filename

    def __str__(self):
        return "{}\t{}".format(self.filename, ", ".join(self.aliases))

    def __eq__(self, other):
        if not isinstance(other, Sound):
            return False
        return self.filename == other.getFilename

In the plugin class, I'm trying to use a persistent attribute self['SOUNDS'] to store the list of the sounds.

Here is the function to add a sound:

@botcmd(split_args_with=None)
def soundbox_add(self, msg, args):
    """Add a sound"""
    sound = Sound(args[0],args[1:])
    with self.mutable('SOUNDS') as sounds_list:
        sounds_list.append(sound)
    return "Sound was successfully added."

I also have a function to list the sounds:

@botcmd
def soundbox_list(self, msg, args):
    """List sounds"""
    for sound in self['SOUNDS']:
        yield str(sound)
    if len(self['SOUNDS']) == 0:
        return "No sounds yet"

I can add sounds with no problem, I can list them. But when I restart errbot the sound is sometimes not persisted. I say "sometimes" because I didn't find yet a pattern that makes it persist and sometimes not.

Steps to reproduce

!soundbox list

It shows:

file.mp3   something, stuff

Then add a sound

!soundbox add somefile.mp3 alias1 alias2

You can then see that is was successful by typing:

 !soundbox list

It shows:

file.mp3   something, stuff
somefile.mp3 alias1, alias2

Then restart errbot:

systemctl restart errbot

Print the list again:

 !soundbox list

It shows:

file.mp3   something, stuff

The sound that we added is gone.

raspbeguy avatar Jul 08 '20 07:07 raspbeguy