Add logging

This commit is contained in:
Edgar P. Burkhart 2024-09-06 20:12:51 +02:00
parent dafbabe0b8
commit db695b0805
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227

View file

@ -1,4 +1,5 @@
import asyncio
import logging
import pickle
import random
import tomllib
@ -9,6 +10,9 @@ from .ai import AIBot
description = """BotBotBot"""
logger = logging.getLogger("botbotbot")
logging.basicConfig(level=logging.DEBUG)
with open("config.toml", "rb") as config_file:
config = tomllib.load(config_file)
@ -24,7 +28,7 @@ Ta réponse doit être une seule phrase.
TA RÉPONSE DOIT ÊTRE EN FRANÇAIS !!!"""
aibot = AIBot(
config.get("mistral_api_key"),
model="open-mixtral-8x7b",
model="open-mixtral-8x22b",
system_message=system_prompt,
)
@ -42,10 +46,11 @@ shuffle_tasks = set()
@bot.listen("on_ready")
async def on_ready():
print(f"We have logged in as {bot.user}")
logger.info(f"We have logged in as {bot.user}")
async def reply(message):
logger.info(f"Reply to {message.author}")
mention = random.choices(
[f"<@{message.author.id}>", "@everyone", "@here"], weights=(98, 1, 1)
)[0]
@ -61,7 +66,7 @@ async def reply(message):
async def ai_reply(message):
print("Running AI")
logger.info(f"AI Reply to {message.author}")
prompt = message.clean_content
if prompt == "" and message.embeds:
prompt = message.embeds[0].description
@ -86,6 +91,7 @@ async def on_message(message):
return
if message.author != bot.user and bot.user in message.mentions:
logger.info(f"{message.author} metion")
await random.choice((reply, react))(message)
return
@ -101,38 +107,45 @@ async def on_message(message):
@bot.listen("on_reaction_add")
async def add_more_reaction(reaction, user):
if random.random() < 50 / 100:
logger.info(f"Copy reaction from {user}")
await reaction.message.add_reaction(reaction.emoji)
@bot.listen("on_message_edit")
async def react_message_edit(before, after):
if after.author != bot.user and random.random() < 50 / 100:
logger.info(f"React to edit from {after.author}")
await after.add_reaction("👀")
@bot.listen("on_message")
async def rando_shuffle(message):
if not message.flags.ephemeral and random.random() < 5 / 100 and message.guild:
logger.info(f"Message shuffle after message from {message.author}")
await try_shuffle(message.guild)
def save_wordlist():
logger.info("Saving updated wordlist")
with open("wordlist.pickle", "wb") as word_file:
pickle.dump(word_list, word_file)
@bot.slash_command(name="bibl", guild_ids=guild_ids, description="Ajouter une phrase")
async def bibl(ctx, phrase):
logger.info(f"BIBL {ctx.author} {phrase}")
word_list.append(phrase)
embed = discord.Embed(
title="BIBL", description=phrase, color=discord.Colour.green()
)
await ctx.respond(embed=embed)
save_wordlist()
logger.info("FIN BIBL")
@bot.slash_command(name="tabl", guild_ids=guild_ids, description="Lister les phrases")
async def tabl(ctx):
logger.info(f"TABL {ctx.author}")
embed = discord.Embed(
title="TABL", description="\n".join(word_list), color=discord.Colour.green()
)
@ -141,6 +154,7 @@ async def tabl(ctx):
@bot.slash_command(name="enle", guild_ids=guild_ids, description="Enlever une phrase")
async def enle(ctx, phrase):
logger.info(f"ENLE {ctx.author} {phrase}")
try:
word_list.remove(phrase)
except ValueError:
@ -148,12 +162,14 @@ async def enle(ctx, phrase):
title="ERRE ENLE", description=phrase, color=discord.Colour.red()
)
await ctx.respond(embed=embed)
logger.info("ERRE ENLE")
else:
embed = discord.Embed(
title="ENLE", description=f"~~{phrase}~~", color=discord.Colour.green()
)
await ctx.respond(embed=embed, ephemeral=True, delete_after=delay)
save_wordlist()
logger.info("FIN ENLE")
async def try_shuffle(guild):
@ -167,7 +183,7 @@ async def try_shuffle(guild):
async def shuffle_nicks(guild):
print("ALEA")
logger.info("Shuffle")
members = guild.members
members.remove(guild.owner)
@ -175,45 +191,61 @@ async def shuffle_nicks(guild):
random.shuffle(nicks)
for member, nick in zip(members, nicks):
print(member, nick)
logger.info(member, nick)
await member.edit(nick=nick)
print("FIN ALEA")
logger.info("Shuffle done")
@bot.slash_command(name="alea", guild_ids=guild_ids, description="Modifier les pseudos")
async def alea(ctx):
logger.info(f"ALEA {ctx.author}")
await ctx.defer()
if await try_shuffle(ctx.guild):
embed = discord.Embed(title="ALEA", color=discord.Colour.green())
await ctx.respond(embed=embed, ephemeral=True, delete_after=delay)
logger.info("FIN ALEA")
else:
embed = discord.Embed(title="ERRE ALEA", color=discord.Colour.red())
await ctx.respond(embed=embed)
logger.info("ERRE ALEA")
@bot.listen("on_voice_state_update")
async def voice_random_nicks(member, before, after):
if before.channel is None and random.random() < 5 / 100:
logger.info(f"Voice shuffle from {member}")
await try_shuffle(member.guild)
logger.debug("Voice state update")
logger.debug(before.channel)
logger.debug(after.channel)
if after.channel:
logger.debug(after.channel.members)
if (
before.channel is None
and after.channel is not None
and random.random() < 5 / 100
and bot not in after.channel.members
):
logger.info(f"Voice connect from {member}")
source = await discord.FFmpegOpusAudio.from_probe("assets/allo.ogg")
await asyncio.sleep(random.randrange(60))
vo = await after.channel.connect()
source = await discord.FFmpegOpusAudio.from_probe("assets/allo.ogg")
await asyncio.sleep(random.randrange(10))
vo.play(source)
await asyncio.sleep(random.randrange(60))
await vo.disconnect()
logger.info("Voice disconnect")
@bot.slash_command(
name="indu", guild_ids=guild_ids, description="Poser une question à MistralAI"
)
async def indu(ctx, prompt):
logger.info(f"INDU {ctx.author} {prompt}")
await ctx.defer()
res_stream = aibot.get_response_stream(prompt)
@ -232,12 +264,14 @@ async def indu(ctx, prompt):
embed.color = None
await message.edit(embed=embed)
logger.info("FIN INDU")
@bot.slash_command(
name="chan", guild_ids=guild_ids, description="Donner de nouveaux pseudos"
)
async def chan(ctx, file: discord.Attachment):
logger.info(f"CHAN {ctx.author}")
await ctx.defer()
members = ctx.guild.members
@ -251,13 +285,14 @@ async def chan(ctx, file: discord.Attachment):
random.shuffle(nicks)
for member, nick in zip(members, nicks):
print(member, nick)
logger.info(member, nick)
await member.edit(nick=nick)
embed = discord.Embed(
title="CHAN", description="\n".join(nicks), color=discord.Colour.green()
)
await ctx.respond(embed=embed)
logger.info("FIN CHAN")
bot.run(config.get("token"))