-
Notifications
You must be signed in to change notification settings - Fork 1
/
__main__.py
162 lines (135 loc) · 5.68 KB
/
__main__.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
import json, os.path, asyncio, datetime
import discord
from discord.ext import commands
import config
import cogs
from Verifier import Verifier
def main():
# define bot
bot = Bot(
description=config.description,
verbose=config.verbose,
bleeding=config.bleeding,
reactive=config.reactive
)
bot.add_cog(cogs.Fun (bot))
bot.add_cog(cogs.Stats(bot))
bot.add_cog(cogs.Info (bot))
bot.add_cog(cogs.Admin(bot))
# launch bot
try:
bot.run(config.token)
except discord.errors.LoginFailure as e:
print(e, end='\n\n')
class Bot(commands.Bot):
def __init__(self, verbose=False, bleeding=False, reactive=True, *args, **kwargs):
# Rewrite the command_prefix flag to force mention
super().__init__(*args, command_prefix=commands.when_mentioned_or('!'), **kwargs)
self.config = {
'verbose' : verbose,
'bleeding' : bleeding,
'reactive' : reactive,
'rank_whitelist_file': 'rank_whitelist.txt',
'admin_roles_file' : 'admin_roles.txt',
'power_admins_file' : 'power_admins.txt'
}
self.rank_whitelist = self.load(self.config['rank_whitelist_file'])
self.admin_roles = self.load(self.config['admin_roles_file'])
self.power_admins = self.load(self.config['power_admins_file'])
def load(self, path):
r = []
try:
with open(path) as f:
for line in f:
r.append(line.rstrip('\n'))
except FileNotFoundError as e:
self.log('Warning.\n'
+ 'Failed to load file {} : file does not exist.\n'.format(path))
return r
def log(self, txt):
if self.config['verbose']:
print(txt)
def is_owner(self, user):
return user.name + '#' + str(user.discriminator) in self.power_admins
def get_text(self, context):
return context.message.content[(len(context.prefix + context.invoked_with)) + 1:]
async def report(self, context, error):
await self.doubt(context)
msg = 'Error !'
embed = discord.Embed(description=str(error), colour=discord.Colour.orange())
await self.send_message(context.message.channel, msg, embed=embed)
async def ok(self, context):
await self.react(context, '\N{OK HAND SIGN}')
async def doubt(self, context):
await self.react(context, '\N{THINKING FACE}')
async def sees(self, context):
await self.react(context, '\N{EYES}')
async def replied(self, context):
await self.react(context, '\N{RIGHTWARDS ARROW WITH HOOK}')
async def react(self, context, emoji=None):
if emoji is None:
return
if self.config['reactive']:
try:
await self.add_reaction(context.message, emoji)
except discord.errors.NotFound:
pass
async def shrug(self, context):
if self.config['reactive']:
await self.send_message(context.message.channel, '¯\_(ツ)_/¯')
async def on_ready(self):
self.log('Logged as {}#{}'.format(self.user.name, self.user.id))
self.log('My boty is ready')
@asyncio.coroutine
def on_message(self, message):
def anti_lag(message2):
# if bot = not a lag (bots do not lag, they are superior entities)
if message.author.bot:
return False
# we ignore messages with embeds
if len(message.embeds) > 0:
return False
# we ignore messages with attachments
if len(message.attachments) > 0:
return False
# same id = not a lag
if message.id == message2.id:
return False
# different author = not a lag
if message.author != message2.author:
return False
# more than 2 minutes delta = not a lag
if message.timestamp - message2.timestamp > datetime.timedelta(0, 20):
return False
# same content = a lag, not same content = not a lag
return message.content == message2.content
def is_pd(message):
content = message.content.casefold()
remove_chars = ['\'', '"', '(', ')', ',', ';', '*', '`', '!', '.']
for char in remove_chars:
content = content.replace(char, '')
if len(content) == 2:
return 'pd' == content
if content[-3:] == ' pd' or content[:3] == 'pd ':
return True
return ' pd ' in content
if self.config['reactive']:
if is_pd(message):
try:
yield from self.add_reaction(message, ':pd:354989542580944906')
except:
pass
# call purge check anti_lag on every message
yield from self.purge_from(message.channel, limit=10, check=anti_lag)
# process commands
yield from self.process_commands(message)
async def on_member_join(self, member):
if self.config['bleeding']:
self.log('Initiating verification procedure for user "{}".'.format(member.name))
msg = 'Please send !register followed by your EPITECH mail adress\n'
msg += 'i.e.: ```!register [email protected]```\n'
msg += 'It has to be an EPITECH address, any other address will not be accepted'
await self.send_message(member, msg)
Verifier.add(member)
if __name__ == '__main__':
main()