-
Notifications
You must be signed in to change notification settings - Fork 3
/
msg_scan.py
76 lines (61 loc) · 2.17 KB
/
msg_scan.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# IRC Bot for help users in #sugar channels (Freenode)
# Copyright (C) 2014, Sam Parkinson <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import json
from fuzzywuzzy import fuzz
data = {'they_know': []}
if os.path.isfile('data.json'):
with open('data.json') as f:
data = json.load(f)
brain = {'langs': []}
if os.path.isfile('brain.json'):
with open('brain.json') as f:
brain = json.load(f)
MIN_SCORE = 95 # Out of 100
sent_count = {}
SPAM_HELP_TEXT = ("Am I spaming you? If so please type "
" 'sugarbot: i know' and I won't spam you")
SEND_SPAM_HELP = 2
def scan_msg(msg, user):
"""
Scan the message to see if the person needs help
"""
if user.lower() in data['they_know']:
return False
for lang in brain['langs']:
for i in lang['signs']:
if fuzz.partial_ratio(str(i), msg) > MIN_SCORE:
sent_count[user] = sent_count.get(user, 0) + 1
if sent_count.get(user, 0) >= SEND_SPAM_HELP:
return lang['out'] + [SPAM_HELP_TEXT]
return lang['out']
def _save_data():
with open('data.json', 'w') as f:
json.dump(data, f)
def they_know_now(nick):
global data
nick = nick.lower()
if not nick in data['they_know']:
data['they_know'].append(nick)
_save_data()
def they_dont_know(nick):
global data
nick = nick.lower()
if nick in data['they_know']:
data['they_know'].remove(nick)
_save_data()