# -*- coding: utf-8 -*-

from chatbot import api


def merge_dicts(srcdict, mergedict, overwrite=False):
    """Merges mergedict into srcdict and returns srcdict."""
    if overwrite:
        for k, v in mergedict.items():
            srcdict[k] = v
    else:
        diff = set(mergedict) - set(srcdict)
        for i in diff:
            srcdict[i] = mergedict[i]
    return srcdict


def merge_dicts_copy(srcdict, mergedict, overwrite=False):
    """Same as _merge_dicts but returns a shallow copy instead of merging directly into srcdict."""
    ndict = dict(srcdict)
    merge_dicts(ndict, mergedict, overwrite)
    return ndict


async def edit_or_reply(msg: api.ChatMessage, text: str):
    if msg.is_editable():
        await msg.edit(text)
    else:
        await msg.reply(text)

@@ -18,16 +18,81 @@
 -8 -*-%0A%0A
+import asyncio%0Afrom concurrent.futures import ThreadPoolExecutor%0A
 from cha
@@ -843,12 +843,335 @@
 reply(text)%0A
+%0A%0Aasync def run_in_thread(callback, *args):%0A    %22%22%22Spawn a new thread, run a callback in it, and wait until it returns.%0A%0A    Returns what the callback returns.%0A    %22%22%22%0A    loop = asyncio.get_running_loop()%0A    with ThreadPoolExecutor(max_workers=1) as pool:%0A        return await loop.run_in_executor(pool, callback, *args)%0A

Add run_in_thread() utility function