Skip to content

Commit a1f4e17

Browse files
AmbratolmAmbratolm
authored andcommitted
Reworked AI: cog, initiative system w/ task managment
1 parent 15900b2 commit a1f4e17

File tree

13 files changed

+504
-444
lines changed

13 files changed

+504
-444
lines changed

bot/cogs/ai_cog.py

Lines changed: 284 additions & 237 deletions
Large diffs are not rendered by default.

bot/cogs/board_cog.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import asyncio
2-
31
from discord import Color, Embed, Guild, Interaction, Member, User, app_commands
42
from discord.ext.commands import Cog
5-
from humanize import intcomma, intword, metric, naturalsize, naturaltime, ordinal
3+
from humanize import intcomma, naturalsize, naturaltime
64
from odmantic import query
7-
from tabulate import tabulate
85

96
from bot.main import ActBot
107
from bot.ui import EmbedX

bot/cogs/console_cog.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
from discord import Interaction, app_commands
1+
from discord import ClientException, Interaction, VoiceChannel, app_commands
22
from discord.ext.commands import Cog
33

44
from bot.main import ActBot
55
from bot.ui import EmbedX
6-
from db.actor import Actor
76

87

98
# ----------------------------------------------------------------------------------------------------
@@ -30,21 +29,24 @@ async def sync(self, interaction: Interaction, global_sync: bool = True):
3029
ephemeral=True,
3130
)
3231

33-
# @app_commands.guild_only()
34-
# @app_commands.checks.has_permissions(administrator=True)
35-
# @app_commands.command(description="Synchronize commands")
36-
# async def update_actors(self, interaction: Interaction):
37-
# if not interaction.guild:
38-
# return
39-
# await interaction.response.defer(ephemeral=True)
40-
# db = self.bot.get_db(interaction.guild)
41-
# actors = db.find(Actor)
42-
# members = interaction.guild.members
43-
# for member in members:
44-
# for actor in actors:
45-
# if actor.id == member.id:
46-
47-
# if not actor:
48-
# actor = self.bot.create_actor(member)
49-
# db.save_all(actors)
50-
# await interaction.followup.send(embed=EmbedX.success(title="", description=f"{len(members)}/{len(actors)} actors updated."))
32+
# ----------------------------------------------------------------------------------------------------
33+
# * Join
34+
# ----------------------------------------------------------------------------------------------------
35+
@app_commands.default_permissions(administrator=True)
36+
@app_commands.checks.has_permissions(administrator=True)
37+
@app_commands.command(description="Add bot to a voice channel")
38+
async def join(self, interaction: Interaction, channel: VoiceChannel):
39+
try:
40+
await channel.connect()
41+
await interaction.response.send_message(
42+
embed=EmbedX.info(f"Joined {channel.name}."), ephemeral=True
43+
)
44+
except ClientException:
45+
await interaction.response.send_message(
46+
embed=EmbedX.warning("Already in a voice channel."), ephemeral=True
47+
)
48+
except Exception as e:
49+
await interaction.response.send_message(
50+
embed=EmbedX.error(f"Could not join the voice channel: {e}"),
51+
ephemeral=True,
52+
)

bot/cogs/farm_cog.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import re
22
from random import randint
33

4-
from discord import Color, Embed, Member, Message, Role, utils
4+
from discord import Member, Message, Role, utils
55
from discord.ext.commands import Cog
66

77
from bot.main import ActBot

bot/cogs/filter_cog.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from collections import defaultdict
22
from datetime import timedelta
33

4-
from discord import Color, Embed, Member, Message
4+
from discord import Member, Message
55
from discord.ext.commands import Cog
66
from profanity_check import predict_prob
77

@@ -17,7 +17,7 @@
1717
# * Filter Cog
1818
# ----------------------------------------------------------------------------------------------------
1919
class FilterCog(Cog, description="Filters blacklisted message content."):
20-
TOLERANCE = 0.99 # Between 0 and 1
20+
TOLERANCE = 0.99 # 99 %
2121
MAX_OFFENSES = 5
2222
GOLD_PENALTY = 500
2323

@@ -100,9 +100,11 @@ async def on_message(self, message: Message):
100100
# ----------------------------------------------------------------------------------------------------
101101

102102
@classmethod
103-
def get_profane_words(cls, words: list[str]) -> list[str]:
103+
def get_profane_words(cls, words: list[str]) -> list[str] | None:
104104
"""Get list of profane words from given list. If non found, get empty list."""
105-
predictions = predict_prob(words)
105+
predictions = predict_prob(words) if words else None
106+
if predictions is None:
107+
return None
106108
profane_words = []
107109
for i, word in enumerate(words):
108110
if predictions[i] >= cls.TOLERANCE: # 1 means profane, 0 means clean

bot/cogs/purge_cog.py

Lines changed: 0 additions & 174 deletions
This file was deleted.

db/actor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from datetime import datetime
2-
from typing import ClassVar, Optional
2+
from typing import Any, ClassVar, Optional
33

44
from odmantic import Field, Model
55
from pydantic import NonNegativeInt
@@ -19,6 +19,7 @@ class Actor(Model):
1919
avatar_url: str = ""
2020

2121
ai_interacted_at: Optional[datetime] = None # Last time actor interacted with AI
22+
ai_chat_history: list[dict[str, Any]] = []
2223

2324
xp: NonNegativeInt = 0
2425
level: NonNegativeInt = 0

db/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515

1616
# ----------------------------------------------------------------------------------------------------
17-
# * Database reference
17+
# * Database Reference
1818
# ----------------------------------------------------------------------------------------------------
1919
class DbRef(Model):
2020
"""Database model for storage of reference and relevant data about a database."""

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ dependencies = [
1414
"humanize>=4.11.0",
1515
"odmantic>=1.0.2",
1616
"pydantic>=2.10.6",
17+
"pynacl>=1.5.0",
1718
"python-dotenv>=1.0.1",
1819
"tabulate>=0.9.0",
1920
]

utils/ai.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def use_session(self, id: int, history: list[Content] | None = None) -> Chat:
6767
] # Sanitize history to ensure correct dumping in dump_history()
6868
chat = self._client.chats.create(model=self.model_name, history=history) # type: ignore
6969
self._chats[id] = chat
70-
self._current_chat_id = id
70+
self._current_chat_id = id
7171
return chat
7272

7373
def dump_history(self, id: int | None = None, history_max_items=20) -> list[dict]:

0 commit comments

Comments
 (0)