Skip to content

Commit

Permalink
Write waifu suggestions directly to a file
Browse files Browse the repository at this point in the history
I decided that the complexity of caching suggestions to `bot.db` isn't
worth it. Savings on SSD wear are likely to be negligible as this
feature doesn't see heavy use, and it's actually *more* likely to lose
data that's only cached in `bot.memory` if the bot crashes or otherwise
fails to shut down gracefully. Plus, plugin values in the database will
be truncated to a fairly short length on certain DB backends (see issue
tracking this upstream, sopel-irc/sopel#2291).

Tested live adding a few waifus, viewed the file (making sure newlines
were handled correctly), and then used `.clearwaifus` to delete it.
  • Loading branch information
dgw committed Feb 24, 2024
1 parent c2b00f4 commit 09388d0
Showing 1 changed file with 31 additions and 49 deletions.
80 changes: 31 additions & 49 deletions sopel_waifu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@

LOGGER = tools.get_logger('waifu')
WAIFU_LIST_KEY = 'waifu-list'
WAIFU_SUGGESTIONS_KEY = 'waifu-suggestions'
WAIFU_SUGGESTIONS_FILE = 'waifu-suggestions.txt'


def get_waifu_suggestions_file(bot):
return os.path.join(bot.config.core.homedir, WAIFU_SUGGESTIONS_FILE)


class WaifuSection(config.types.StaticSection):
Expand Down Expand Up @@ -72,19 +76,8 @@ def setup(bot):
if bot.config.waifu.unique_waifus:
bot.memory[WAIFU_LIST_KEY] = list(set(bot.memory[WAIFU_LIST_KEY]))

if bot.config.waifu.accept_suggestions:
LOGGER.debug("Waifu suggestions are enabled; loading suggestion cache")
bot.memory[WAIFU_SUGGESTIONS_KEY] = bot.db.get_plugin_value(
'waifu', 'suggestions', [])


def shutdown(bot):
if WAIFU_SUGGESTIONS_KEY in bot.memory:
LOGGER.debug("Persisting waifu suggestion cache to database...")
bot.db.set_plugin_value('waifu', 'suggestions', bot.memory[WAIFU_SUGGESTIONS_KEY])
del bot.memory[WAIFU_SUGGESTIONS_KEY]
LOGGER.debug("...done!")

try:
del bot.memory[WAIFU_LIST_KEY]
except KeyError:
Expand Down Expand Up @@ -115,6 +108,7 @@ def waifu(bot, trigger):

bot.say(msg.format(target=target, waifu=choice))


@plugin.commands('fmk')
@plugin.example('.fmk Peorth', user_help=True)
@plugin.example('.fmk', user_help=True)
Expand All @@ -126,8 +120,10 @@ def fmk(bot, trigger):
sample = random.sample(bot.memory[WAIFU_LIST_KEY], 3)
except ValueError:
condition = 'empty' if len(bot.memory[WAIFU_LIST_KEY]) == 0 else 'too short'
bot.reply("Sorry, looks like the waifu list is {condition}!",
condition=condition)
bot.reply(
"Sorry, looks like the waifu list is {condition}!",
condition=condition,
)
return

sample = [item.replace('$c', formatting.CONTROL_COLOR) for item in sample]
Expand All @@ -153,50 +149,36 @@ def add_waifu(bot, trigger):
bot.reply("Who did you want to suggest?")
return plugin.NOLIMIT

try:
bot.memory[WAIFU_SUGGESTIONS_KEY].append(new_waifu)
except Exception:
bot.reply("I'm terribly sorry, but something has gone very wrong. "
"Please notify my owner, {}".format(bot.config.core.owner))
return
else:
bot.say("Recorded {}'s suggestion for a new waifu: {}".format(trigger.nick, new_waifu))


@plugin.commands('dumpwaifus')
@plugin.require_admin
def dump_waifus(bot, trigger):
"""Dump the list of suggested waifus to a file in Sopel's homedir."""
if not bot.memory.get(WAIFU_SUGGESTIONS_KEY, []):
bot.reply("No waifu suggestions to dump.")
return
file_path = get_waifu_suggestions_file(bot)
created = not os.path.isfile(file_path)

filename = os.path.join(bot.config.core.homedir, 'suggested-waifus.txt')
try:
with open(filename, 'a') as file:
for waifu in bot.memory[WAIFU_SUGGESTIONS_KEY]:
file.write(waifu + '\n')
with open(file_path, 'a') as f:
f.write(new_waifu + '\n')
except Exception:
bot.reply("Sorry, something went wrong.")
bot.reply("I'm terribly sorry, but something has gone very wrong. "
"Please notify my owner, {}.".format(bot.config.core.owner))
return
else:
bot.reply("Dumped waifu suggestions to file. Location sent separately in PM.")
bot.say("Waifu suggestion file: {}".format(filename), trigger.nick)
bot.say("Recorded {}'s suggestion for a new waifu: {}".format(
trigger.nick, new_waifu)
)
if created:
bot.say(
"Created a waifu suggestion file at: {}".format(file_path),
bot.config.core.owner)


@plugin.commands('clearwaifus')
@plugin.require_admin('Only a bot admin can clear my waifu suggestion list.')
def clear_suggestions(bot, trigger):
"""Clear the waifu suggestion cache."""
if WAIFU_SUGGESTIONS_KEY in bot.memory:
if bot.memory[WAIFU_SUGGESTIONS_KEY]:
LOGGER.info('Cached waifu suggestions:')
for waifu in bot.memory[WAIFU_SUGGESTIONS_KEY]:
LOGGER.info(' %s', waifu)
LOGGER.debug('Clearing waifu suggestion cache in memory')
bot.memory[WAIFU_SUGGESTIONS_KEY] = []

LOGGER.debug('Deleting saved waifu suggestions from bot DB')
bot.db.delete_plugin_value('waifu', 'suggestions')
"""Clear the waifu suggestion list."""
file_path = get_waifu_suggestions_file(bot)

if not os.path.isfile(file_path):
bot.reply('There are no waifu suggestions to clear.')
return

LOGGER.info('Deleting saved waifu suggestions from {}'.format(file_path))
os.remove(file_path)
bot.reply("Cleared waifu suggestions.")

0 comments on commit 09388d0

Please sign in to comment.