-
Notifications
You must be signed in to change notification settings - Fork 3
/
bot.py
306 lines (254 loc) · 11.2 KB
/
bot.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# / \
# [o]<===========
# /===\
# /_____\
# / \
# /_______\
# / \
# /_________\
#
# Cedar Sentinel
# A Discord Bot for using trained models to detect spam
# Built for the Pine64 Chat Network
# Copyright 2021-2022 Matthew Petry (fireTwoOneNine), Samuel Sloniker (kj7rrv)
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
# OR OTHER DEALINGS IN THE SOFTWARE.
from importlib import import_module
from re import M
from time import sleep
from urllib import response
import discord
import irc.bot
import gptc
import yaml
from yaml.loader import SafeLoader
import json
import datetime
import http.client
import pprint
import cedarscript
optionalModules = ["cv2", "pytesseract", "numpy", "requests"]
try:
for module in optionalModules:
import_module(module)
ocrAvailable = True
# following line is redundant, but is provided to suppress errors from language helpers like Pylance
import cv2
import pytesseract
import numpy
import requests
print("All optional modules loaded. OCR features are available (if enabled in config)")
except ImportError as error:
print("Unable to import " + module + ". OCR features are unavailable.")
ocrAvailable = False
version = "0.6.2"
configFile = "config.yaml"
knownUsers = {}
def change_reputation(username, change):
try:
knownUsers[username] = knownUsers[username] + change
except KeyError:
knownUsers[username] = change
if config["persistKnownUsers"]:
with open(config["persistFile"], "w") as f:
json.dump(knownUsers, f)
def get_reputation(username):
try:
return knownUsers[username]
except KeyError:
knownUsers[username] = 0
if config["persistKnownUsers"]:
with open(config["persistFile"], "w") as f:
json.dump(knownUsers, f)
return 0
# Extract username of message sender, and return status based on classification and/or known user bypass
def handle_message(author, content, attachments=[]):
if author == config["bridgeBot"]:
author = content.split(">", 1)[0]
author = author.split("<", 1)[1].replace("@", "").strip()
content = content.split(">", 1)[1]
# TODO: OCR functionality for image links on IRC
if (config["ocrEnable"] == True) and (ocrAvailable == True):
for item in attachments:
# very naive filetype checker
# TODO: find a better way to check image type and validity.
filetypes = [".jpg", ".jpeg", ".png"]
for ftype in filetypes:
if ftype in item.url:
targetImage = requests.get(item.url)
targetArray = numpy.frombuffer(targetImage.content, numpy.uint8)
targetImageCV = cv2.imdecode(targetArray, cv2.IMREAD_UNCHANGED)
targetText = pytesseract.image_to_string(targetImageCV)
if config["debugMode"]:
print("OCR result: " + targetText.strip())
content = content + targetText
break
confidences = {
"good": 0,
"spam": 0,
} # set defaults for good and spam to prevent KeyErrors in parsing
confidences.update(classifier.confidence(content))
content = content.strip()
author = author.strip()
confidence = confidences["spam"]
length = len(content)
reputation = get_reputation(author)
actions = interpreter.interpret(confidence, length, reputation)
if "log" in actions:
logMessage(content, confidence)
if "increasereputation" in actions:
change_reputation(author, 1)
elif "decreasereputation" in actions:
change_reputation(author, -1)
flag = "flag" in actions or "moderate" in actions
moderate = "moderate" in actions
return flag, moderate, confidence, author, content
# Prepare and send notification about detected spam
async def sendNotifMessage(message, confidence=0.0, customMessage=""):
notifChannel = None
notifPing = ""
for channel in message.guild.text_channels:
if channel.name == config["notificationChannel"]:
notifChannel = channel
if notifChannel is None:
notifChannel = message.channel
print("Notification channel not found! Sending in same channel as potential spam.")
for role in message.guild.roles:
if role.name == config["spamNotifyPing"]:
notifPing = role.mention
if customMessage == "":
await notifChannel.send(f'{notifPing} {"**"} {config["spamNotifyMessage"]} {"**"} {message.jump_url}')
if config["debugMode"]:
await notifChannel.send(f"DEBUG: Confidence value on the above message is: {confidence}")
else:
await notifChannel.send(customMessage)
# log message to file for later analysis
def logMessage(message, confidence):
with open(config["spamFile"], "a") as f:
logTime = str(datetime.datetime.today())
f.write(",\n") # append to previous JSON dump, and make this prettier for human eyes
logEntry = {"time": logTime, "message": message, "confidence": confidence}
json.dump(logEntry, f)
async def messageDeleter(message):
if config["autoDeleteAPI"] == "customMB":
bridge = http.client.HTTPConnection(config["bridgeURL"])
bridge.request("DELETE", "/api/message", headers={"Content-Type": "application/json"}, body='{"id": "%s", "channel": "%s", "protocol": "discord", "account": "discord.mydiscord"}' % (message.id, message.channel.name))
response = bridge.getresponse()
responseText = response.read()
await sendNotifMessage(message, customMessage="**Automatic Deletion Result:** %s" % (responseText.decode("utf-8").strip()))
await sendNotifMessage(message, customMessage="**Message was:** `%s`" % (message.content))
alertMsg = await message.channel.send(config["publicDeleteNotice"])
sleep(10)
bridge.request("DELETE", "/api/message", headers={"Content-Type": "application/json"}, body='{"id": "%s", "channel": "%s", "protocol": "discord", "account": "discord.mydiscord"}' % (alertMsg.id, alertMsg.channel.name))
if config["autoDeleteAPI"] == "discord":
try:
await message.delete()
except discord.Forbidden:
await sendNotifMessage(message, customMessage="**Automatic Deletion Result:** No permission")
return
except discord.NotFound:
await sendNotifMessage(message, customMessage="**Automatic Deletion Result:** Message does not exist")
return
except discord.HTTPException:
await sendNotifMessage(message, customMessage="**Automatic Deletion Result:** Failed")
return
await sendNotifMessage(message, customMessage="**Automatic Deletion Result:** OK")
alertMsg = await message.channel.send(config["publicDeleteNotice"])
sleep(10)
await alertMsg.delete()
class BotInstance(discord.Client):
async def on_ready(self):
print(f"Logged on as {self.user}!")
async def on_message(self, message):
if not (message.author == bot.user):
author = message.author.name + "#" + message.author.discriminator
content = message.content
attachments = message.attachments
flag, moderate, confidence, author, content = handle_message(author, content, attachments)
print(f"Message from {author} -> {message.channel}: {content}")
if config["debugMode"]:
print(confidence)
print(f"Flagging: {flag}; Moderating: {moderate}")
if flag:
await sendNotifMessage(message, confidence)
if moderate:
if not (config["autoDeleteAPI"] == "none"):
await messageDeleter(message)
class CedarSentinelIRC(irc.bot.SingleServerIRCBot):
def on_nicknameinuse(self, c, e):
c.nick(c.get_nickname() + "_")
def on_welcome(self, connection, event):
print("Connected!")
for target in config["channels"].split(" "):
connection.join(target)
if config["notificationChannel"].startswith("#"):
connection.join(config["notificationChannel"])
def on_join(self, connection, event):
print(f"Joined {event.target}!")
def on_pubmsg(self, connection, event):
author = event.source.split("!")[0].strip()
content = event.arguments[0]
flag, moderate, confidence, author, content = handle_message(author, content)
print()
print(f"Message from {author} -> {event.target}: {content}")
if config["debugMode"]:
print(confidence)
print(f"Flagging: {flag}")
if flag:
notification_channel = config["notificationChannel"]
if notification_channel == "*":
notification_channel = event.target
connection.privmsg(
notification_channel,
f'{config["spamNotifyPing"]}: {config["spamNotifyMessage"]} ({author} -> {event.target}) {content}',
)
if config["debugMode"]:
connection.privmsg(
notification_channel,
f"DEBUG: Confidence value on the above message is: {confidence}",
)
print("Cedar Sentinel version " + version + " starting up.")
print()
# load files
with open(configFile) as f:
config = yaml.load(f, Loader=SafeLoader)
print("Configuration loaded!")
pprint.pprint(config)
print()
if config["persistKnownUsers"]:
try:
with open(config["persistFile"]) as f:
knownUsers = json.load(f)
print("Known users file loaded!")
except:
print("No known users file found. Starting fresh.")
print()
with open(config["spamModel"]) as f:
spamModel = json.load(f)
classifier = gptc.Classifier(spamModel)
print("GPTC model loaded!")
print()
with open("script.txt") as f:
script = f.read()
interpreter = cedarscript.Interpreter(script)
print("CedarScript interpreter loaded!")
print()
# Startup
if config["platform"] == "discord":
bot = BotInstance()
bot.run(config["discordToken"])
elif config["platform"] == "irc":
bot = CedarSentinelIRC(
[irc.bot.ServerSpec(config["ircServer"], int(config["ircPort"]))],
config["ircNick"],
config["ircNick"],
)
bot.start()