Updated logging

This commit is contained in:
cameron 2024-05-10 16:08:06 -04:00
parent a190e523d4
commit f0fc5b643a
3 changed files with 25 additions and 19 deletions

10
main.py
View File

@ -11,23 +11,25 @@ class DankBot(discord.ext.commands.Bot):
plugin_files = glob.glob(os.path.join(plugin_path, '**', 'plugin.py'), recursive=True) plugin_files = glob.glob(os.path.join(plugin_path, '**', 'plugin.py'), recursive=True)
for plugin_file in plugin_files: for plugin_file in plugin_files:
plugin_name = os.path.basename(os.path.dirname(plugin_file)) plugin_name = os.path.basename(os.path.dirname(plugin_file))
print(f"Loading plugin {plugin_name}") logger.info(f"Loading plugin {plugin_name}")
await self.load_extension(f'plugins.{plugin_name}.plugin') await self.load_extension(f'plugins.{plugin_name}.plugin')
async def on_ready(self): async def on_ready(self):
print(f'Logged on as {self.user}!') logger.info(f'Logged on as {self.user}!')
await self.load_plugins() await self.load_plugins()
await self.change_presence(activity=discord.Game("with the bots")) await self.change_presence(activity=discord.Game("with the bots"))
async def on_message(self, message): async def on_message(self, message):
print(f'Message from {message.author}: {message.content}') logger.info(f'Message from {message.author}: {message.content}')
await self.process_commands(message) await self.process_commands(message)
async def main(): async def main():
intents = discord.Intents.default() intents = discord.Intents.default()
intents.message_content = True intents.message_content = True
intents.members = True intents.members = True
logging.basicConfig(level=logging.INFO) discord.utils.setup_logging(level=logging.INFO, root=True)
global logger
logger = logging.getLogger("bot.main")
with open(".token") as token_file: with open(".token") as token_file:
token = token_file.read() token = token_file.read()

View File

@ -6,7 +6,9 @@ import aiohttp
import yaml import yaml
import random import random
import os import os
import logging
logger=logging.getLogger("plugin.botchat")
plugin_folder=os.path.dirname(os.path.realpath(__file__)) plugin_folder=os.path.dirname(os.path.realpath(__file__))
prompts_folder=os.path.join(plugin_folder, 'prompts') prompts_folder=os.path.join(plugin_folder, 'prompts')
default_prompt="default.txt" default_prompt="default.txt"
@ -20,24 +22,24 @@ async def prompt_llm(prompt):
:param prompt: prompt to complete :param prompt: prompt to complete
:return: returns a string consisting of completion text :return: returns a string consisting of completion text
""" """
print("Prompting LLM") logger.info("Prompting LLM")
print(f"PROMPT DATA\n{prompt}") logger.info(f"PROMPT DATA\n{prompt}")
async with aiohttp.ClientSession(llm_data["api_base"]) as session: async with aiohttp.ClientSession(llm_data["api_base"]) as session:
async with session.post("/completion", json={"prompt": prompt, "n_predict": 250, "mirostat": 2}) as resp: async with session.post("/completion", json={"prompt": prompt, "n_predict": 350, "mirostat": 2}) as resp:
print(f"LLM response status {resp.status}") logger.info(f"LLM response status {resp.status}")
response_json=await resp.json() response_json=await resp.json()
content=response_json["content"] content=response_json["content"]
return content return content
def get_message_contents(msg): def get_message_contents(msg):
""" """
Given a Discord message object, prints the contents in an IRC-like format Given a Discord message object, logger.infos the contents in an IRC-like format
:param msg: discord.Message to get the contents of :param msg: discord.Message to get the contents of
:return: returns a string in the format "user: message" :return: returns a string in the format "user: message"
""" """
message_text = f"{msg.author.name}: {msg.clean_content}" message_text = f"{msg.author.name}: {msg.clean_content}"
print(f"Message contents -- {message_text}") logger.info(f"Message contents -- {message_text}")
return message_text return message_text
async def get_chat_history(ctx, limit=20): async def get_chat_history(ctx, limit=20):
@ -98,7 +100,7 @@ async def send_chat_responses(ctx, response_text):
:param ctx: Message context that we're replying to :param ctx: Message context that we're replying to
:param response_text: String containing message we want to send :param response_text: String containing message we want to send
""" """
print("Processing chat response") logger.info("Processing chat response")
fullResponseLog = "dank-bot:" + response_text # first response won't include the user fullResponseLog = "dank-bot:" + response_text # first response won't include the user
responseLines = fullResponseLog.splitlines() responseLines = fullResponseLog.splitlines()
output_strs = [] output_strs = []
@ -147,28 +149,28 @@ async def handle_message(ctx):
:param ctx: Message context :param ctx: Message context
""" """
print("Dank-bot received message") logger.info("Dank-bot received message")
print(f"Dank-bot ID is {llm_data['bot'].user.id}") logger.info(f"Dank-bot ID is {llm_data['bot'].user.id}")
bot_id = llm_data['bot'].user.id bot_id = llm_data['bot'].user.id
# First case, bot DMed # First case, bot DMed
if (isinstance(ctx.channel,discord.DMChannel) and ctx.author.id != bot_id): if (isinstance(ctx.channel,discord.DMChannel) and ctx.author.id != bot_id):
print("Dank-bot DMed, responding") logger.info("Dank-bot DMed, responding")
await llm_response(ctx) await llm_response(ctx)
return return
# Second case, bot mentioned # Second case, bot mentioned
bot_mentions=list(filter(lambda x: x.id == bot_id, ctx.mentions)) bot_mentions=list(filter(lambda x: x.id == bot_id, ctx.mentions))
if (len(bot_mentions) > 0): if (len(bot_mentions) > 0):
print("Dank-bot mentioned, responding") logger.info("Dank-bot mentioned, responding")
await llm_response(ctx) await llm_response(ctx)
return return
# Other case, random response # Other case, random response
random_roll = random.random() random_roll = random.random()
print(f"Dank-bot rolled {random_roll}") logger.info(f"Dank-bot rolled {random_roll}")
if (random_roll < llm_data['response_probability']): if (random_roll < llm_data['response_probability']):
print(f"{random_roll} < {llm_data['response_probability']}, responding") logger.info(f"{random_roll} < {llm_data['response_probability']}, responding")
await llm_response(ctx) await llm_response(ctx)
return return
@ -185,4 +187,4 @@ async def setup(bot):
bot.add_command(llm_response) bot.add_command(llm_response)
bot.add_listener(handle_message, "on_message") bot.add_listener(handle_message, "on_message")
llm_data["bot"] = bot llm_data["bot"] = bot
print("LLM interface initialized") logger.info("LLM interface initialized")

View File

@ -1,9 +1,11 @@
# Posts a dank pepe in the chat # Posts a dank pepe in the chat
from discord.ext import commands from discord.ext import commands
import discord import discord
import logging
import random import random
import os import os
logger=logging.getLogger("plugin.pepe")
plugin_folder=os.path.dirname(os.path.realpath(__file__)) plugin_folder=os.path.dirname(os.path.realpath(__file__))
pepe_filename=os.path.join(plugin_folder, 'pepes.txt') pepe_filename=os.path.join(plugin_folder, 'pepes.txt')
@ -16,5 +18,5 @@ async def send_pepe(ctx):
await ctx.send(f'{chosen_pepe}') await ctx.send(f'{chosen_pepe}')
async def setup(bot): async def setup(bot):
print("Rare Pepes initialized") logger.info("Rare Pepes initialized")
bot.add_command(send_pepe) bot.add_command(send_pepe)