Skip to content

Commit

Permalink
可能な場合、別のVCで使われているBotを入れ替えてでも同じBotを使うように修正 (#544)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mogyuchi authored May 21, 2023
1 parent 91092c5 commit b7955fe
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 18 deletions.
5 changes: 2 additions & 3 deletions src/connectionCtx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,19 @@ import {
Message,
ChatInputCommandInteraction,
type DiscordErrorData,
type TextBasedChannel,
type GuildTextBasedChannel,
} from 'discord.js'
import { Queue } from './utils.js'
import { fetchAudioStream } from './voiceRead.js'
import { getUserSetting } from './db.js'

class ConnectionContext {
readChannel: TextBasedChannel
readChannel: GuildTextBasedChannel
readQueue: any
player: AudioPlayer | null = null
connection: VoiceConnection

constructor(readChannel: TextBasedChannel, connection: VoiceConnection) {
constructor(readChannel: GuildTextBasedChannel, connection: VoiceConnection) {
this.readChannel = readChannel
this.readQueue = new Queue(this._readMessage.bind(this))
this.connection = connection
Expand Down
40 changes: 32 additions & 8 deletions src/guildCtx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
ChannelType,
} from 'discord.js'
import { WorkerClientMap } from './worker.js'
import { workerClientMap } from './index.js'
import { client, workerClientMap } from './index.js'
import { ConnectionCtxManager } from './connectionCtx.js'

const getBots = async (guild: Guild, worker: WorkerClientMap) => {
Expand Down Expand Up @@ -45,18 +45,42 @@ class GuildContext {

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

if (
workerId === undefined ||
[...this.connectionManager.values()].find(
(v) => v.connection.joinConfig.group === workerId,
)
) {
if (workerId === undefined) {
workerId = (await this.bots).find(
(botId) =>
![...this.connectionManager.values()]
.map((connectionCtx) => connectionCtx.connection.joinConfig.group)
.includes(botId),
)
} else {
const oldConnectionCtx = [...this.connectionManager.values()].find(
(v) => v.connection.joinConfig.group === workerId,
)
if (oldConnectionCtx !== undefined) {
const oldVoiceChannel = await client.channels
.fetch(oldConnectionCtx.connection.joinConfig.channelId!)
.then((channels) => {
return channels?.type === ChannelType.GuildVoice ? channels : null
})
const forOldCtxWorkerId = (await this.bots).find(
(botId) =>
![...this.connectionManager.values()]
.map((connectionCtx) => connectionCtx.connection.joinConfig.group)
.includes(botId) && botId !== workerId,
)
if (forOldCtxWorkerId === undefined) {
throw Error('No worker')
}
this.leave(oldVoiceChannel!)
try {
this.connectionManager.connectionJoin(
oldVoiceChannel!,
this.guild.id,
oldConnectionCtx.readChannel,
workerClientMap.get(forOldCtxWorkerId)!,
)
} catch {}
}
}
if (workerId === undefined) {
throw Error('No worker')
Expand All @@ -72,7 +96,7 @@ class GuildContext {
} catch {}
return worker
}
async leave(voiceChannel: VoiceChannel) {
leave(voiceChannel: VoiceChannel) {
if (!this.connectionManager.channelMap.has(voiceChannel)) throw Error()
const workerId = this.connectionManager.connectionLeave(voiceChannel)
return workerId
Expand Down
27 changes: 20 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { SignalConstants } from 'os'
import { getUserSetting } from './db.js'
import { WorkerClientMap } from './worker.js'
import { joinMessageRun } from './joinMessage.js'
import { ChannelType } from 'discord.js'
const debug__ErrorHandler = debug('index.js:ErrorHandler')

let isCalledDestroy = false
Expand Down Expand Up @@ -84,17 +85,29 @@ client.on('messageCreate', async (message: typings.Message) => {
connectionManager.get(message.channel)!.addMessage(convertedMessage, message)
})

client.on('voiceStateUpdate', (oldState, newState) => {
client.on('voiceStateUpdate', async (oldState, newState) => {
if (oldState.channel?.type !== ChannelType.GuildVoice) return
const guildCtx = guildCtxManager.get(newState.guild)

if (
(newState.channelId == null &&
newState.id === client.user?.id &&
oldState.channel instanceof VoiceChannel &&
guildCtx.connectionManager.channelMap.has(oldState.channel)) ||
(oldState.channel instanceof VoiceChannel &&
guildCtx.connectionManager.channelMap.has(oldState.channel) &&
oldState.channel.members.size === 1)
(await guildCtx.bots).includes(newState.id) &&
[...guildCtx.connectionManager.values()].find(
(connectionCtx) =>
connectionCtx.connection.joinConfig.channelId ===
oldState.channelId &&
connectionCtx.connection.joinConfig.group === oldState.id,
)) ||
(guildCtx.connectionManager.channelMap.has(oldState.channel) &&
oldState.channel.members.size === 1 &&
oldState.channel.members.every((member) =>
[...guildCtx.connectionManager.values()].find(
(connectionCtx) =>
connectionCtx.connection.joinConfig.channelId ===
oldState.channelId &&
connectionCtx.connection.joinConfig.group === member.id,
),
))
) {
guildCtx.connectionManager.channelMap
.get(oldState.channel)!
Expand Down

0 comments on commit b7955fe

Please sign in to comment.