Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 105 additions & 3 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,56 @@ core.start = async (commander) => {
}

const {RTMClient} = require("@slack/client");
const rtm = new RTMClient(token, {logLevel: "error"});
const rtm = new RTMClient(token, {
logLevel: "error",
retryConfig: {
retries: 3,
factor: 2
}
});

// エラーログのスロットリング用変数
let lastErrorTime = 0;
let errorCount = 0;
const ERROR_THROTTLE_MS = 300000; // 5分間隔でのみエラーログを表示

// RTMClient の内部エラーをキャッチするためのグローバルハンドラ
process.on("uncaughtException", (error) => {
const now = Date.now();

// RTMClient.js からのエラーを検出
if (error && error.stack && error.stack.includes("RTMClient.js")) {
errorCount++;
if (now - lastErrorTime > ERROR_THROTTLE_MS) {
if (errorCount > 1) {
console.error(`RTM WebSocket errors occurred (${errorCount} times since last report)`);
} else {
console.error("RTM WebSocket error caught:", error.message || "Unknown RTM error");
}
lastErrorTime = now;
errorCount = 0;
}
return;
}

// "Cannot read properties of null" エラーを直接キャッチ
if (error && error.message && error.message.includes("Cannot read properties of null")) {
errorCount++;
if (now - lastErrorTime > ERROR_THROTTLE_MS) {
if (errorCount > 1) {
console.error(`Null property access errors occurred (${errorCount} times since last report)`);
} else {
console.error("Null property access error caught:", error.message);
}
lastErrorTime = now;
errorCount = 0;
}
return;
}

// その他の予期しないエラーは通常通り処理
throw error;
});

let colors = ["red", "green", "yellow", "blue", "magenta", "cyan", "white", "redBright", "greenBright", "yellowBright", "blueBright", "magentaBright", "cyanBright", "whiteBright"];

Expand Down Expand Up @@ -165,16 +214,51 @@ core.start = async (commander) => {
setInterval(collectTweets, 1000 * 60);
}


rtm.on("error", (error) => {
if (error && typeof error === "object") {
console.error("RTM connection error:", error.message || JSON.stringify(error));
} else if (error) {
console.error("RTM connection error:", String(error));
} else {
console.error("RTM connection error: Unknown error");
}
});

let isReconnecting = false;

rtm.on("disconnect", () => {
if (!isReconnecting) {
console.log("RTM connection disconnected. Attempting to reconnect...");
isReconnecting = true;
}
});

rtm.on("connecting", () => {
if (isReconnecting) {
console.log("RTM reconnecting...");
}
});

rtm.on("authenticated", (rtmStartData) => {
if (isReconnecting) {
console.log("RTM reconnected successfully");
isReconnecting = false;
}

if (!util.startUp) {
console.log(
`Logged in as ${chalk.bold(rtmStartData.self.name)} of team ${chalk.green.bold(rtmStartData.team.name)}, but not yet connected to a channel`
);

util.startUp = true;
}
});

rtm.on("unable_to_rtm_start", (error) => {
console.error("Unable to start RTM:", error.message || error);
isReconnecting = false;
});

rtm.on("message", (message) => {
let time = moment(message.ts * 1000);
let text = util.parseText(message);
Expand Down Expand Up @@ -307,16 +391,34 @@ core.start = async (commander) => {
}

response = await web.users.list();
while(response.response_metadata.next_cursor != "") {

if (response.members && Array.isArray(response.members)) {
response.members.forEach((v, i) => {
v.color = colors[i % colors.length];
// nameプロパティが存在しない場合の代替案を追加
if (!v.name) {
v.name = v.real_name || (v.profile && v.profile.display_name) || v.id;
}
util.users[v.id] = v;
});
}

while(response.response_metadata && response.response_metadata.next_cursor != "") {
response = await web.users.list({
limit: 1000,
cursor: response.response_metadata.next_cursor
});

if (response.members && Array.isArray(response.members)) {
response.members.forEach((v, i) => {
v.color = colors[i % colors.length];
// nameプロパティが存在しない場合の代替案を追加
if (!v.name) {
v.name = v.real_name || (v.profile && v.profile.display_name) || v.id;
}
util.users[v.id] = v;
});
}
}

// complete
Expand Down
Loading