From 67f8339cd64978f7a675aad93fb65f10ee97a787 Mon Sep 17 00:00:00 2001 From: Oleksandr Kozachuk Date: Mon, 21 Aug 2023 13:29:59 +0200 Subject: [PATCH] Add boreness thread, the bot get borred with time and say something, even if nobody speak with him. --- fjerkroa_bot/discord_bot.py | 42 +++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/fjerkroa_bot/discord_bot.py b/fjerkroa_bot/discord_bot.py index 9aa312c..dcaea3b 100644 --- a/fjerkroa_bot/discord_bot.py +++ b/fjerkroa_bot/discord_bot.py @@ -4,6 +4,10 @@ import tomlkit import discord import logging import re +import random +import time +import asyncio +import math from discord import Message, TextChannel, DMChannel from discord.ext import commands from watchdog.observers import Observer @@ -45,11 +49,40 @@ class FjerkroaBot(commands.Bot): self.aichannels = {chan_name: AIResponder(self.config, chan_name) for chan_name in self.config['additional-responders']} def init_channels(self): + if 'chat-channel' in self.config: + self.chat_channel = self.channel_by_name(self.config['chat-channel'], no_ignore=True) self.staff_channel = self.channel_by_name(self.config['staff-channel'], no_ignore=True) self.welcome_channel = self.channel_by_name(self.config['welcome-channel'], no_ignore=True) + def init_boreness(self): + if 'chat-channel' not in self.config: + return + self.last_activity_time = 0.0 + self.loop.create_task(self.on_boreness()) + logging.info('Boreness initialised.') + + async def on_boreness(self): + logging.info(f'Boreness started on channel: {repr(self.chat_channel)}') + while True: + if self.chat_channel is None: + await asyncio.sleep(7) + continue + boreness_interval = float(self.config.get('boreness-interval', 12.0)) + elapsed_time = (time.monotonic() - self.last_activity_time) / 3600.0 + probability = 1 / (1 + math.exp(-1 * (elapsed_time - (boreness_interval / 2.0)) + math.log(1 / 0.2 - 1))) + if random.random() < probability: + logging.info(f'Borred with {probability} probability after {elapsed_time}') + boreness_prompt = self.config.get('boreness-prompt', 'Pretend that you just now thought of something, be creative.') + message = AIMessage('system', boreness_prompt, self.config.get('chat-channel', 'chat'), True) + try: + await self.respond(message, self.chat_channel) + except Exception as err: + logging.warning(f"Failed to activate borringness: {repr(err)}") + await asyncio.sleep(7) + async def on_ready(self): self.init_channels() + self.init_boreness() logging.info(f"We have logged in as {self.user} ({repr(self.staff_channel)}, {repr(self.welcome_channel)})") async def on_member_join(self, member): @@ -119,8 +152,12 @@ class FjerkroaBot(commands.Bot): return for ma_user in self._re_user.finditer(message_content): uid = int(ma_user.group(1)) - user = message.guild.get_member(uid) - message_content = re.sub(f'[<][@][!]? *{uid} *[>]', f'@{user.name}', message_content) + for guild in self.guilds: + user = guild.get_member(uid) + if user is not None: + break + if user is not None: + message_content = re.sub(f'[<][@][!]? *{uid} *[>]', f'@{user.name}', message_content) channel_name = self.get_channel_name(message.channel) msg = AIMessage(message.author.name, message_content, channel_name, self.user in message.mentions) await self.respond(msg, message.channel) @@ -139,6 +176,7 @@ class FjerkroaBot(commands.Bot): await answer_channel.send(response.answer, files=images, suppress_embeds=True) else: await answer_channel.send(response.answer, suppress_embeds=True) + self.last_activity_time = time.monotonic() async def respond( self,