create custom channel discord implementation
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import type { VoiceChannelsServiceInterface } from "@avocadi/bot-core/entities/channels/voice/voice-channels.service";
|
||||
import { config } from "config";
|
||||
import { ChannelType, type VoiceChannel } from "discord.js";
|
||||
import { ChannelType, type User, type VoiceChannel } from "discord.js";
|
||||
import client from "lib/client";
|
||||
import { logger } from "lib/common-logger";
|
||||
|
||||
@@ -35,13 +35,21 @@ export class VoiceChannelsService
|
||||
}
|
||||
|
||||
async cloneVoiceChannel(
|
||||
user_id: string,
|
||||
channel: VoiceChannel,
|
||||
options?: { name?: string; position?: number },
|
||||
) {
|
||||
const clonedChannel = await channel.clone({
|
||||
name: options?.name,
|
||||
position: options?.position,
|
||||
permissionOverwrites: [
|
||||
{
|
||||
id: user_id,
|
||||
allow: ["ManageChannels"],
|
||||
},
|
||||
],
|
||||
});
|
||||
logger.info("DONE");
|
||||
return clonedChannel;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,9 +17,17 @@ export const handleDynamicVoiceChannelCreation = async (
|
||||
return;
|
||||
}
|
||||
|
||||
const member = newState.member;
|
||||
if (!member) {
|
||||
logger.error("Member not found for VoiceStateUpdate event");
|
||||
return;
|
||||
}
|
||||
// New channel is created if user joined one of the specific channels (e.g. "Join to Create") or switched to one of them
|
||||
const newChannel =
|
||||
await dynamicVoiceChannelService.createDynamicVoiceChannel(channel.id);
|
||||
await dynamicVoiceChannelService.createDynamicVoiceChannel(
|
||||
member.id,
|
||||
channel.id,
|
||||
);
|
||||
logger.debug(`Channel found for VoiceStateUpdate event: ${channel.name}`);
|
||||
if (newChannel) {
|
||||
await newState.setChannel(newChannel);
|
||||
|
||||
@@ -7,10 +7,7 @@ import { logger } from "lib/common-logger";
|
||||
|
||||
client.on(Events.GuildMemberAdd, async (member: GuildMember) => {
|
||||
logger.info(`${member.user.username} joined the server}`);
|
||||
if (member.user.bot) {
|
||||
// Don't send a welcome message for bots (sorry tom)
|
||||
return;
|
||||
}
|
||||
if (member.user.bot) return;
|
||||
|
||||
const channel = await client.channels.fetch(
|
||||
process.env.NODE_ENV === "production"
|
||||
|
||||
@@ -3,3 +3,4 @@ import "./stop.listener";
|
||||
import "./messages/messages.listener";
|
||||
import "./reactions/reactions.listener";
|
||||
import "./guild-members/guild-members.listener";
|
||||
import "./voice-state/voice-state.listener";
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
import { Events, type GuildMember, User } from "@fluxerjs/core";
|
||||
import client from "lib/client";
|
||||
import { logger } from "lib/common-logger";
|
||||
|
||||
interface VoiceStateUpdate {
|
||||
guild_id?: string;
|
||||
channel_id: string | null;
|
||||
user_id: string;
|
||||
connection_id: string;
|
||||
session_id?: string;
|
||||
deaf: boolean;
|
||||
mute: boolean;
|
||||
self_deaf: boolean;
|
||||
self_mute: boolean;
|
||||
self_video: boolean;
|
||||
self_stream: boolean;
|
||||
is_mobile: boolean;
|
||||
viewer_stream_keys?: string[];
|
||||
version: number;
|
||||
}
|
||||
|
||||
client.on(Events.VoiceStateUpdate, async (data: VoiceStateUpdate) => {
|
||||
// logger.debug(data);
|
||||
// return;
|
||||
const guildId = data.guild_id;
|
||||
if (!guildId) return;
|
||||
|
||||
const guild = await client.guilds.fetch(guildId);
|
||||
if (!guild) return;
|
||||
|
||||
const userId = data.user_id;
|
||||
if (!userId) return;
|
||||
|
||||
const channelId = data.channel_id;
|
||||
if (!channelId) {
|
||||
logger.debug("!channelId");
|
||||
return;
|
||||
}
|
||||
logger.debug(`channelId: ${channelId}`);
|
||||
|
||||
const member = guild.members.get(userId) ?? (await guild.fetchMember(userId));
|
||||
if (!channelId) {
|
||||
logger.debug("!channelId");
|
||||
return;
|
||||
}
|
||||
logger.debug(`channelId: ${channelId}`);
|
||||
|
||||
const channel =
|
||||
client.channels.get(channelId) ?? (await client.channels.fetch(channelId));
|
||||
if (!channel) {
|
||||
logger.debug("!channel");
|
||||
return;
|
||||
}
|
||||
logger.debug(`channel: ${channel}`);
|
||||
|
||||
if (!member?.user) {
|
||||
logger.debug("!member?.user");
|
||||
return;
|
||||
}
|
||||
logger.debug(`member?.user${member?.user}`);
|
||||
|
||||
logger.debug("user joined vc");
|
||||
if (!member?.user.bot) {
|
||||
logger.debug("!member.user.bot");
|
||||
return;
|
||||
}
|
||||
logger.debug("user joined vc");
|
||||
});
|
||||
@@ -6,6 +6,7 @@ export interface VoiceChannelsServiceInterface<
|
||||
createVoiceChannel(name: string): Promise<C>;
|
||||
|
||||
cloneVoiceChannel(
|
||||
user_id: string,
|
||||
channel: C,
|
||||
options?: { name?: string; position?: number },
|
||||
): Promise<C>;
|
||||
|
||||
@@ -30,27 +30,29 @@ export class DynamicVoiceChannelsService<
|
||||
);
|
||||
}
|
||||
|
||||
async createDynamicVoiceChannel(id: string): Promise<C | undefined> {
|
||||
if (this.validChannelIds.has(id)) {
|
||||
// Channel is one of the dynamic voice channels, create a new one based on it
|
||||
async createDynamicVoiceChannel(
|
||||
user_id: string,
|
||||
channel_id: string,
|
||||
): Promise<C | undefined> {
|
||||
if (!this.validChannelIds.has(channel_id)) return;
|
||||
// Channel is one of the dynamic voice channels, create a new one based on it
|
||||
|
||||
const channel = await this.voiceChannelsService.getVoiceChannelById(id);
|
||||
const channel =
|
||||
await this.voiceChannelsService.getVoiceChannelById(channel_id);
|
||||
|
||||
if (!channel) return;
|
||||
if (!channel) return;
|
||||
|
||||
const newChannel = await this.voiceChannelsService.cloneVoiceChannel(
|
||||
channel,
|
||||
{
|
||||
name: channel.name,
|
||||
},
|
||||
);
|
||||
const newChannel = await this.voiceChannelsService.cloneVoiceChannel(
|
||||
user_id,
|
||||
channel,
|
||||
{
|
||||
name: channel.name,
|
||||
},
|
||||
);
|
||||
|
||||
this.createdChannelIdsSet.add(newChannel.id);
|
||||
this.createdChannelIdsSet.add(newChannel.id);
|
||||
|
||||
return newChannel;
|
||||
} else {
|
||||
// Channel is not a dynamic voice channel, do nothing
|
||||
}
|
||||
return newChannel;
|
||||
}
|
||||
|
||||
deleteDynamicVoiceChannel(channelId: string): Promise<void> {
|
||||
|
||||
Reference in New Issue
Block a user