-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpathfinderbot.py
352 lines (284 loc) · 12.2 KB
/
pathfinderbot.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/usr/bin/env python3
'''
-------------------------
The MIT License (MIT)
Copyright (c) 2015-2016 Rapptz
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
-------------------------
Modified by Pathfinders
'''
import os
import json
import random
import aiohttp
import discord
from discord.ext import commands
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
from aiohttp import web
import shelve
description = '''Based on an example bot to showcase the discord.ext.commands
extension module, by Rapptz
There are a number of utility commands being showcased here.'''
bot = commands.Bot(command_prefix='!', description=description)
### Use this function to get text (HTML) from URLs asynchronously ###
async def get_page(url):
"""
Accepts a URL.
Asynchronously retrieves a page from URL and returns it.
"""
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
return await resp.text()
### Use this function to get a raw response from URLs asynchronously ###
async def get_json(url):
"""
Accepts a URL.
Asynchronously retrieves a JSON response from URL and returns it.
"""
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
return web.json_response(resp)
@bot.event
async def on_ready():
print('Logged in as')
print(bot.user.name)
print(bot.user.id)
print('------')
@bot.command()
async def ping():
"""Responds with "Pong!\""""
await bot.say('Pong!')
@bot.command()
async def pathfinders():
"""Info about the Pathfinders group."""
with open('missive.txt') as info:
for line in info.readlines():
await bot.say(line)
return
@bot.command(description='For when you wanna settle the score some other way')
async def choose(*choices: str):
"""Chooses between multiple choices."""
await bot.say(random.choice(choices))
@bot.command()
async def repeat(times: int, content='repeating...'):
"""Repeats a message multiple times."""
if times < 10:
for _ in range(times):
await bot.say(content)
else:
await bot.say("Really? Don't you think that's a little excessive?")
@bot.command()
async def joined(member: discord.Member):
"""Says when a member joined."""
await bot.say('{0.name} joined in {0.joined_at}'.format(member))
@bot.event
async def on_member_join(member):
# Appears to be broken.
"""Welcomes a new member to the server and provides introductory info."""
server = member.server
fmt = 'Welcome {0.mention} to {1.name}!'
await bot.send_message(server, fmt.format(member, server))
# Should be cleaned up and made to be one bot.say() call
welcome = [
'Welcome to the server, {0.name}.'.format(member),
'This is a learning environment. Please do not be afraid to ask ' +
'questions in the general channel. Others may benefit from your ' +
'question as well.',
'Please take a moment to tell us which subjects you are focusing ' +
'on learning, and which subjects you have experience with. ' +
'This is so appropriate "roles" (labels) can be applied to you. ' +
'Examples: Programmer, PHP, WebDesign, HTML5, LinuxAdmin, IT Security'
]
for line in welcome:
await bot.say(line)
@bot.group(pass_context=True)
async def cool(ctx):
"""Says if a user is cool.
In reality this just checks if a subcommand is being invoked.
"""
if ctx.invoked_subcommand is None:
await bot.say('No, {0.subcommand_passed} is not cool'.format(ctx))
@cool.command(name='bot')
async def _bot():
"""Is the bot cool?"""
await bot.say('Yes, the bot is cool.')
@bot.command()
async def rps(*, message: str):
"""Plays Rock Paper Scissors with the bot."""
# Check to see if scores exist yet
try:
d = shelve.open('rps_scores')
_ = d['wins']
except Exception:
d = shelve.open('rps_scores')
d['wins'] = 0
d['loss'] = 0
d['draws'] = 0
# Change user input to full lenght string for to say back later when telling who won
if message.lower() == 'r':
player_choice = 'rock'
elif message.lower() == 'p':
player_choice = "paper"
elif message.lower() == 's':
player_choice = 'scissors'
else:
await bot.say('Please try again using "!rps" + "r" or "p" or "s" to pick your play')
return
# print("player: ", player_choice)
bot_choice = random.choice(['rock', 'paper', 'scissors'])
# print("bot: ", bot_choice)
# Game logic:
# 3x if/elif for the players 3 possible inputs
# 2x if/elifs inside these to check if there is a win or loss in that game
# else catches if there is a draw
# TODO - lots of repetition in this section, could win/loss be decided by a function??
if player_choice == 'rock':
if bot_choice == 'scissors':
await bot.say('Your {} beats my {}! YOU WIN'.format(player_choice, bot_choice))
d['wins'] += 1
await bot.say('Human wins: {}, Human losses: {}, Draws: {}'.format(d['wins'], d['loss'], d['draws']))
d.close()
print('test')
elif bot_choice == 'paper':
await bot.say('My {} beats your {}! YOU LOSE'.format(bot_choice, player_choice))
d['loss'] += 1
await bot.say('Human wins: {}, Human losses: {}, Draws: {}'.format(d['wins'], d['loss'], d['draws']))
d.close()
print('test')
elif player_choice == 'paper':
if bot_choice == 'rock':
await bot.say('Your {} beats my {}! YOU WIN'.format(player_choice, bot_choice))
d['wins'] += 1
await bot.say('Human wins: {}, Human losses: {}, Draws: {}'.format(d['wins'], d['loss'], d['draws']))
d.close()
elif bot_choice == 'scissors':
await bot.say('My {} beats your {}! YOU LOSE'.format(bot_choice, player_choice))
d['loss'] += 1
await bot.say('Human wins: {}, Human losses: {}, Draws: {}'.format(d['wins'], d['loss'], d['draws']))
d.close()
elif player_choice == 'scissors':
if bot_choice == 'paper':
await bot.say('Your {} beats my {}! YOU WIN'.format(player_choice, bot_choice))
d['wins'] += 1
await bot.say('Human wins: {}, Human losses: {}, Draws: {}'.format(d['wins'], d['loss'], d['draws']))
d.close()
elif bot_choice == 'rock':
await bot.say('My {} beats your {} ! YOU LOSE'.format(bot_choice, player_choice))
d['loss'] += 1
await bot.say('Human wins: {}, Human losses: {}, Draws: {}'.format(d['wins'], d['loss'], d['draws']))
d.close()
if player_choice == bot_choice:
await bot.say('Your {} draws with my {}.'.format(player_choice, bot_choice))
d['draws'] += 1
await bot.say('Human wins: {}, Human losses: {}, Draws: {}'.format(d['wins'], d['loss'], d['draws']))
d.close()
@bot.command()
async def random_module():
"""Links to a random Python 3 module from the standard library."""
my_url = 'https://docs.python.org/3/py-modindex.html'
# get page, get soup
page_html = await get_page(my_url)
page_soup = soup(page_html, 'html.parser')
containers = page_soup.find_all("td")
module_list = []
for container in containers:
# check if text contained
if container.text != '':
# check if there is a link present
if container.a != None:
# print(container.text)
# print(container.a['href'])
module_list.append((container.text.strip(), container.a['href']))
selected_module = (random.choice(module_list))
url_base = 'https://docs.python.org/3/'
await bot.say('You should do some reading about:\n {} , \nClick this link to read more: \n \
{}{}'.format(selected_module[0], url_base, selected_module[1]))
@bot.command()
# command to get the first definition of a word using the Oxford English Dictionary API
# TODO - currently just gets the first definition, would like it to return them all.
async def definition(*, message: str):
"""Displays the first definition for a word according to oxforddictionaries.com."""
try:
app_id = oxford_id
app_key = oxford_key
base_url = 'https://od-api.oxforddictionaries.com/api/v1/entries/'
language = 'en'
word_id = message
page_url = base_url + language + '/' + word_id.lower()
reqheaders = {'app_id': app_id, 'app_key': app_key}
async with aiohttp.request(method='GET', url=page_url, headers=reqheaders) as resp:
json_data = json.loads(await resp.text())
# url = base_url + language + '/' + word_id.lower()
# json_data = requests.get(url, headers = {'app_id': app_id, 'app_key': app_key}).json()
# json_data = json.loads(resp.text)
# print(json_data)
# json_data = json.loads(resp)
# for printing info
# r = await get_page(url, headers = {'app_id': app_id, 'app_key': app_key})
# print("****code {}\n".format(r.status_code))
# print("****text \n" + r.text)
# print("****json \n" + json.dumps(r.json()))
# Some kind of loop needed here to get all definitions insead of just the first one
definitions = json_data['results'][0]['lexicalEntries'][0]['entries'][0]['senses'][0]['definitions'][0]
await bot.say('The first definition of ' + message + ' I have is :\n' + definitions)
except Exception as ex:
# print(ex)
# If it cant find the word
await bot.say('Are you sure thats a word in the dictionary?')
@bot.command()
async def coin(*, message: str):
"""
Coin command is returning the price from a specific coin in USD.
"""
r = await get_page('https://min-api.cryptocompare.com/data/price?fsym=' + message.upper() + '&tsyms=USD')
# changed to account for async page loading
# my_data = r.json()
my_data = json.loads(r)
for k, v in my_data.items():
if str(v) == 'Error':
await bot.say(f"{message.upper()} coin doesn't exists.")
break
await bot.say(f"{message.upper()} price is {str(v)} {k.upper()}.")
@bot.command()
async def pydoc(*, message: str):
"""
Post a link to the python doc page for the requested module in chat.
"""
page = await get_page('https://docs.python.org/3/py-modindex.html')
page_soup = soup(page, 'html.parser')
base_url = 'https://docs.python.org/3/library/'
# Check that requested module name is valid.
mod_list = [mod.text for mod in page_soup.findAll('code')]
if str(message) in mod_list:
doc_link = base_url + message
await bot.say(doc_link)
else:
await bot.say('"{}" does not appear to be a module in the standard library.'.format(message))
return
# Run Bot
# non-heroku method of loading keys
# with open('oxford_dictionary_api.txt', 'r') as oxford_key_file:
# oxford_id = oxford_key_file.readline().rstrip()
# oxford_key = oxford_key_file.readline().rstrip()
# with open('token.txt', 'r') as token_file:
# token = token_file.readline().rstrip()
# Heroku method
token = os.environ['TOKEN']
oxford_id = os.environ['OXFORD_ID']
oxford_key = os.environ['OXFORD_KEY']
bot.run(token)