· 5 years ago · Oct 07, 2020, 11:30 PM
1import pickle
2import os.path
3from googleapiclient.discovery import build
4from google_auth_oauthlib.flow import InstalledAppFlow
5from google.auth.transport.requests import Request
6import json
7
8from evennia import Command as BaseCommand
9from evennia import default_cmds
10from evennia import Command
11from evennia.utils import logger
12from evennia import InterruptCommand
13from evennia.utils.evmenu import EvMenu
14from evennia import utils
15import random
16from evennia.utils import wrap
17from evennia.utils import pad
18
19class CmdQuestMaster(Command):
20
21 key = 'questmaster'
22 locks = 'call:perm(Admin)'
23 aliases = ("qm", "questm")
24
25 def func(self):
26 caller = self.caller
27 text = ''
28
29 # If modifying these scopes, delete the file token.pickle.
30 SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
31
32 # The ID and range of spreadsheet.
33 SAMPLE_SPREADSHEET_ID = '1Bt5UG1qZjU4OOjtIhTJ0tTzrYq-yomijtl2bw6LfYGw'
34 SAMPLE_RANGE_NAME = 'Quests 1-50!A2:I50'
35
36 creds = None
37 # The file token.pickle stores the user's access and refresh tokens, and is
38 # created automatically when the authorization flow completes for the first
39 # time.
40 if os.path.exists('token.pickle'):
41 with open('token.pickle', 'rb') as token:
42 creds = pickle.load(token)
43 # If there are no (valid) credentials available, let the user log in.
44 if not creds or not creds.valid:
45 if creds and creds.expired and creds.refresh_token:
46 creds.refresh(Request())
47 else:
48 flow = InstalledAppFlow.from_client_secrets_file("credentials.json", SCOPES)
49 creds = flow.run_local_server(port=0)
50 # Save the credentials for the next run
51 with open('token.pickle', 'wb') as token:
52 pickle.dump(creds, token)
53
54 service = build('sheets', 'v4', credentials=creds)
55
56 # Call the Sheets API
57 sheet = service.spreadsheets()
58 result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID,
59 range=SAMPLE_RANGE_NAME).execute()
60 values = result.get('values', [])
61
62 if not values:
63 text += ('No data found.')
64 else:
65 text += ('Quests 1-50:\nID - NAME - DESCRIPTION - START NPC - END NPC - TYPE - REQUIREMENT - REQ. DONE - FOLLOW UP')
66 text += ("--------------------------------------------------------------------------------------------------------")
67 for row in values:
68 # Print columns A and E, which correspond to indices 0 and 4.
69 #print('(%s) %s : %s | %s | %s' % (row[0], row[1], row[2], row[3], row[4]))
70 if len(row[2]) > 25:
71 desc = row[2]
72 desc = desc[0:25]
73 desc = desc + " ..."
74 else:
75 desc = row[2]
76 text += (f"({row[0]}) |w{row[1]}|n : {desc} | {row[3]} | {row[4]} | {row[5]} | {row[6]} | {row[7]} | {row[8]}")
77
78 caller.msg(text)