Skip to content

Commit

Permalink
feat: reconnect when process is restarted (#54)
Browse files Browse the repository at this point in the history
* 多分これで動くんじゃないんですか〜?

* upsert の方が嬉しい気がするんだよな

* この世の全ての例外の終着点で handleExit したら嬉しいんじゃないか?

* 本当はこんな map の使い方したくなくてえ(涙)

* EOF の前に改行入ってなくて苦しい
  • Loading branch information
yuimarudev authored Nov 30, 2024
1 parent 39ff80c commit 374fc54
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 33 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
"lefthook": "^1.8.4",
"libsodium-wrappers": "^0.7.15",
"prism-media": "1.3.5",
"prisma": "^5.22.0",
"prisma": "^6.0.0",
"typescript": "^5.6.3"
},
"dependencies": {
"@prisma/client": "5.9.1"
"@prisma/client": "6.0.0"
},
"volta": {
"node": "21.6.2"
Expand Down
60 changes: 30 additions & 30 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions prisma/migrations/20241130131415_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- CreateTable
CREATE TABLE "Connections" (
"guildId" TEXT NOT NULL PRIMARY KEY,
"voiceChannelId" TEXT NOT NULL,
"textChannelId" TEXT NOT NULL
);
6 changes: 6 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ model Dictionary {
@@unique([guildId, word])
}

model Connections {
guildId String @id
voiceChannelId String
textChannelId String
}
31 changes: 30 additions & 1 deletion src/handlers/ready.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,40 @@ import manifest from "../../.github/release-please/.release-please-manifest.json
type: "json",
};
import { initCommands } from "../commands/init.js";
import { client } from "../index.js";
import { client, gateway } from "../index.js";
import { prisma } from "../index.js";
import Room from "../voice/room.js";

export default async ({ api }: ToEventProps<GatewayReadyDispatchData>) => {
await initCommands(api);

const connections = await prisma.connections.findMany();

await Promise.all(
connections.map(async (connection) => {
await new Room(
gateway,
api,
connection.voiceChannelId,
connection.textChannelId,
connection.guildId,
)
.connect()
.catch(console.error);
await api.channels.createMessage(connection.textChannelId, {
embeds: [
{
description: "接続しました(再起動が終了しました)",
color: 0x00ff00,
},
],
});
await prisma.connections.delete({
where: { guildId: connection.guildId },
});
}),
);

console.log("ready!");

client.updatePresence(0, {
Expand Down
41 changes: 41 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { WebSocketManager } from "@discordjs/ws";
import { PrismaClient } from "@prisma/client";
import "dotenv/config";
import handlers from "./handlers/index.js";
import { roomManager } from "./voice/room.js";

if (!process.env["token"]) {
process.exit(1);
Expand All @@ -22,6 +23,38 @@ const gateway = new WebSocketManager({
});
const client = new Client({ rest, gateway });
const prisma = new PrismaClient();
const handleExit = async () => {
await Promise.all(
[...roomManager.values()].map(async (room) => {
await room.destroy();
await room.api.channels.createMessage(room.textChannelId, {
embeds: [
{
description:
"Bot が再起動されるためボイスチャンネルから切断しました.再起動後に再接続されます.",
color: 0xff0000,
},
],
});
await prisma.connections.upsert({
create: {
guildId: room.guildId,
textChannelId: room.textChannelId,
voiceChannelId: room.voiceChannelId,
},
update: {
textChannelId: room.textChannelId,
voiceChannelId: room.voiceChannelId,
},
where: {
guildId: room.guildId,
},
});
}),
);

process.exit(0);
};

for (const [event, fn] of Object.entries(handlers)) {
client.on(
Expand All @@ -33,4 +66,12 @@ for (const [event, fn] of Object.entries(handlers)) {
await gateway.connect();
await prisma.$connect();

process.on("SIGINT", handleExit);
process.on("SIGTERM", handleExit);
process.on("uncaughtException", async (e) => {
await handleExit();
console.error("uncaught: %o", e);
process.exit(1);
});

export { gateway, prisma, client };

0 comments on commit 374fc54

Please sign in to comment.