This repository has been archived by the owner on Jan 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·241 lines (199 loc) · 7.92 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
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
from isAllowed import *
# Start up some logging for debugging
logger = logging.getLogger('discord')
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(filename='%sbot.log' %
workingDirectory, encoding='utf-8', mode='w')
handler.setFormatter(logging.Formatter(
'%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)
# Create the bot
description = '''This is my bot I like to make it do things.'''
bot = commands.Bot(command_prefix=['.','<@199136310416375808> '], description=description, pm_help=True)
# Create all of the tokens and keys
discordToken = tokens['discord']
mashapeKey = {"X-Mashape-Key":
tokens['Mashape']}
htmlHead = {'Accept-Endoding': 'identity'}
imgurUsr = ImgurClient(tokens['ImgurClient'], tokens['ImgurSecret'])
youtubeData = yapi.YoutubeAPI(tokens['YoutubeData'])
# Set the paths
mainPath = os.path.dirname(os.path.realpath(__file__))
addonsPath = mainPath + \
"addons/"
# Make the bot more unicode-friendly
non_bmp_map = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0xfffd)
# This uses the imgur API to convert an album into a list of links
def imgurAlbumToItems(albumLink):
if type(albumLink) == str:
imgObj = imgurUsr.get_album_images(albumLink)
ret = ''
for i in imgObj:
ret = ret + i.link + '\n'
ret = ret[:-1]
elif albumLink.is_album:
imgObj = imgurUsr.get_album_images(albumLink.id)
ret = ''
for i in imgObj:
ret = ret + i.link + '\n'
ret = ret[:-1]
else:
ret = albumLink.link
return ret
@bot.event
async def on_member_join(member):
server = member.server
i = giveAllowances(server)
if i['Joins']['Channel'] != '':
server = discord.Object(i['Joins']['Channel'])
if i['Joins']['Enabled'] == 'True':
fmt = i['Joins']['Text'].replace('{mention}',member.mention).replace('{name}',member.name)
await bot.send_message(server, fmt)
@bot.event
async def on_member_ban(member):
server = member.server
i = giveAllowances(server)
if i['Bans']['Channel'] != '':
server = discord.Object(i['Bans']['Channel'])
if i['Bans']['Enabled'] == 'True':
fmt = i['Bans']['Text'].replace('{mention}',member.mention).replace('{name}',member.name)
await bot.send_message(server, fmt)
@bot.event
async def on_member_remove(member):
server = member.server
i = giveAllowances(server)
if i['Leaves']['Channel'] != '':
server = discord.Object(i['Leaves']['Channel'])
if i['Leaves']['Enabled'] == 'True':
fmt = i['Leaves']['Text'].replace('{mention}',member.mention).replace('{name}',member.name)
await bot.send_message(server, fmt)
@bot.event
async def on_channel_update(before, after):
i = giveAllowances(after.server)
toSay = []
if i['ChannelUpdates']['Enabled'] != 'True':
return
if before.name != after.name:
# This will run if the channel's name changed
toSay = toSay + ["This channel's name was changed from `{0.name}` to `{1.name}`".format(before, after)]
if before.topic != after.topic:
# This will run if the chanenl's topic changed
if before.topic in [None, '', ' ']: before.topic = '[None]'
if after.topic in [None, '', ' ']: after.topic = '[None]'
toSay = toSay + ["This channel's topic was changed from `{0.topic}` to `{1.topic}`".format(before, after)]
await bot.send_message(after, '\n\n'.join(toSay))
@bot.event
async def on_server_update(before, after):
i = giveAllowances(after)
toSay = []
if i['ServerUpdates']['Enabled'] != 'True':
return
server = after
if i['ServerUpdates']['Channel'] != '':
server = discord.Object(i['ServerUpdates']['Channel'])
if before.name != after.name:
# This runs if the server's name has changed
toSay = toSay + ["This server's name changed from `{0.name}` to `{1.name}`".format(before, after)]
if before.icon_url != after.icon_url:
# This runs if the icon has changed
bef = '`[None]`' if before.icon_url in [None, '', ' '] else '<{}>'.format(before.icon_url)
aft = '`[None]`' if after.icon_url in [None, '', ' '] else '<{}>'.format(after.icon_url)
toSay = toSay + ["This server's icon changed from {0} to {1}".format(bef, aft)]
await bot.send_message(server, '\n\n'.join(toSay))
@bot.event
async def on_ready():
print("----------")
print("Logged in as:")
print(" " + str(bot.user.name))
print(" " + str(bot.user.id))
with open(workingDirectory+'game.txt') as a:
gameThingy = a.read()
await bot.change_presence(game=discord.Game(name=gameThingy))
print("Game changed to '%s'." % gameThingy)
try:
with open(workingDirectory + 'restartFile.txt', 'r') as a:
cha = discord.Object(id=a.readlines()[0])
await bot.send_message(cha, "\"I'm back, you subnormal halfwit!\"")
os.remove(workingDirectory + 'restartFile.txt')
except FileNotFoundError:
pass
startup_extensions = []
for i in os.listdir(mainPath):
if i.startswith('addon_'):
startup_extensions.append(i[:-3])
for extension in startup_extensions:
try:
bot.load_extension(extension)
except Exception as e:
exc = '{}: {}'.format(type(e).__name__, e)
print('Failed to load extension {}\n{}'.format(extension, exc))
print("----------")
@bot.event
async def on_message(message):
if message.author.bot:
return
z = {
'weed':'🌿',
'blaze':'🔥',
'nice':'👌',
'weed.':'🌿',
'blaze.':'🔥',
'nice.':'👌'
}
try:
await bot.add_reaction(message, z[message.content.lower()])
except:
pass
if message.content.lower() in ('kill me', 'kill me.', '*kill me*'):
await bot.send_message(message.channel, '{} *Later*'.format(str(message.author.mention)))
# The people on my server are idiots.
if message.content.lower().startswith('+volume') and message.server.id == '178070516353990657':
num = message.content.split(' ')[1]
try:
if int(num) > 100:
await bot.send_message(message.channel, "+volume 100")
except:
pass
# Get custom commands
continueWithComms = True
aq = giveAllowances(message.server.id)
# Read custom commands
try:
with open(serverConfigs + message.server.id + '.json', 'r', encoding='utf-8') as data_file:
customCommands = json.load(data_file)['CustomCommands']
try:
await bot.send_message(message.channel, eval(customCommands[message.content.lower()]).translate(non_bmp_map))
continueWithComms = False
except KeyError:
pass
except KeyError:
pass
except FileNotFoundError:
pass
# Read Imgur album into images
if 'imgur.com/' in message.content and aq['ImgurAlbum']['Enabled'] == 'True':
print('yee')
if 'imgur.com/a/' in message.content:
imLink = message.content.split('imgur.com/a/')[1][:5]
elif 'imgur.com/gallery/' in message.content:
imLink = message.content.split('imgur.com/gallery/')[1][:5]
try:
imLink = imgurAlbumToItems(imLink)
x = [message.author.mention] + [i for i in imLink.split('\n')]
y = z = ''
while True:
if x == []:
break
y = z + x[0] + '\n'
if len(y) > 2000:
await bot.send_message(message.channel, z)
y = z = ''
else:
z = y
del x[0]
# await bot.send_message(message.channel, '{}\n{}'.format(message.author.mention, imLink))
except UnboundLocalError:
pass
if continueWithComms:
await bot.process_commands(message)
bot.run(discordToken)