-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel_is_test.js
122 lines (107 loc) · 2.95 KB
/
channel_is_test.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
// authenticates you with the API standard library
// type `await lib.` to display API autocomplete
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
// Get channel id and topic info
const channel_id = context.params.event.channel_id;
const topic = await lib.discord.channels['@0.3.0'].retrieve({
channel_id: channel_id,
});
let channel_name = topic.name;
// Useful functions
async function GetSheetData(range) {
let sheet_data = await lib.googlesheets.query['@0.3.0'].select({
range: range,
bounds: 'FIRST_EMPTY_ROW',
where: [{}],
limit: {
count: 0,
offset: 0,
},
});
fieldsets = [];
for (row of sheet_data.rows) {
fieldsets = fieldsets.concat(row.fields);
}
return fieldsets;
}
async function CleanSheet(range) {
await lib.googlesheets.query['@0.3.0'].delete({
range: range,
bounds: 'FIRST_EMPTY_ROW',
where: [{}],
limit: {
count: 0,
offset: 0,
},
});
}
async function InsertInSheet(range, fieldsets) {
await lib.googlesheets.query['@0.3.0'].insert({
range: range,
fieldsets: fieldsets,
});
}
async function SendMessage(type) {
let content;
if (type == 'NoDeal') {
content = "Tu n'es pas autorisé à me donner cet ordre.";
} else if (type == 'Start') {
content = 'Mise à jour en cours.';
} else if (type == 'Finish') {
content = 'Ca y est, ce canal est considéré comme un test !';
}
await lib.discord.channels['@0.3.0'].messages.create({
channel_id: channel_id,
content: content,
});
}
// Check if user can perform this action
eligible_roles = [
'936641846719967263', // Modération on server: Yaourt's bot
'902501987415900170', // Modération on server: Silence on joue !
'902502594998595585', // Chroniqueureuses on server: Silence on joue !
];
user_roles = context.params.event.member.roles;
not_allowed = true;
if (user_roles.length > 0) {
for (role of user_roles) {
if (eligible_roles.includes(role)) {
not_allowed = false;
}
}
}
if (not_allowed) {
await SendMessage('NoDeal');
return 0;
}
// Let's begin!
await SendMessage('Start');
// Range names
channel_range = 'channel';
// Get channel table and check if channel is test
channel_array = await GetSheetData(channel_range);
if (
channel_array.length == 0 ||
!channel_array.some((channel) => channel.channel_id == channel_id)
) {
channel_array.push({
channel_id: channel_id,
channel_name: channel_name,
is_test: 1,
test_last_message_id: 0,
});
}
channel_array = channel_array.map((channel) => {
if (channel.channel_id == channel_id) {
return {...channel, is_test: 1};
}
return channel;
});
// Clear all sheets on Google sheet
await CleanSheet(channel_range);
// Insert new data
await InsertInSheet(channel_range, channel_array);
// End of process message
await SendMessage('Finish');
// Success code
return 1;