Skip to content

Commit

Permalink
clean message content
Browse files Browse the repository at this point in the history
  • Loading branch information
sagarreddypatil committed May 21, 2022
1 parent 7752432 commit db38fb7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
5 changes: 4 additions & 1 deletion chatbot/chatbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ def get_queue(self):

queue = property(fget=get_queue)

def reset(self):
def dump(self):
if not os.path.isdir("chatdata"):
os.mkdir("chatdata")

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

def reset(self):
self.dump()

self.start_offset = 0
self.__queue = []

Expand Down
18 changes: 11 additions & 7 deletions discord/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,20 @@ async def on_message(self, message: discord.Message):
self.convos[channel_id] = Conversation(convo_id)
self.model.init_conversation(self.convos[channel_id])

content: str = message.content
content: str = message.clean_content

if content.startswith(cmd_text):
await self.handle_cmd(message)
return

convo = self.convos[message.channel.id]
convo.add_message(
ChatbotMessage(sender=message.author.display_name, message=message.content)
)
convo.add_message(ChatbotMessage(sender=message.author.display_name, message=content))

respond = name.lower() in message.content.lower()
respond = name.lower() in content.lower()
respond = respond or self.user.mentioned_in(message)
respond = respond or (len(message.mentions) == 0 and random() < 0.05)
respond = respond or (
convo.queue[-2].sender == name and ("you" in message.content.lower() or random() < 0.33)
convo.queue[-2].sender == name and ("you" in content.lower() or random() < 0.33)
)

if respond:
Expand Down Expand Up @@ -113,7 +111,7 @@ async def handle_chat(self, message: discord.Message):
)

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

return embed

async def close(self):
for convo in self.convos.values():
convo.dump()

await super().close()


if __name__ == "__main__":
intents = discord.Intents.default()
Expand Down

0 comments on commit db38fb7

Please sign in to comment.