Add boreness thread, the bot get borred with time and say something, even if nobody speak with him.

This commit is contained in:
OK 2023-08-21 13:29:59 +02:00
parent 9d1d99ac50
commit 67f8339cd6

View File

@ -4,6 +4,10 @@ import tomlkit
import discord import discord
import logging import logging
import re import re
import random
import time
import asyncio
import math
from discord import Message, TextChannel, DMChannel from discord import Message, TextChannel, DMChannel
from discord.ext import commands from discord.ext import commands
from watchdog.observers import Observer 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']} self.aichannels = {chan_name: AIResponder(self.config, chan_name) for chan_name in self.config['additional-responders']}
def init_channels(self): 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.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) 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): async def on_ready(self):
self.init_channels() self.init_channels()
self.init_boreness()
logging.info(f"We have logged in as {self.user} ({repr(self.staff_channel)}, {repr(self.welcome_channel)})") 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): async def on_member_join(self, member):
@ -119,7 +152,11 @@ class FjerkroaBot(commands.Bot):
return return
for ma_user in self._re_user.finditer(message_content): for ma_user in self._re_user.finditer(message_content):
uid = int(ma_user.group(1)) uid = int(ma_user.group(1))
user = message.guild.get_member(uid) 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) message_content = re.sub(f'[<][@][!]? *{uid} *[>]', f'@{user.name}', message_content)
channel_name = self.get_channel_name(message.channel) channel_name = self.get_channel_name(message.channel)
msg = AIMessage(message.author.name, message_content, channel_name, self.user in message.mentions) msg = AIMessage(message.author.name, message_content, channel_name, self.user in message.mentions)
@ -139,6 +176,7 @@ class FjerkroaBot(commands.Bot):
await answer_channel.send(response.answer, files=images, suppress_embeds=True) await answer_channel.send(response.answer, files=images, suppress_embeds=True)
else: else:
await answer_channel.send(response.answer, suppress_embeds=True) await answer_channel.send(response.answer, suppress_embeds=True)
self.last_activity_time = time.monotonic()
async def respond( async def respond(
self, self,