forked from fourjr/discord-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
257 lines (213 loc) · 9.67 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import aiohttp
import json
import os
import platform
import re
from argparse import ArgumentParser
from getpass import getpass
import colorama
import discord
import requests
from aioconsole.stream import ainput
from discord.ext import commands
from termcolor import cprint
from ext.context import Context
parser = ArgumentParser(description='Runs a Discord Account in the CLI.', usage='main.py token [-c CHANNEL] [-h]')
parser.add_argument('-t', '--token', help='Your discord account/bot token')
parser.add_argument('-c', '--channel', help='A single default channel you want your account to run in', type=int)
args = parser.parse_args()
colorama.init()
# CHECKS TO ENSURE YOU DON'T TRY TO LOAD UP WITH UNSUPPORTED #
if float('.'.join(platform.python_version().split('.')[:2])) < 3.5:
cprint('\n'.join((
'You are using an unsupported version of Python.',
'Please upgrade to at least Python 3.5 to use discord-cli',
'You are currently on ' + platform.python_version()
)), 'red')
exit(0)
# PROGRAM #
class Bot(commands.Bot):
'''Bot subclass to handle CLI IO'''
def __init__(self):
super().__init__(command_prefix='/')
self.session = aiohttp.ClientSession(loop=self.loop)
self.loop.create_task(self.user_input())
self.channel = None
self.is_bot = None
self.paused = False
self.role_converter = commands.RoleConverter()
self.member_converter = commands.MemberConverter()
self.remove_command('help')
for i in [i.replace('.py', '') for i in os.listdir('commands') if i.endswith('.py')]:
self.load_extension('commands.' + i)
cprint('Logging in...', 'green')
self.run()
def pause(self):
self.paused = True
async def on_connect(self):
'''Sets the client presence'''
self.is_bot = self._connection.is_bot
await self.change_presence(status=discord.Status.offline, afk=True)
async def on_ready(self):
'''Sets up the channel'''
self.channel = self.get_channel(args.channel)
if self.channel is None:
if args.channel is not None:
cprint('Invalid channel ID provided.', 'red')
cprint('\n'.join(('Logged in as {0.user} in no specified channel.'.format(self),
'Send a channel ID to start the program')), 'green')
else:
cprint('Logged in as {0.user} in #{0.channel}'.format(self), 'green')
async def on_message(self, message):
'''Prints to console upon new message'''
await self.wait_until_ready()
if not self.channel or self.paused:
return
if message.channel.id == self.channel.id:
if message.author.id == self.user.id:
color = 'cyan'
else:
color = 'yellow'
match = [i.group(0) for i in re.finditer(r'<(@(!?|&?)|#)([0-9]+)>', message.content)]
if match:
for mention in match:
mention_id = int(
mention
.replace('<@', '')
.replace('>', '')
.replace('!', '')
.replace('&', '')
.replace('<#', '')
)
def check(role):
return role.id == mention_id
result = self.get_user(mention_id) or discord.utils.find(check, message.guild.roles) or self.get_channel(mention_id)
message.content = message.content.replace(mention, '@{}'.format(result))
cprint('{0.author}: {0.content}'.format(message), color)
async def user_input(self):
'''Captures user input as a background task asynchronusly'''
await self.wait_until_ready()
while not self.is_closed():
if self.paused:
continue
try:
text = await ainput()
except EOFError:
continue
if text:
ctx = await self.get_context(text)
# MENTION CONVERT #
match = [i.group(1).strip() for i in re.finditer(r'@([^ @]+)', text)]
if match:
for mention in match:
try:
result = await self.member_converter.convert(ctx, mention)
except commands.errors.BadArgument:
try:
result = await self.role_converter.convert(ctx, mention)
except commands.errors.BadArgument:
result = None
if result is not None:
text = text.replace('@' + mention, result.mention)
# END OF MENTION CONVERt #
if ctx.channel:
try:
if ctx.command is None:
await self.channel.send(text)
else:
await ctx.command.invoke(ctx)
except discord.DiscordException as error:
cprint(error, 'red')
async def get_context(self, message):
'''Overwrites the default get_context'''
view = commands.view.StringView(message)
ctx = Context(view=view, bot=self, message=message)
prefix = await self.get_prefix(message)
invoked_prefix = prefix
if isinstance(prefix, str):
if not view.skip_string(prefix):
return ctx
else:
invoked_prefix = discord.utils.find(view.skip_string, prefix)
if invoked_prefix is None:
return ctx
invoker = view.get_word()
ctx.invoked_with = invoker
ctx.prefix = invoked_prefix
ctx.command = self.all_commands.get(invoker)
return ctx
def get_all_guilds(self):
for guild in self.guilds:
yield guild
def run(self):
'''Starts the bot'''
if not getattr(args, 'token'):
email = input('Enter your email: ')
password = getpass('Enter your password: ')
payload = {
'email': email,
'password': password,
'captcha_key': None,
'undelete': False
}
endpoint = 'https://discordapp.com/api/v6/auth/login'
# inspect.currentframe().f_back.f_code.co_name
with requests.post(endpoint, json=payload) as resp:
data = json.loads(resp.text)
if resp.status_code == 400:
if data == {'password': ['Password does not match.']}:
cprint('Invalid credentials provided.', 'red')
elif data == {'email': ['Not a well formed email address.']}:
cprint('Not a well formed email address.', 'red')
elif data == {'captcha_key': ['captcha-required']}:
cprint(''.join(('Due to certain limitations, in order to use this CLI with email/password. ',
'You would have to either:\n-Activate 2FA,',
'\n-Use a token to login, \n-Login on the actual discord recently')), 'red')
else:
cprint('Something else went wrong. Could be invalid email.', 'red')
return
if data.get('mfa'):
cprint('2FA Required', 'cyan')
payload = {
'ticket': data['ticket']
}
if data.get('sms'):
cprint('\n'.join(('If you want your 2FA Code to be sent via SMS, input "SMS" without the quotes.',
'Else, input your 2FA Code.')), 'cyan'
)
sms = input('>')
if sms.upper() == 'SMS':
endpoint = 'https://canary.discordapp.com/api/v6/auth/mfa/sms/send'
auth_code = json.loads(requests.post(endpoint, json=payload).text)
cprint('Code has been sent to {}. Please enter your code below\n>'.format(auth_code['phone']), 'cyan')
payload['code'] = input('>')
endpoint = 'https://canary.discordapp.com/api/v6/auth/mfa/sms'
auth = requests.post(endpoint, json=payload)
elif sms in ('', '\n'):
cprint('Invalid 2FA Code', 'red')
else:
payload['code'] = sms
endpoint = 'https://canary.discordapp.com/api/v6/auth/mfa/totp'
auth = requests.post(endpoint, json=payload)
else:
payload['code'] = sms
endpoint = 'https://canary.discordapp.com/api/v6/auth/mfa/totp'
auth = requests.post(endpoint, json=payload)
auth_data = json.loads(auth.text)
if auth_data == {'code': 60008, 'message': 'Invalid two-factor code'}:
cprint('Invalid 2FA Code', 'red')
return
data['token'] = auth_data['token']
super().run(data['token'], bot=False)
else:
try:
self.loop.run_until_complete(self.start(args.token))
except discord.errors.LoginFailure:
try:
super().run(args.token, bot=False)
except discord.errors.LoginFailure:
cprint('Invalid token provided.', 'red')
finally:
self.loop.close()
if __name__ == '__main__':
Bot()