-
Notifications
You must be signed in to change notification settings - Fork 0
/
launcher.py
126 lines (108 loc) · 4.76 KB
/
launcher.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
"""Launcher for ;; bot."""
import os # Checking for source updates
import sys # Checking for source updates
import asyncio # Discord requirement
import discord # Discord API wrapper
import core # ;; main source code
from cmds import stime # Had to put it somewhere
class Client(discord.Client):
"""Wrapper around discord.Client class."""
def __init__(self, master='', admins=None, playing=None):
"""Init client."""
super(Client, self).__init__()
self.running = False
self.master = master
self.admins = admins
self.update = {'check': True, 'sources': [], 'last': 0}
self._watchlist = ([], [])
self.game_name = playing
def watch(self, *user_ids):
"""Add users to watchlist."""
self._watchlist[0].extend(list(user_ids))
def watch_output(self, *channels):
"""Add channels to watcher output."""
self._watchlist[1].extend(list(channels))
def check_updates(self, val):
"""Set the auto-update behavior."""
self.update['check'] = val
def run(self, login, password):
"""Start the client."""
if self.update['check']:
self.update['sources'] = [f for f in os.listdir('.') if f.endswith('.py')]
self.update['last'] = max([os.stat(f)[8] for f in self.update['sources']])
self.running = True
super(Client, self).run(login, password)
@asyncio.coroutine
def play(self, game_name):
"""Change client game."""
game = discord.Game
game.name = game_name
yield from self.change_status(game=game)
@asyncio.coroutine
def on_member_update(self, _, after):
"""Monitor status updates and change AFK state accordingly. Also watch users."""
if after.id in self._watchlist[0]:
for channel in self.get_all_channels():
if channel.id in self._watchlist[1]:
print(channel.name, after.name, _.status.name, after.status.name)
yield from self.send_message(channel, ('`Status update: ' + after.name +
' went ' + after.status.name + '`'))
try:
user = core.afk.AFK.get(after.id)
except AttributeError:
pass
else:
if user and user.is_set():
user.check(self)
@asyncio.coroutine
def on_message(self, message):
"""Handle messages from Discord."""
text, author_id, author = message.content, message.author.id, message.author.name
if self.update['check']: # Update check
edit_time = max([os.stat(f)[8] for f in self.update['sources']])
if edit_time > self.update['last'] or (text == 'RLD' and author_id in self.admins):
print(stime() + ' reloading')
os.execl(sys.executable, *([sys.executable]+sys.argv))
if author_id in self.admins and text == ';masterkill':
print(stime() + ' masterkill by ' + author)
self.running = False
self.client.logout()
exit(0)
elif author_id in self.admins and text == ';kill':
print(stime() + ' killed by ' + author)
self.running = False
elif author_id in self.admins and text == ';reload':
print(stime() + ' reloaded by ' + author)
self.running = True
yield from self.send_message(message.channel, '`Bot reloaded.`')
elif self.running:
yield from core.process(self, message, self.admins)
@asyncio.coroutine
def on_ready(self):
"""Initialize bot."""
# Taking good habits
game = None
if self.game_name:
game = discord.Game()
game.name = self.game_name
yield from super(Client, self).change_status(game=game, idle=True)
try:
core.watcher.start(self)
except AttributeError:
print('Error loading watcher.')
def main():
"""Main function."""
# Fetching login data
with open('data/login', 'r') as login_file:
email = login_file.readline()[:-1]
passw = login_file.readline()[:-1]
# Fetching master/admin data
master_id = '111100569845784576'
with open('data/admins') as admin_file:
admins = admin_file.read().splitlines()
client = Client(master_id, admins, playing=None)
client.watch('143120006618677248', '142356131837116417')
client.watch_output('133648084671528961', '142370164376076288', '142380636915630080')
client.run(email, passw)
if __name__ == '__main__':
main()