-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinyrpg_choix1.js
146 lines (135 loc) · 3.63 KB
/
tinyrpg_choix1.js
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
// authenticates you with the API standard library
// type `await lib.` to display API autocomplete
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
// console.log(context.params.event);
let last_episode = await lib.googlesheets.query['@0.3.0'].select({
range: `Tiny-RPG!J:K`,
bounds: 'FIRST_EMPTY_ROW',
where: [
{
'gamemaster': `${context.params.event.user.id}`
}
],
limit: {
'count': 0,
'offset': 0
},
});
last_episode = last_episode.rows[0].fields.active_episode;
let new_episode = await lib.googlesheets.query['@0.3.0'].select({
range: `Tiny-RPG!A:H`,
bounds: 'FIRST_EMPTY_ROW',
where: [
{
'episode': `${last_episode}`
}
],
limit: {
'count': 0,
'offset': 0
},
});
new_episode = new_episode.rows[0].fields.choix1_next;
let result = await lib.googlesheets.query['@0.3.0'].select({
range: `Tiny-RPG!A:H`,
bounds: 'FIRST_EMPTY_ROW',
where: [
{
'episode': `${new_episode}`
}
],
limit: {
'count': 0,
'offset': 0
},
});
let url = result.rows[0].fields.url;
let message = result.rows[0].fields.message;
let choix_nombre = result.rows[0].fields.choix_nombre;
const slice_limit = 1900;
//Check and split long messages
let index = 0;
let trimmedString;
let division;
for (let i = 0; i < 7; i++) {
division = Math.floor(message.length/slice_limit);
if (division > 0) {
//trim the string to the maximum length
trimmedString = message.substr(0,slice_limit);
//re-trim if we are in the middle of a word
trimmedString = trimmedString.substr(0, Math.min(trimmedString.length, trimmedString.lastIndexOf(" ")));
await lib.discord.channels['@0.3.0'].messages.create({
"channel_id": `${context.params.event.channel_id}`,
"content": `${trimmedString}`,
"tts": false
});
index = trimmedString.length;
message = message.substr(index);
division = Math.floor(message.length/slice_limit);
}
}
///////
await lib.googlesheets.query['@0.3.0'].update({
range: `Tiny-RPG!J:K`,
bounds: 'FIRST_EMPTY_ROW',
where: [
{
'gamemaster': `${context.params.event.user.id}`
}
],
fields: {
'active_episode': `${new_episode}`
},
});
if (choix_nombre == 1) {
let choix1 = result.rows[0].fields.choix1;
await lib.discord.channels['@0.3.0'].messages.create({
"channel_id": `${context.params.event.channel_id}`,
"content": `${message} \n${url} \n[#${new_episode}]`,
"tts": false,
"components": [
{
"type": 1,
"components": [
{
"style": 1,
"label": `${choix1}`,
"custom_id": `tinyrpg_choix1`,
"disabled": false,
"type": 2
}
]
}
]
});
}
else {
let choix1 = result.rows[0].fields.choix1;
let choix2 = result.rows[0].fields.choix2;
await lib.discord.channels['@0.3.0'].messages.create({
"channel_id": `${context.params.event.channel_id}`,
"content": `${message} \n${url} \n[#${new_episode}]`,
"tts": false,
"components": [
{
"type": 1,
"components": [
{
"style": 1,
"label": `${choix1}`,
"custom_id": `tinyrpg_choix1`,
"disabled": false,
"type": 2
},
{
"style": 1,
"label": `${choix2}`,
"custom_id": `tinyrpg_choix2`,
"disabled": false,
"type": 2
}
]
}
]
});
}