Skip to content

Commit

Permalink
十分な量のBotがいる場合、VCとBotが一対一対応するように変更 (#542)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mogyuchi authored May 20, 2023
1 parent 6bf08b9 commit 3acd9c0
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions src/guildCtx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
type Guild,
VoiceChannel,
type GuildTextBasedChannel,
ChannelType,
} from 'discord.js'
import { WorkerClientMap } from './worker.js'
import { workerClientMap } from './index.js'
Expand All @@ -12,26 +13,51 @@ const getBots = async (guild: Guild, worker: WorkerClientMap) => {
const results = await Promise.allSettled(
Array.from(worker.keys()).map((id) => guild.members.fetch(id)),
)
return results.flatMap((r) => (r.status === 'fulfilled' ? r.value.id : []))
return results
.flatMap((r) => (r.status === 'fulfilled' ? r.value.id : []))
.sort((a, b) => Number(BigInt(a) - BigInt(b)))
}

class GuildContext {
guild: Guild
bots: Promise<string[]>
standbyBots: Promise<string[]>
connectionManager: ConnectionCtxManager

constructor(guild: Guild, worker: WorkerClientMap) {
this.guild = guild
this.bots = getBots(guild, worker)
this.standbyBots = this.bots
this.connectionManager = new ConnectionCtxManager()
}

async join(voiceChannel: VoiceChannel, readChannel: GuildTextBasedChannel) {
const workerId = (await this.standbyBots).sort((a, b) =>
Number(BigInt(a) - BigInt(b)),
)[0]
const vcArray = (await voiceChannel.guild.channels.fetch())
.map((v) => {
if (v?.type === ChannelType.GuildVoice && v.joinable) {
return v
} else {
return null
}
})
.flatMap((data) => {
return data ?? []
})
.sort((a, b) => a.rawPosition - b.rawPosition)

let workerId = (await this.bots)[vcArray.indexOf(voiceChannel)]

if (
workerId === undefined ||
[...this.connectionManager.values()].find(
(v) => v.connection.joinConfig.group === workerId,
)
) {
workerId = (await this.bots).find(
(botId) =>
![...this.connectionManager.values()]
.map((connectionCtx) => connectionCtx.connection.joinConfig.group)
.includes(botId),
)
}
if (workerId === undefined) {
throw Error('No worker')
}
Expand All @@ -43,19 +69,16 @@ class GuildContext {
readChannel,
worker,
)
;(await this.standbyBots).shift()
} catch {}
return worker
}
async leave(voiceChannel: VoiceChannel) {
if (!this.connectionManager.channelMap.has(voiceChannel)) throw Error()
const workerId = this.connectionManager.connectionLeave(voiceChannel)
;(await this.standbyBots).push(workerId)
return workerId
}
async addBot(workerId: string) {
;(await this.bots).push(workerId)
;(await this.standbyBots).push(workerId)
}
async resetBots(workerClientMap: WorkerClientMap) {
for (const voiceChannel of this.connectionManager.channelMap.keys()) {
Expand All @@ -64,7 +87,6 @@ class GuildContext {
} catch {}
}
this.bots = getBots(this.guild, workerClientMap)
this.standbyBots = this.bots
}
}

Expand Down

0 comments on commit 3acd9c0

Please sign in to comment.