-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
224 lines (175 loc) · 6.93 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
import discord
from discord.ext import commands
import random
from moderation.hate_speech.core import MLP
from moderation.hate_speech.clf import HateSpeechClassifier, Preprocesser
from data.user.db_handler import UsersDataHandler
from utils.replies import *
from utils.loader import *
import torch
import numpy as np
import logging
import os
import pickle
from datetime import datetime
import sqlite3
def start():
'''
Starts the bot
'''
print('Starting bot...')
# set working dir and paths
os.chdir(os.path.dirname(os.path.realpath(__file__)))
print('Loading database...', end ='')
conn = sqlite3.connect('data/user/user.db', detect_types=sqlite3.PARSE_DECLTYPES)
db = UsersDataHandler(conn)
print('DONE')
# set up logging
logger = get_logger()
# set up classifier
clf = get_classifier()
# get replies
replies = get_replies()
_prefix = '!'
bot, client, _client = get_bot_client(_prefix)
@bot.event
async def on_ready():
'''
Triggers when bot is ready to go
'''
print('*** READY ***')
print('Logged in as')
print(bot.user.name)
print(bot.user.id)
print('------')
return
@bot.command()
async def list_commands(ctx):
'''
Command that returns currenly supported list of commands
'''
available_commands = \
f'''
{_prefix}who: Tells you a bit about myself\
\n{_prefix}warned: Tells you if have already been warned\
\n{_prefix}sorry: Removes all your warnings for only the first time\
\n{_prefix}exit [Admin-only]: My cue to leave\
\n{_prefix}thanks: Makes the bot feel good about itself
'''
return await ctx.send(available_commands)
@bot.command()
async def who(ctx):
'''
Returns some basic information about the bot
'''
reply = \
'''
{}, I'm Shuddi, a bot that helps keep your channel free of profanity. To know more about me, head over to my GitHub Repository\
'''.format(random.choice(replies['greet']))
return await ctx.send(reply)
@bot.command()
async def warned(ctx):
'''
Returns the level at which the user has been warned
'''
warning_level = db.get_warning_level(ctx.message.author)
if warning_level is None or warning_level == 0:
reply = f'{ctx.message.author.name}, you have not violated any rules.'
elif warning_level == 1:
reply = f'{ctx.message.author.name}, you already have been warned once.'
else:
reply = f'{ctx.message.author.name}, you have already been warned twice. Next violation will result in a kick.'
return await ctx.send(reply)
@bot.command()
async def sorry(ctx):
'''
Command that lets users strike off their warnings
'''
return await ctx.send(on_apology(ctx))
@bot.command()
async def thanks(ctx):
'''
Command that triggers a 'your're welcome'
'''
return await ctx.send(on_thanks(ctx))
def on_thanks(ctx):
'''
Triggers when the bot says thanks
'''
return random.choice(replies['on_thanks']).format(ctx.message.author.name)
def on_apology(ctx):
'''
Handles apology. If a user has apologized within 24 hours, another apology will not be accepted.
If not, accepts it and strikes off their warnings.
'''
author = ctx.message.author
prev_apology = db.get_prev_apology(author)
if prev_apology == False:
# user does not exist in database
return f'{author.name}, it\'s all good.'
warning_level = db.get_warning_level(author)
if warning_level is None or warning_level == 0:
return f'{author.name}, it\'s all good.'
if prev_apology is not None:
hours_since_last_apology = (datetime.now() - prev_apology).total_seconds() / (60 * 60)
else:
# user has not apologized ever, so go easy on him.
hours_since_last_apology = 100
if hours_since_last_apology >= 24:
_ = db.reset_warning_level(author)
_ = db.set_prev_apology(author)
reply = random.choice(replies['apology_on_n_warning']).format(ctx.message.author.name)
else:
reply = random.choice(replies['unaccepted_apology']).format(ctx.message.author.name)
return reply
@bot.command()
@commands.is_owner()
async def exit(ctx):
'''
Shuts down the bot
'''
await ctx.send(random.choice(replies['on_exit']))
await bot.logout()
@bot.event
async def on_message(message):
'''
On every new message sent in the group, checks if it is a command. if so, processes it.
Also checks if the message is explicit or hateful, if so, issues a warning to the user.
'''
channel = message.channel
try:
if message.content.startswith(_prefix):
# if its a command, process commands and return
return await bot.process_commands(message)
except discord.ext.commands.errors.CommandNotFound:
await channel.send('Invalid command. Please use !list_commands to see the currently supported commands.')
pass
if message.author.bot:
# if author is a bot, ignore
return
if clf.predict(message.content):
return await channel.send(await on_hate_speech(message))
async def on_hate_speech(message):
'''
Increases warning level and takes action against repeat offenders
'''
db.increase_warning_level(message.author)
warning_level = db.get_warning_level(message.author)
if warning_level == 1:
reply = random.choice(replies['on_1_warning']).format(message.author.name)
elif warning_level == 2:
reply = random.choice(replies['on_2_warning']).format(message.author.name)
else:
try:
await message.author.kick(reason='Hateful/profane messages')
reply = f'User {message.author.name} has been kicked.'
except discord.errors.Forbidden as e:
reply = f'Could not kick {message.author}, this incident will be reported.'
logger.warning(f'{e}: Bot tried to kick admin {message.author} for hateful messages')
return reply
try:
with open('utils/token.pickle', 'rb') as save_file:
TOKEN = pickle.load(save_file) # token is unique
except FileNotFoundError:
print('Token not found, please find your token and store it in utils/token.pickle')
bot.run(TOKEN)