Skip to content

Commit

Permalink
fix: consider the case where a signal is sent twice
Browse files Browse the repository at this point in the history
  • Loading branch information
yuimarudev committed Dec 1, 2024
1 parent fc0f453 commit 158c435
Showing 1 changed file with 34 additions and 27 deletions.
61 changes: 34 additions & 27 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Client, GatewayIntentBits } from "@discordjs/core";
import { REST } from "@discordjs/rest";
import { WebSocketManager } from "@discordjs/ws";
import { PrismaClient } from "@prisma/client";
import { Mutex } from "async-mutex";
import "dotenv/config";
import handlers from "./handlers/index.js";
import { roomManager } from "./voice/room.js";
Expand All @@ -23,35 +24,41 @@ const gateway = new WebSocketManager({
});
const client = new Client({ rest, gateway });
const prisma = new PrismaClient();
const handleExitLock = new Mutex();
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,
const release = await handleExitLock.acquire();
try {
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,
},
],
});
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,
},
});
}),
);
update: {
textChannelId: room.textChannelId,
voiceChannelId: room.voiceChannelId,
},
where: {
guildId: room.guildId,
},
});
}),
);
} finally {
release();
}

process.exit(0);
};
Expand Down

0 comments on commit 158c435

Please sign in to comment.