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
/
addon_notifications.py
314 lines (289 loc) · 12.3 KB
/
addon_notifications.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
307
308
309
310
311
312
313
314
from isAllowed import *
youtubeData = yapi.YoutubeAPI(tokens['YoutubeData'])
# Returns streamID if channel live, otherwise -1
def IsTwitchLive(streamID):
url = str('https://api.twitch.tv/kraken/streams/'+streamID)
respose = requests.get(url,headers={'Client-ID':tokens['TwitchTV'],'Accept':'application/vnd.twitchtv.v5+json'})
html = respose.text
data = json.loads(html)
if str(data['stream']) == str(None):
return -1
try:
return str(data['stream']['game'])
except:
return 'unknown'
return -1
async def twitchChecker(bot):
await bot.wait_until_ready()
print("Twitch check starting")
fmt = '@everyone the channel `{0}` is live on Twitch playing `{1}`! Check it out! <http://twitch.tv/{0}>'
while True:
for i in bot.servers:
serverStuff = giveAllowances(i.id)
if serverStuff['Streams']['Channel'] != '':
toPostTo = discord.Object(serverStuff['Streams']['Channel'])
else:
toPostTo = i
try:
streams = serverStuff['Streams']['TwitchTV']
for o in streams:
q = IsTwitchLive(streams[o][0])
if streams[o][1] != str(q) and str(q) != '-1':
await bot.send_message(toPostTo, fmt.format(o, str(q)))
streams[o][1] = str(q)
# await bot.send_message(toPostTo, 'The channel `{0}` does not exist or has more then one result; please delete from config.'.format(o))
serverStuff['Streams']['TwitchTV'] = streams
writeAllow(i.id, serverStuff)
except Exception as e: # Find correct exception type
print("IsTwitchLive Failed: {}".format(e))
await asyncio.sleep(60)
# Returns list of new video if any
def NewYoutubeVid(channelItem, doLive):
newVids = {'vids' : {}, 'max_date' : ""}
dates = []
lastFive = youtubeData.get_playlist_items_by_playlist_id(channelItem['UploadsPlaylist'], max_results=5)
for video in lastFive.items:
if video.snippet.publishedAt > channelItem['lastPostDate']:
dates.append(str(video.snippet.publishedAt))
newVids['vids'][str(video.snippet.resourceId.videoId)] = str(video.snippet.title)
if len(dates) != 0:
newVids['max_date'] = max(dates)
return newVids
async def youtubeChecker(bot):
await bot.wait_until_ready()
print("Youtube check starting")
fmt = '@everyone the channel `{}` has posted a new video on youtube, `{}` Check it out! <https://youtu.be/{}>'
while True:
for i in bot.servers:
serverStuff = giveAllowances(i.id)
try:
serverStuff['Youtube']
except KeyError:
serverStuff['Youtube'] = defSerCon['Youtube']
if serverStuff['Youtube']['Channel'] != '':
toPostTo = discord.Object(serverStuff['Youtube']['Channel'])
else:
toPostTo = i
channels = serverStuff['Youtube']['Channels']
try:
for c in channels:
newVids = NewYoutubeVid(channels[c], serverStuff['Youtube']['DoLive'])
for video in newVids['vids']:
await bot.send_message(toPostTo, fmt.format(channels[c]['Name'], newVids['vids'][video], str(video)))
# await bot.send_message(toPostTo, 'The channel `{0}` does not exist or has more then one result; please delete from config.'.format(o))
serverStuff['Youtube']['Channels'][c]['lastPostDate'] = newVids['max_date']
writeAllow(i.id, serverStuff)
except Exception as e: # Find correct exception type
print("NewYoutubeVid Failed: {}".format(e))
await asyncio.sleep(300)
class NotificationsCommands():
def __init__(self, bot):
self.bot = bot
@commands.group(pass_context=True)
async def twitch(self, ctx):
if allowUse(ctx, ['manage_server']):
pass
else:
await self.bot.say(notallowed)
@twitch.command(pass_context=True,name='add')
async def twitchAdd(self, ctx, channelName:str):
if not allowUse(ctx, ['manage_server']):
return
i = giveAllowances(ctx)
try:
strm = i['Streams']['TwitchTV']
except KeyError:
i['Streams'] = defSerCon['Streams']
strm = i['Streams']['TwitchTV']
if channelName.lower() in strm:
await self.bot.say("This user has already been added to the streamer list for this server.")
return
url = str('https://api.twitch.tv/kraken/users?login='+channelName)
streamID = -1
respose = requests.get(url,headers={'Client-ID':tokens['TwitchTV'],'Accept':'application/vnd.twitchtv.v5+json'})
html = respose.text
data = json.loads(html)
if data['_total'] == 1:
try:
channelID = data['users'][0]['_id']
except:
await self.bot.say("This user does not exist.")
return
else:
await self.bot.say("This user does not exist.")
return
strm[channelName.lower()] = [channelID, '-1']
i['Streams']['TwitchTV'] = strm
writeAllow(ctx, i)
await self.bot.say("This user has now been added to the streamer list for this server.")
@twitch.command(pass_context=True,name='del',aliases=['delete','rem','remove'])
async def twitchDel(self, ctx, channelID:str):
if not allowUse(ctx, ['manage_server']):
return
i = giveAllowances(ctx)
try:
strm = i['Streams']['TwitchTV']
except KeyError:
i['Streams'] = defSerCon['Streams']
writeAllow(ctx, i)
strm = i['Streams']['TwitchTV']
if channelID.lower() not in strm:
await self.bot.say("This user isn't in the streamer list for this server.")
return
del strm[channelID.lower()]
i['Streams']['TwitchTV'] = strm
writeAllow(ctx, i)
await self.bot.say("This user has now been removed from the streamer list for this server.")
@twitch.command(pass_context=True,name='list',aliases=['users'])
async def twitchList(self, ctx):
if not allowUse(ctx, ['manage_server']):
return
i = giveAllowances(ctx)
try:
strm = i['Streams']['TwitchTV']
except KeyError:
i['Streams'] = defSerCon['Streams']
writeAllow(ctx, i)
strm = i['Streams']['TwitchTV']
users_list = "Twitch Users:"
if len(strm) != 0:
for item in strm:
users_list += ('\n\t' + str(item))
else:
users_list += '\nNone'
await self.bot.say(users_list)
@twitch.command(pass_context=True,name='set')
async def twitchSet(self, ctx, channelID:str):
if not allowUse(ctx, ['manage_server']):
return
i = giveAllowances(ctx)
try:
chan = ctx.message.raw_channel_mentions[0]
except IndexError:
await self.bot.say("Please provide a channel.")
return
try:
i['Streams']['Channel'] = chan
except KeyError:
i['Streams'] = defSerCon['Streams']
i['Streams']['Channel'] = chan
writeAllow(ctx, i)
await self.bot.say("The stream announcements have now been set to <#{}>".format(chan))
@commands.group(pass_context=True)
async def youtube(self, ctx):
if allowUse(ctx, ['manage_server']):
pass
else:
await self.bot.say(notallowed)
@youtube.command(pass_context=True,name='add')
async def youtubeAdd(self, ctx, channelURL:str):
if not allowUse(ctx, ['manage_server']):
return
i = giveAllowances(ctx)
try:
strm = i['Youtube']['Channels']
except KeyError:
i['Youtube'] = defSerCon['Youtube']
strm = i['Youtube']['Channels']
for channel in strm:
if channelURL.lower() == strm[channel]['ChannelURL']:
await self.bot.say("This user has already been added to the channel list for this server.")
return
findChannel = youtubeData.get_channel_by_name(channelURL)
if len(findChannel.items) == 0:
findChannel = youtubeData.get_channel_by_id(channelURL)
if len(findChannel.items) == 0:
await self.bot.say("This channel does not exist.")
return
channel = findChannel.items[0]
try:
customUrl = channel.snippet.customUrl
except AttributeError:
customUrl = channel.id
strm[str(channel.snippet.title).lower()] = \
{
"Name" : str(channel.snippet.title),
"ID" : str(channel.id),
"ChannelURL" : str(customUrl),
"UploadsPlaylist" : str(channel.contentDetails.relatedPlaylists.uploads),
"lastPostDate" : str(datetime.datetime.now().isoformat())
}
i['Youtube']['Channels'] = strm
writeAllow(ctx, i)
await self.bot.say("The channel `{}` has now been added to the channel list for this server.".format(str(channel.snippet.title)))
@youtube.command(pass_context=True,name='del',aliases=['delete','rem','remove'])
async def youtubeDel(self, ctx, ChannelName:str):
if not allowUse(ctx, ['manage_server']):
return
i = giveAllowances(ctx)
try:
strm = i['Youtube']['Channels']
except KeyError:
i['Youtube'] = defSerCon['Youtube']
writeAllow(ctx, i)
strm = i['Youtube']['Channels']
if ChannelName.lower() not in strm:
await self.bot.say("This channel isn't in the channel list for this server.")
return
del strm[ChannelName.lower()]
i['Youtube']['Channels'] = strm
writeAllow(ctx, i)
await self.bot.say("This channel has now been removed from the channel list for this server.")
@youtube.command(pass_context=True,name='list',aliases=['users'])
async def youtubeList(self, ctx):
if not allowUse(ctx, ['manage_server']):
return
i = giveAllowances(ctx)
try:
strm = i['Youtube']['Channels']
except KeyError:
i['Youtube'] = defSerCon['Youtube']
writeAllow(ctx, i)
strm = i['Youtube']['Channels']
users_list = "Youtube Channels:"
if len(strm) != 0:
for item in strm:
users_list += ('\n\t' + str(strm[item]['Name']))
else:
users_list += '\nNone'
await self.bot.say(users_list)
@youtube.command(pass_context=True,name='set')
async def youtubeSet(self, ctx, channelID:str):
if not allowUse(ctx, ['manage_server']):
return
i = giveAllowances(ctx)
try:
chan = ctx.message.raw_channel_mentions[0]
except IndexError:
await self.bot.say("Please provide a channel.")
return
try:
i['Youtube']['Channel'] = chan
except KeyError:
i['Youtube'] = defSerCon['Youtube']
i['Youtube']['Channel'] = chan
writeAllow(ctx, i)
await self.bot.say("The new video announcements have now been set to <#{}>".format(chan))
@youtube.command(pass_context=True,name='toggle')
async def youtubeToggle(self, ctx, toggle:str):
if not allowUse(ctx, ['manage_server']):
return
i = giveAllowances(ctx)
if toggle == None or toggle == "":
self.bot.say("Please provide a toggle (True/False)")
return
if toggle.lower() in ['f', 'false']:
chan = "False"
elif toggle.lower() in ['t', 'true']:
chan = "True"
try:
i['Youtube']['DoLive'] = chan
except KeyError:
i['Youtube'] = defSerCon['Youtube']
i['Youtube']['DoLive'] = chan
writeAllow(ctx, i)
await self.bot.say("Live video announcements have now been set to `{}`".format(chan))
def setup(bot):
bot.loop.create_task(twitchChecker(bot))
bot.loop.create_task(youtubeChecker(bot))
bot.add_cog(NotificationsCommands(bot))