Skip to content

Commit

Permalink
fix(radio): where station audiences were mistakenly reset
Browse files Browse the repository at this point in the history
  • Loading branch information
vittee committed May 14, 2024
1 parent e340885 commit b343234
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 24 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/station/station.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export enum AudienceType {

export type AudienceOfGroup<A extends AudienceType, G extends string[]> = `${A}$${Join<G, '/'>}`;

export type DiscordAudienceGroupId = AudienceOfGroup<AudienceType.Discord, [string, string]>;
export type DiscordAudienceGroupId = AudienceOfGroup<AudienceType.Discord, [automatonId: string, guildId: string]>;

export type AudienceGroupId = DiscordAudienceGroupId | AudienceOfGroup<Exclude<AudienceType, AudienceType.Discord>, [string]>;

Expand Down
78 changes: 55 additions & 23 deletions packages/radio/src/discord/automaton/guild-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { GuildSpecificConfig, MedleyAutomaton } from "./automaton";
import { TrackMessageCreator } from "../trackmessage/creator/base";
import { makeCreator } from "../trackmessage/creator";
import { createCoverImageAttachment } from "../helpers/message";
import { extractSpotifyMetadata, extractSpotifyUrl, fetchSpotifyInfo, formatSpotifyField, spotifyURI } from "../helpers/spotify";
import { extractSpotifyUrl, fetchSpotifyInfo, formatSpotifyField } from "../helpers/spotify";

export type GuildStateAdapter = {
getAutomaton(): MedleyAutomaton;
Expand Down Expand Up @@ -132,7 +132,13 @@ export class GuildState {
this.#voiceConnector = undefined;
}

#updateChannelAudiences(channel: VoiceBasedChannel | null | undefined, muted: boolean) {
/**
* Just joined a voice channel, audiences for this guild should be reset to this channel's members
*/
#joinedVoiceChannel(channel: VoiceBasedChannel | null | undefined, muted: boolean) {
this.#voiceChannelId = channel?.id;
this.#serverMuted = muted;

const station = this.tunedStation;

if (!station) {
Expand All @@ -143,16 +149,15 @@ export class GuildState {

if (muted || !channel) {
station.removeAudiencesForGroup(audienceGroup);
} else {
updateStationAudiences(station, audienceGroup, channel);
return;
}
}

#joinedVoiceChannel(channel: VoiceBasedChannel | null | undefined, muted: boolean) {
this.#voiceChannelId = channel?.id;
this.#serverMuted = muted;

this.#updateChannelAudiences(channel, muted);
station.updateAudiences(
audienceGroup,
channel.members
.filter(member => !member.user.bot && !member.voice.deaf)
.map(member => member.id)
);
}

#leftVoiceChannel() {
Expand Down Expand Up @@ -352,10 +357,11 @@ export class GuildState {
const channel = this.adapter.getChannel(this.voiceChannelId);

if (channel?.type === ChannelType.GuildVoice) {
updateStationAudiences(
this.preferredStation,
this.preferredStation.updateAudiences(
this.adapter.makeAudienceGroup(this.guildId),
channel
channel.members
.filter(member => !member.user.bot && !member.voice.deaf)
.map(member => member.id)
);
}
}
Expand Down Expand Up @@ -409,7 +415,42 @@ export class GuildState {
// Stationary
if (oldState.serverMute !== newState.serverMute) {
this.#serverMuted = newState.serverMute === true;
this.#updateChannelAudiences(newState.channel, this.#serverMuted);
this.#serverMuteStateChanged(newState.channel);
}
}

#serverMuteStateChanged(channel: VoiceBasedChannel | null) {
const station = this.tunedStation;

if (!station) {
return;
}

const audienceGroup = this.adapter.makeAudienceGroup(this.guildId);

if (this.#serverMuted || !channel) {
station.removeAudiencesForGroup(audienceGroup);
return;
}

const updateAudience = !this.#serverMuted
? ((member: GuildMember) => {
if (!member.user.system && !member.user.bot && !member.voice.deaf) {
station.addAudience(audienceGroup, member.id);
}
})
: (mmeber: GuildMember) => station.removeAudience(audienceGroup, mmeber.id)

channel.members.forEach(updateAudience);

// Remove invalid audiences, any audience that does not belong to this channel
const audiences = station.getAudiences(audienceGroup);
if (audiences) {
for (const memberId of audiences) {
if (!channel.members.has(memberId)) {
station.removeAudience(audienceGroup, memberId);
}
}
}
}

Expand Down Expand Up @@ -675,15 +716,6 @@ export type StationLink = {
exciter: DiscordAudioPlayer;
}

export function updateStationAudiences(station: Station, groupId: AudienceGroupId, channel: VoiceBasedChannel) {
station.updateAudiences(
groupId,
channel.members
.filter(member => !member.user.bot && !member.voice.deaf)
.map(member => member.id)
);
}

interface VoiceStateWithMember extends VoiceState {
get member(): GuildMember;
}
Expand Down

0 comments on commit b343234

Please sign in to comment.