-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.js
More file actions
111 lines (98 loc) · 4.61 KB
/
plugin.js
File metadata and controls
111 lines (98 loc) · 4.61 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
class Plugin {
constructor(workspace) {
this.workspace = workspace;
this.catName = 'BOTステータス';
}
async onload() {
this.registerBlocks();
console.log("CheckStatus Plugin loaded!");
}
async onunload() {
this.unregisterBlocks();
console.log("CheckStatus Plugin unloaded.");
}
registerBlocks() {
if (typeof Blockly === 'undefined') return;
const blocksInfo = [
{ id: 'status_cpu_usage', name: '💻 CPU使用率 (%)', py: 'psutil.cpu_percent(interval=0.1)' },
{ id: 'status_mem_used', name: '🧠 RAM使用量 (GiB)', py: 'psutil.virtual_memory().used / (1024 ** 3)' },
{ id: 'status_mem_total', name: '💾 RAM合計 (GiB)', py: 'psutil.virtual_memory().total / (1024 ** 3)' },
{ id: 'status_mem_percent', name: '📊 RAM使用率 (%)', py: 'psutil.virtual_memory().percent' },
{ id: 'status_ping', name: '📡 Ping (ms)', py: 'bot.latency * 1000' },
{ id: 'status_guild_count', name: '🏠 サーバー数', py: 'len(bot.guilds)' },
{ id: 'status_command_count', name: '🛠️ コマンド数', py: 'len(bot.tree.get_commands(guild=discord.Object(id=GUILD_ID)))' },
{ id: 'status_shard_count', name: '💎 Shard数', py: 'bot.shard_count or 1' },
{ id: 'status_uptime_current', name: '⏱️ 現稼働時間', py: 'str(datetime.now(timezone.utc) - getattr(bot, "start_time", datetime.now(timezone.utc))).split(".")[0]' }
];
blocksInfo.forEach(info => {
// 1. ブロック定義
Blockly.Blocks[info.id] = {
init: function () {
this.appendDummyInput().appendField(info.name);
this.setOutput(true, null);
this.setColour(230);
this.setTooltip(info.name + 'を取得します。');
}
};
// 2. Pythonジェネレータ登録
const generator = (block) => {
// 自動インポートの確実な登録
if (Blockly.Python) {
if (!Blockly.Python.definitions_) Blockly.Python.definitions_ = {};
Blockly.Python.definitions_['import_psutil'] = 'import psutil';
Blockly.Python.definitions_['import_discord'] = 'import discord';
Blockly.Python.definitions_['from_datetime_import_datetime_timezone'] = 'from datetime import datetime, timezone';
}
const order = (Blockly.Python && (Blockly.Python.ORDER_ATOMIC || Blockly.Python.ORDER_NONE)) || 0;
return [info.py, order];
};
// EDBB環境に合わせて複数の登録方法を試行
if (Blockly.Python) {
if (Blockly.Python.forBlock) {
Blockly.Python.forBlock[info.id] = generator;
}
// フォールバック
Blockly.Python[info.id] = generator;
}
});
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', '#4CAF50');
toolbox.appendChild(category);
}
category.innerHTML = `
<block type="status_cpu_usage"></block>
<block type="status_mem_used"></block>
<block type="status_mem_total"></block>
<block type="status_mem_percent"></block>
<block type="status_ping"></block>
<block type="status_guild_count"></block>
<block type="status_command_count"></block>
<block type="status_shard_count"></block>
<block type="status_uptime_current"></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);
}
}
}
}
}