Skip to content

Commit db38fb7

Browse files
clean message content
1 parent 7752432 commit db38fb7

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

chatbot/chatbot.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,17 @@ def get_queue(self):
3434

3535
queue = property(fget=get_queue)
3636

37-
def reset(self):
37+
def dump(self):
3838
if not os.path.isdir("chatdata"):
3939
os.mkdir("chatdata")
4040

4141
uid = f"{self.id}_{datetime.now().isoformat()}"
4242
with open(f"chatdata/{uid}.txt", "w") as f:
4343
f.write(self.summary())
4444

45+
def reset(self):
46+
self.dump()
47+
4548
self.start_offset = 0
4649
self.__queue = []
4750

discord/main.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,20 @@ async def on_message(self, message: discord.Message):
6767
self.convos[channel_id] = Conversation(convo_id)
6868
self.model.init_conversation(self.convos[channel_id])
6969

70-
content: str = message.content
70+
content: str = message.clean_content
7171

7272
if content.startswith(cmd_text):
7373
await self.handle_cmd(message)
7474
return
7575

7676
convo = self.convos[message.channel.id]
77-
convo.add_message(
78-
ChatbotMessage(sender=message.author.display_name, message=message.content)
79-
)
77+
convo.add_message(ChatbotMessage(sender=message.author.display_name, message=content))
8078

81-
respond = name.lower() in message.content.lower()
79+
respond = name.lower() in content.lower()
8280
respond = respond or self.user.mentioned_in(message)
8381
respond = respond or (len(message.mentions) == 0 and random() < 0.05)
8482
respond = respond or (
85-
convo.queue[-2].sender == name and ("you" in message.content.lower() or random() < 0.33)
83+
convo.queue[-2].sender == name and ("you" in content.lower() or random() < 0.33)
8684
)
8785

8886
if respond:
@@ -113,7 +111,7 @@ async def handle_chat(self, message: discord.Message):
113111
)
114112

115113
async def handle_cmd(self, message: discord.Message):
116-
content = shlex.split(message.content)[1:]
114+
content = shlex.split(message.clean_content)[1:]
117115
try:
118116
args = parser.parse_args(content)
119117
except EarlyExit as e:
@@ -166,6 +164,12 @@ def create_embed(self, author, title: str, description: str, footer=None) -> dis
166164

167165
return embed
168166

167+
async def close(self):
168+
for convo in self.convos.values():
169+
convo.dump()
170+
171+
await super().close()
172+
169173

170174
if __name__ == "__main__":
171175
intents = discord.Intents.default()

0 commit comments

Comments
 (0)