33import datetime
44import importlib
55import json
6- import mimetypes
76import os
87import random
98import re
2726import sbds
2827import steam_news
2928import twitch
30- import twitter
31- import warframe
3229from timer import readable_rel
3330
3431class Bot :
@@ -47,12 +44,7 @@ def __init__(self, commands):
4744 self .heartbeat_thread = None
4845 self .timer_thread = None
4946 self .timer_condvar = threading .Condition ()
50- self .zkill_thread = None
51- self .warframe_thread = None
5247 self .twitch_thread = None
53- self .twitter_thread = None
54- self .twitter_post_thread = None
55- self .twitter_post_condvar = threading .Condition ()
5648 self .instagram_thread = None
5749 self .steam_news_thread = None
5850 self .advent_of_code_thread = None
@@ -66,8 +58,6 @@ def __init__(self, commands):
6658 'READY' : self .handle_ready ,
6759 'MESSAGE_CREATE' : self .handle_message_create ,
6860 'INTERACTION_CREATE' : self .handle_interaction_create ,
69- 'MESSAGE_REACTION_ADD' : self .handle_reaction_add ,
70- 'MESSAGE_REACTION_REMOVE' : self .handle_reaction_remove ,
7161 'GUILD_CREATE' : self .handle_guild_create ,
7262 'GUILD_ROLE_CREATE' : self .handle_guild_role_create ,
7363 'GUILD_ROLE_UPDATE' : self .handle_guild_role_update ,
@@ -258,16 +248,8 @@ def handle_ready(self, d):
258248 if self .timer_thread is not None :
259249 return
260250 self .timer_thread = _thread .start_new_thread (self .timer_loop , ())
261- if config .bot .zkillboard is not None :
262- self .zkill_thread = _thread .start_new_thread (self .zkill_loop , ())
263- if config .bot .warframe is not None :
264- self .warframe_thread = _thread .start_new_thread (self .warframe_loop , ())
265251 if config .bot .twitch is not None :
266252 self .twitch_thread = _thread .start_new_thread (self .twitch_loop , ())
267- if config .bot .twitter is not None :
268- self .twitter_thread = _thread .start_new_thread (self .twitter_loop , ())
269- if config .bot .twitter_post is not None :
270- self .twitter_post_thread = _thread .start_new_thread (self .twitter_post_loop , ())
271253 if config .bot .instagram is not None :
272254 self .instagram_thread = _thread .start_new_thread (self .instagram_loop , ())
273255 if config .bot .steam_news is not None :
@@ -349,59 +331,6 @@ def _autoreload(self, command_name, handler):
349331 # continue replacing all the commands in the reloaded file; do not break/return
350332 return handler
351333
352- def handle_reaction_add (self , d ):
353- if config .bot .twitter_post is None :
354- return
355-
356- if d ['channel_id' ] != config .bot .twitter_post ['channel' ] or \
357- d ['emoji' ]['name' ] != 'shrfood_twitter' or d ['user_id' ] == self .user_id :
358- return
359-
360- if d ['message_id' ] in config .state .twitter_queue :
361- return
362-
363- message = self .get_message (d ['channel_id' ], d ['message_id' ])
364- attachments = message .get ('attachments' )
365- if not attachments :
366- self .react (d ['channel_id' ], d ['message_id' ], '🙈' )
367- return
368-
369- for attachment in attachments [:4 ]:
370- media_type , _ = mimetypes .guess_type (attachment ['filename' ])
371- if media_type .startswith ('video/' ):
372- if len (attachments ) != 1 :
373- self .react (d ['channel_id' ], d ['message_id' ], '🧐' )
374- return
375- if attachment ['size' ] > 5000000 :
376- self .react (d ['channel_id' ], d ['message_id' ], '😓' )
377- return
378-
379- config .state .twitter_queue .append (d ['message_id' ])
380- config .state .save ()
381-
382- self .react (d ['channel_id' ], d ['message_id' ], '✅' )
383- with self .twitter_post_condvar :
384- self .twitter_post_condvar .notify ()
385-
386- def handle_reaction_remove (self , d ):
387- if config .bot .twitter_post is None :
388- return
389-
390- if d ['channel_id' ] != config .bot .twitter_post ['channel' ] or \
391- d ['emoji' ]['name' ] != 'shrfood_twitter' :
392- return
393-
394- emoji = '%s:%s' % (d ['emoji' ]['name' ], d ['emoji' ]['id' ])
395- reactions = self .get_reactions (d ['channel_id' ], d ['message_id' ], emoji )
396- if len (reactions ) == 0 : # no more shrfood_twitter emoji
397- try :
398- config .state .twitter_queue .remove (d ['message_id' ])
399- except ValueError :
400- return
401- config .state .save ()
402-
403- self .remove_reaction (d ['channel_id' ], d ['message_id' ], '✅' )
404-
405334 def handle_guild_create (self , d ):
406335 log .write ('in guild %s (%d members)' % (d ['name' ], d ['member_count' ]))
407336 self .guilds [d ['id' ]] = Guild (d )
@@ -465,52 +394,6 @@ def timer_loop(self):
465394 with self .timer_condvar :
466395 self .timer_condvar .wait (wakeup )
467396
468- def zkill_loop (self ):
469- while True :
470- r = self .rs .get ('https://redisq.zkillboard.com/listen.php' , params = {'ttw' : 30 })
471- if r .ok :
472- data = r .json ()
473- if not data or not data ['package' ]:
474- time .sleep (10 )
475- continue
476- killmail = data ['package' ]['killmail' ]
477- victim = killmail ['victim' ]
478-
479- characters = killmail ['attackers' ]
480- characters .append (victim )
481- for char in characters :
482- if 'alliance' in char and char ['alliance' ]['id' ] == config .bot .zkillboard ['alliance' ]:
483- break
484- else : # alliance not involved in kill
485- continue
486-
487- if 'character' not in victim :
488- continue
489- victim_name = victim ['character' ]['name' ]
490- ship = victim ['shipType' ]['name' ]
491- cost = data ['package' ]['zkb' ]['totalValue' ] / 1000000
492- url = 'https://zkillboard.com/kill/%d/' % killmail ['killID' ]
493- self .send_message (config .bot .zkillboard ['channel' ],
494- "%s's **%s** (%d mil) %s" % (victim_name , ship , cost , url ))
495- else :
496- log .write ('zkill: %s %s\n %s' % (r .status_code , r .reason , r .text [:1000 ]))
497- time .sleep (30 )
498-
499- def warframe_loop (self ):
500- last_alerts = []
501- while True :
502- time .sleep (5 * 60 )
503- try :
504- alerts = warframe .alert_analysis ()
505- broadcast_alerts = set (alerts ) - set (last_alerts )
506- if len (broadcast_alerts ) > 0 :
507- self .send_message (config .bot .warframe ['channel' ], '\n ' .join (broadcast_alerts ))
508- last_alerts = alerts
509- except requests .exceptions .HTTPError as e :
510- log .write ('warframe: %s\n %s' % (e , e .response .text [:1000 ]))
511- except requests .exceptions .RequestException as e :
512- log .write ('warframe: %s' % e )
513-
514397 def twitch_loop (self ):
515398 while True :
516399 # https://dev.twitch.tv/docs/api/guide#rate-limits
@@ -523,48 +406,6 @@ def twitch_loop(self):
523406 except requests .exceptions .RequestException as e :
524407 log .write ('twitch: %s' % e )
525408
526- def twitter_loop (self ):
527- while True :
528- # https://developer.twitter.com/en/docs/tweets/timelines/api-reference/get-statuses-user_timeline.html
529- # 100,000 in 24 hours is 69.4 a minute, so wait 1 minute per account (1 request per account)
530- time .sleep (60 * len (config .bot .twitter ['accounts' ]))
531- try :
532- twitter .new_tweets (self )
533- except requests .exceptions .HTTPError as e :
534- log .write ('twitter: %s\n %s' % (e , e .response .text [:1000 ]))
535- except requests .exceptions .RequestException as e :
536- log .write ('twitter: %s' % e )
537-
538- def twitter_post_loop (self ):
539- while True :
540- if len (config .state .twitter_queue ) == 0 :
541- with self .twitter_post_condvar :
542- self .twitter_post_condvar .wait ()
543- continue
544-
545- sleep = 12 * 60 * 60 # 12 hours
546- if config .state .twitter_last_post_time :
547- sleep = config .state .twitter_last_post_time + sleep - time .time ()
548- with self .twitter_post_condvar :
549- self .twitter_post_condvar .wait (sleep )
550- if config .state .twitter_last_post_time and \
551- time .time () < config .state .twitter_last_post_time + 12 * 60 * 60 :
552- # we were woken up by a reaction add but it's too early
553- continue
554-
555- try :
556- twitter .post (self , config .state .twitter_queue [0 ])
557- config .state .twitter_queue .pop (0 )
558- except requests .exceptions .HTTPError as e :
559- log .write ('twitter: %s\n %s' % (e , e .response .text [:1000 ]))
560- except requests .exceptions .RequestException as e :
561- log .write ('twitter: %s' % e )
562- except Exception :
563- log .write ('twitter post:\n ' + traceback .format_exc ())
564- # always update the last post time, even if we failed to tweet
565- config .state .twitter_last_post_time = int (time .time ())
566- config .state .save ()
567-
568409 def instagram_loop (self ):
569410 while True :
570411 # https://developers.facebook.com/docs/graph-api/overview/rate-limiting#platform-rate-limits
0 commit comments