markov.py: get context/target from guild or author rather than hardcoded
This commit is contained in:
parent
10fdf5f33c
commit
cfb7cdf6b2
|
@ -43,6 +43,3 @@ logging.config.dictConfig({
|
|||
},
|
||||
}
|
||||
})
|
||||
|
||||
# module specific configs
|
||||
MARKOV_CONTEXT = os.environ.get('HITOMI_MARKOV_CONTEXT', 'hitomi')
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
import logging
|
||||
from urllib.parse import quote
|
||||
|
||||
from hitomi import bot, config
|
||||
from hitomi import bot
|
||||
from hitomi.backends import dr_botzo, DrBotzoError
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
@ -11,24 +11,34 @@ logger = logging.getLogger(__name__)
|
|||
@bot.listen(name='on_message')
|
||||
async def on_message_learn(message):
|
||||
"""Learn lines, potentially also respond to them."""
|
||||
logger.info("learning to %s: '%s'", config.MARKOV_CONTEXT, message.clean_content)
|
||||
if hasattr(message.channel, 'guild'):
|
||||
target = f'{message.channel.guild.name} ({message.channel.guild.id})'
|
||||
else:
|
||||
target = f'{message.author.name} ({message.author.id})'
|
||||
|
||||
logger.info("learning to %s: '%s'", target, message.clean_content)
|
||||
try:
|
||||
response = dr_botzo.post('/markov/rpc/context/{context}/learn/'.format(context=quote(config.MARKOV_CONTEXT)),
|
||||
response = dr_botzo.post('/markov/rpc/context/{context}/learn/'.format(context=quote(target)),
|
||||
json={'line': message.clean_content})
|
||||
logger.debug("succeeded to learn '%s'", response.json()['line'])
|
||||
except DrBotzoError as drex:
|
||||
logger.exception("received an error from dr.botzo attempting to learn to %s: %s", config.MARKOV_CONTEXT, drex)
|
||||
logger.exception("received an error from dr.botzo attempting to learn to %s: %s", target, drex)
|
||||
|
||||
|
||||
@bot.listen(name='on_message')
|
||||
async def on_message_reply(message):
|
||||
"""Learn lines, potentially also respond to them."""
|
||||
if hasattr(message.channel, 'guild'):
|
||||
target = f'{message.channel.guild.name} ({message.channel.guild.id})'
|
||||
else:
|
||||
target = f'{message.author.name} ({message.author.id})'
|
||||
|
||||
if bot.user in message.mentions:
|
||||
logger.info("I was addressed, going to reply with chatter")
|
||||
await message.channel.trigger_typing()
|
||||
try:
|
||||
response = dr_botzo.post('/markov/rpc/context/{context}/generate/'
|
||||
''.format(context=quote(config.MARKOV_CONTEXT)),
|
||||
''.format(context=quote(target)),
|
||||
json={'topics': message.clean_content.split(' ')})
|
||||
reply = response.json()['generated_line']
|
||||
logger.info("replying to %s with '%s'", message.channel, reply)
|
||||
|
|
Loading…
Reference in New Issue