-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.js
More file actions
396 lines (357 loc) · 16.1 KB
/
plugin.js
File metadata and controls
396 lines (357 loc) · 16.1 KB
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
class Plugin {
constructor(workspace) {
this.workspace = workspace;
this.catName = 'ボイスチャットマネージャー';
}
async onload() {
this.registerBlocks();
console.log("VCManager Plugin loaded!");
}
async onunload() {
this.unregisterBlocks();
console.log("VCManager Plugin unloaded.");
}
registerBlocks() {
if (typeof Blockly === 'undefined') return;
const idValidator = function (newValue) {
return newValue.replace(/[^0-9]/g, '');
};
// 1. VC全員移動
Blockly.Blocks['vc_move_all'] = {
init: function () {
this.appendDummyInput()
.appendField("🔊 チャンネル")
.appendField(new Blockly.FieldTextInput("0", idValidator), "FROM_ID")
.appendField("の全員を")
.appendField(new Blockly.FieldTextInput("0", idValidator), "TO_ID")
.appendField("へ移動");
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setColour(160);
this.setTooltip("指定したチャンネルにいる全員を別のチャンネルへ一括移動させます。");
}
};
// 2. VC全員切断
Blockly.Blocks['vc_disconnect_all'] = {
init: function () {
this.appendDummyInput()
.appendField("🔊 チャンネル")
.appendField(new Blockly.FieldTextInput("0", idValidator), "CHANNEL_ID")
.appendField("内の全員を切断させる");
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setColour(160);
this.setTooltip("指定したチャンネルにいる全員をボイスチャンネルから切断させます。");
}
};
// 3. 特定ユーザー切断
Blockly.Blocks['vc_disconnect_member'] = {
init: function () {
this.appendValueInput("USER")
.setCheck(null)
.appendField("👤 ");
this.appendDummyInput()
.appendField("をボイスチャンネルから切断させる");
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setColour(160);
this.setTooltip("指定したユーザーをボイスチャンネルから切断させます。IDまたはメンバーオブジェクトを指定してください。");
}
};
// 4. 人数制限操作
Blockly.Blocks['vc_set_user_limit'] = {
init: function () {
this.appendDummyInput()
.appendField("📏 VC")
.appendField(new Blockly.FieldTextInput("0", idValidator), "CHANNEL_ID")
.appendField("の接続可能人数を");
this.appendDummyInput()
.appendField(new Blockly.FieldNumber(0, 0, 99), "NUM") // 0-99なのでFieldNumberでOK
.appendField(new Blockly.FieldDropdown([
["人増やす", "ADD"],
["人減らす", "SUB"],
["人にセットする", "SET"]
]), "MODE");
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setColour(160);
this.setTooltip("ボイスチャンネルの人数制限を動的に変更します。");
}
};
// 4.5 人数制限解除
Blockly.Blocks['vc_reset_user_limit'] = {
init: function () {
this.appendDummyInput()
.appendField("📏 VC")
.appendField(new Blockly.FieldTextInput("0", idValidator), "CHANNEL_ID")
.appendField("の人数制限を解除する(無限)");
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setColour(160);
this.setTooltip("ボイスチャンネルの人数制限を解除して無限にします。");
}
};
// 5. タイムアウト (期間指定)
Blockly.Blocks['vc_timeout_member'] = {
init: function () {
this.appendValueInput("USER")
.setCheck(null)
.appendField("👤 ");
this.appendDummyInput()
.appendField("をタイムアウトさせる");
this.appendDummyInput()
.appendField("期間:")
.appendField(new Blockly.FieldTextInput("30s, 5m, 1h, 1d等"), "DURATION");
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setColour(20);
this.setTooltip("ユーザーを指定期間タイムアウト(Communication Disabled)にします。最大30日間まで。");
}
};
// 6. 権限設定 (表示/接続/発言)
Blockly.Blocks['vc_set_permission'] = {
init: function () {
this.appendDummyInput()
.appendField("🔐 チャンネル")
.appendField(new Blockly.FieldTextInput("0", idValidator), "CHANNEL_ID")
.appendField("の対象")
.appendField(new Blockly.FieldTextInput("0", idValidator), "TARGET_ID");
this.appendDummyInput()
.appendField("👁️表示:")
.appendField(new Blockly.FieldDropdown([["変更なし", "NONE"], ["許可", "ALLOW"], ["拒否", "DENY"]]), "VIEW")
.appendField(" 🔌接続:")
.appendField(new Blockly.FieldDropdown([["変更なし", "NONE"], ["許可", "ALLOW"], ["拒否", "DENY"]]), "CONNECT")
.appendField(" 💬発言:")
.appendField(new Blockly.FieldDropdown([["変更なし", "NONE"], ["許可", "ALLOW"], ["拒否", "DENY"]]), "SPEAK");
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setColour(20);
this.setTooltip("ボイスチャンネルの「表示・接続・発言」権限を個別に切り替えます。");
}
};
// 7. 名前変更
Blockly.Blocks['vc_set_name'] = {
init: function () {
this.appendValueInput("NAME")
.setCheck("String")
.appendField("📝 VC")
.appendField(new Blockly.FieldTextInput("0", idValidator), "CHANNEL_ID")
.appendField("の名前を");
this.appendDummyInput()
.appendField("に変える");
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setColour(160);
this.setTooltip("ボイスチャンネルの名前を動的に変更します。");
}
};
// 8. ステータス変更
Blockly.Blocks['vc_set_status'] = {
init: function () {
this.appendValueInput("STATUS")
.setCheck("String")
.appendField("💬 VC")
.appendField(new Blockly.FieldTextInput("0", idValidator), "CHANNEL_ID")
.appendField("のステータスを");
this.appendDummyInput()
.appendField("に変える");
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setColour(160);
this.setTooltip("ボイスチャンネルのステータス(ボイス状態メッセージ)を変更します。");
}
};
// 9. 所属VC取得
Blockly.Blocks['vc_get_user_vc'] = {
init: function () {
this.appendValueInput("USER")
.setCheck(null)
.appendField("🔍 ");
this.appendDummyInput()
.appendField("がどのVCにいるか取得する");
this.setOutput(true, null);
this.setColour(160);
this.setTooltip("ユーザーが現在接続しているボイスチャンネルを返します。どこにもいない場合はNoneを返します。");
}
};
// 10. 招待リンク発行 (30分)
Blockly.Blocks['vc_create_invite_30m'] = {
init: function () {
this.appendDummyInput()
.appendField("🔗 VC")
.appendField(new Blockly.FieldNumber(0, 0), "CHANNEL_ID")
.appendField("への一時的なリンク(30分)を発行する");
this.setOutput(true, "String");
this.setColour(160);
this.setTooltip("指定したボイスチャンネルへの30分間有効な招待リンクを発行し、URLを返します。");
}
};
const registerGenerator = (id, fn) => {
if (Blockly.Python) {
if (Blockly.Python.forBlock) {
Blockly.Python.forBlock[id] = fn;
}
Blockly.Python[id] = fn;
}
};
// ジェネレータ実装
registerGenerator('vc_move_all', (block) => {
const fromId = block.getFieldValue('FROM_ID');
const toId = block.getFieldValue('TO_ID');
return `
channel_from = bot.get_channel(int(${fromId}))
channel_to = bot.get_channel(int(${toId}))
if channel_from and channel_to:
for member in channel_from.members:
await member.move_to(channel_to)
`;
});
registerGenerator('vc_disconnect_all', (block) => {
const channelId = block.getFieldValue('CHANNEL_ID');
return `
channel = bot.get_channel(int(${channelId}))
if channel:
for member in channel.members:
await member.move_to(None)
`;
});
registerGenerator('vc_disconnect_member', (block) => {
const user = Blockly.Python.valueToCode(block, 'USER', 0) || 'None';
return `
target = ${user}
member = guild.get_member(int(target)) if isinstance(target, (int, str)) else target
if member and hasattr(member, "move_to"):
await member.move_to(None)
`;
});
registerGenerator('vc_set_user_limit', (block) => {
const channelId = block.getFieldValue('CHANNEL_ID');
const num = block.getFieldValue('NUM');
const mode = block.getFieldValue('MODE');
let calc = `int(${num})`;
if (mode === 'ADD') calc = `channel.user_limit + int(${num})`;
else if (mode === 'SUB') calc = `max(0, channel.user_limit - int(${num}))`;
return `
channel = bot.get_channel(int(${channelId}))
if channel:
await channel.edit(user_limit=${calc})
`;
});
registerGenerator('vc_reset_user_limit', (block) => {
const channelId = block.getFieldValue('CHANNEL_ID');
return `
channel = bot.get_channel(int(${channelId}))
if channel:
await channel.edit(user_limit=0)
`;
});
registerGenerator('vc_timeout_member', (block) => {
const user = Blockly.Python.valueToCode(block, 'USER', 0) || 'None';
const duration = block.getFieldValue('DURATION');
if (Blockly.Python) {
Blockly.Python.definitions_['import_re'] = 'import re';
Blockly.Python.definitions_['from_datetime_import_timedelta'] = 'from datetime import timedelta';
}
return `
target = ${user}
member = guild.get_member(int(target)) if isinstance(target, (int, str)) else target
dur_str = "${duration}"
if member:
times = {"d": 0, "h": 0, "m": 0, "s": 0}
matches = re.findall(r"(\\d+)([dhms])", dur_str)
for v, k in matches: times[k] = int(v)
td = timedelta(days=times["d"], hours=times["h"], minutes=times["m"], seconds=times["s"])
if td.total_seconds() > 0:
await member.timeout(td)
`;
});
registerGenerator('vc_set_permission', (block) => {
const channelId = block.getFieldValue('CHANNEL_ID');
const targetId = block.getFieldValue('TARGET_ID');
const view = block.getFieldValue('VIEW');
const connect = block.getFieldValue('CONNECT');
const speak = block.getFieldValue('SPEAK');
const mapPerm = (val) => val === 'ALLOW' ? 'True' : (val === 'DENY' ? 'False' : 'None');
return `
t_id = int(${targetId})
channel = bot.get_channel(int(${channelId}))
target = guild.get_role(t_id) or guild.get_member(t_id)
if channel and target:
overwrite = channel.overwrites_for(target)
${view !== 'NONE' ? `overwrite.view_channel = ${mapPerm(view)}` : ''}
${connect !== 'NONE' ? `overwrite.connect = ${mapPerm(connect)}` : ''}
${speak !== 'NONE' ? `overwrite.speak = ${mapPerm(speak)}` : ''}
await channel.set_permissions(target, overwrite=overwrite)
`;
});
registerGenerator('vc_set_name', (block) => {
const channelId = block.getFieldValue('CHANNEL_ID');
const name = Blockly.Python.valueToCode(block, 'NAME', 0) || '""';
return `
channel = bot.get_channel(int(${channelId}))
if channel:
await channel.edit(name=${name})
`;
});
registerGenerator('vc_set_status', (block) => {
const channelId = block.getFieldValue('CHANNEL_ID');
const status = Blockly.Python.valueToCode(block, 'STATUS', 0) || 'None';
return `
channel = bot.get_channel(int(${channelId}))
if channel and hasattr(channel, "edit"):
await channel.edit(status=${status})
`;
});
registerGenerator('vc_get_user_vc', (block) => {
const user = Blockly.Python.valueToCode(block, 'USER', 0) || 'None';
const code = `(${user}.voice.channel if hasattr(${user}, "voice") and ${user}.voice else None) if ${user} else None`;
return [code, 0];
});
registerGenerator('vc_create_invite_30m', (block) => {
const channelId = block.getFieldValue('CHANNEL_ID');
const code = `(await bot.get_channel(int(${channelId})).create_invite(max_age=1800)).url if bot.get_channel(int(${channelId})) else ""`;
return [code, 0];
});
this.updateToolbox();
}
updateToolbox() {
const toolbox = document.getElementById('toolbox');
if (!toolbox) return;
let category = toolbox.querySelector(`category[name="${this.catName}"]`);
if (!category) {
category = document.createElement('category');
category.setAttribute('name', this.catName);
category.setAttribute('data-icon', '🔊');
category.setAttribute('colour', '#607D8B');
toolbox.appendChild(category);
}
category.innerHTML = `
<block type="vc_move_all"></block>
<block type="vc_disconnect_all"></block>
<block type="vc_disconnect_member"></block>
<block type="vc_set_user_limit"></block>
<block type="vc_reset_user_limit"></block>
<block type="vc_set_name"></block>
<block type="vc_set_status"></block>
<block type="vc_get_user_vc"></block>
<block type="vc_create_invite_30m"></block>
<block type="vc_timeout_member"></block>
<block type="vc_set_permission"></block>
`;
if (this.workspace && this.workspace.updateToolbox) {
this.workspace.updateToolbox(toolbox);
}
}
unregisterBlocks() {
const toolbox = document.getElementById('toolbox');
if (toolbox) {
const category = toolbox.querySelector(`category[name="${this.catName}"]`);
if (category) {
category.remove();
if (this.workspace && this.workspace.updateToolbox) {
this.workspace.updateToolbox(toolbox);
}
}
}
}
}