From 3f7864be1bb2753b753173b3bfd2cf2e3cb4d726 Mon Sep 17 00:00:00 2001 From: moriese Date: Mon, 13 Jan 2025 01:47:27 +0100 Subject: [PATCH] able to duplicate channel with only joining --- .../dynamicChannel.components.ts | 0 .../dynamicChannel/dynamicChannel.service.ts | 52 +++++++++++++++ src/actions/greeting/greeting.components.ts | 18 +++--- src/config.ts | 10 ++- src/controllers/discord.controller.ts | 63 +++++++++++++++++++ src/lib/client.ts | 39 +++++------- 6 files changed, 149 insertions(+), 33 deletions(-) create mode 100644 src/actions/dynamicChannel/dynamicChannel.components.ts create mode 100644 src/actions/dynamicChannel/dynamicChannel.service.ts diff --git a/src/actions/dynamicChannel/dynamicChannel.components.ts b/src/actions/dynamicChannel/dynamicChannel.components.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/actions/dynamicChannel/dynamicChannel.service.ts b/src/actions/dynamicChannel/dynamicChannel.service.ts new file mode 100644 index 0000000..0912250 --- /dev/null +++ b/src/actions/dynamicChannel/dynamicChannel.service.ts @@ -0,0 +1,52 @@ +import config from "config"; +import client from "lib/client"; +import { getRandomInt } from "lib/utils"; +import { } from "./dynamicChannel.components.ts"; +import { + Client, + EmbedBuilder, + type Message, + type CacheType, + type GuildMember, + type Interaction, + type OmitPartialGroupDMChannel, + VoiceState, + VoiceChannel, + StageChannel, + Events, + type VoiceBasedChannel, +} from "discord.js"; + +export class DynamicChannelService { + async handleInteraction(interaction: Interaction) { + // todo + } + + async createChannel(oldState: VoiceState, newState: VoiceState, channel: VoiceBasedChannel): Promise { + console.log("createChannel()"); + + const newChannel = await channel.clone({ + name: channel.name + " " + newState.member?.displayName, + position: channel.position + }); + + return newChannel; + } + + async deleteChannel(oldState: VoiceState, newState: VoiceState, newChannel: StageChannel | VoiceChannel, channelListeners: Map, channelListener: (oldState: VoiceState, newState: VoiceState) => void) { + + console.log("deleteChannel()"); + + if (oldState.channelId === newChannel.id || newState.channelId === newChannel.id) { + + if (newChannel.members.size === 0) { + newChannel.delete() + .catch(console.error); + + client.removeListener(Events.VoiceStateUpdate, channelListener); + channelListeners.delete(newChannel.id); + } + } + return channelListeners; + } +} diff --git a/src/actions/greeting/greeting.components.ts b/src/actions/greeting/greeting.components.ts index 09ddf9e..88c2225 100644 --- a/src/actions/greeting/greeting.components.ts +++ b/src/actions/greeting/greeting.components.ts @@ -8,31 +8,31 @@ export const dmAcceptedContent = `huhu! du wurdest als lernende:r akzeptiert :3\ export function getWelcomeContent(member: GuildMember) { const welcomeContents = [ - `willkommen auf dem server, ${member}! 💕`, + `willkommen auf dem server, ${member}! fuehl dich wie zuhause💕`, `hey ${member}! schoen, dass du hier bist! 😊`, `hi ${member}, willkommen! viel spass hier! 💖`, - `willkommen, ${member}! schoen, dass du da bist! 🥳`, + `willkommen, ${member}! schoen, dass du da bist! :3`, `moin ${member}! viel spass im server! c:`, `hey ${member}, herzlich willkommen! fuehl dich wie zu hause! <3`, - `hi ${member}! cool, dass du da bist! 👏`, + `hi ${member}! cool, dass du da bist! <3`, `willkommen, ${member}! wir freuen uns, dass du hier bist! 💕`, `hey ${member}! schoen, dass du bei uns bist! :3`, `willkommen auf dem server, ${member}! viel spass hier! ✨`, - `hi ${member}, super, dass du dabei bist! 😊`, + `hi ${member}, super, dass du dabei bist! :3`, `hey ${member}, willkommen bei uns! 💖`, - `moin ${member}! schoen, dass du dabei bist! 🥳`, + `moin ${member}! schoen, dass du dabei bist! :)`, `hi ${member}, willkommen in unserer kleinen community! ✨`, `willkommen, ${member}! fuehl dich wie zu hause! 💕`, - `hey ${member}, schoen, dass du uns gefunden hast! 😊`, - `hi ${member}, willkommen in unserer runde! 🎉`, + `hey ${member}, schoen, dass du uns gefunden hast! <333`, + `hi ${member}, willkommen in unserer runde! c:`, `willkommen, ${member}! schoen, dass du hier bist! 💖`, `moin ${member}! lass uns zusammen spass haben! ✨`, `hey ${member}, herzlich willkommen bei uns! 😊`, `hi ${member}! schoen, dass du dabei bist! 💕`, - `willkommen auf dem server, ${member}! wir freuen uns auf dich! 🎉`, + `willkommen auf dem server, ${member}! wir freuen uns auf dich! <3`, `hey ${member}, schoen, dass du da bist! ✨`, `hi ${member}, willkommen! fuehl dich wie zu hause! 💖`, - `willkommen, ${member}! lass uns gemeinsam eine tolle zeit haben! 😊`, + `willkommen, ${member}! lass uns gemeinsam eine tolle zeit haben! :3`, ]; return welcomeContents[Math.floor(Math.random() * welcomeContents.length)]; } \ No newline at end of file diff --git a/src/config.ts b/src/config.ts index efd28e8..33f986d 100644 --- a/src/config.ts +++ b/src/config.ts @@ -5,16 +5,22 @@ export default { // avocadi serverID: process.env.DISCORD_SERVER_ID || "", + // texxt channel channelIdBot: process.env.DISCORD_CHANNEL_ID_BOT || "", + channelIdFeedback: process.env.DISCORD_CHANNEL_ID_FEEDBACK || "", channelIdNotification: process.env.DISCORD_CHANNEL_ID_NOTIFICATION || "", channelIdWelcome: process.env.DISCORD_CHANNEL_ID_WELCOME || "", channelIdRules: process.env.DISCORD_CHANNEL_ID_RULE || "", channelIdNews: process.env.DISCORD_CHANNEL_ID_NEWS || "", channelIdIntroduction: process.env.DISCORD_CHANNEL_ID_INTRODUCTION || "", - channelIdOffTopic: process.env.DISCORD_CHANNEL_ID_OFF_TOPIC || "", - channelIdFeedback: process.env.DISCORD_CHANNEL_ID_FEEDBACK || "", channelIdHelp: process.env.DISCORD_CHANNEL_ID_HELP || "", + // voice channel# + vchannelIdForTwo: process.env.DISCORD_VCHANNEL_ID_FOR_TWO || "", + vchannelIdForThree: process.env.DISCORD_VCHANNEL_ID_FOR_THREE || "", + vchannelIdForFour: process.env.DISCORD_VCHANNEL_ID_FOR_FOUR || "", + vchannelIdForGroup: process.env.DISCORD_VCHANNEL_ID_FOR_GROUP || "", + // roles roleStudy: process.env.PEOPLE || "", // other diff --git a/src/controllers/discord.controller.ts b/src/controllers/discord.controller.ts index 36fb6c4..245729f 100644 --- a/src/controllers/discord.controller.ts +++ b/src/controllers/discord.controller.ts @@ -1,6 +1,10 @@ import { Commands, type CommandsType } from "commands"; import { ChannelType, + Client, + Events, + IntentsBitField, + VoiceState, type ButtonInteraction, type CacheType, type ChatInputCommandInteraction, @@ -17,6 +21,8 @@ import { GreetingService } from "actions/greeting/greeting.service"; import { ActivityService } from "actions/activity/activity.service"; import { DmService } from "actions/dm/dm.service"; import { CustomMessageService } from "actions/customMessage/customMessage.service"; +import { DynamicChannelService } from "actions/dynamicChannel/dynamicChannel.service"; +import config from "config"; export default class DiscordController extends EventEmitter { private discordService!: DiscordService; @@ -27,6 +33,8 @@ export default class DiscordController extends EventEmitter { activityService: ActivityService; dmService: DmService; customMessageService: CustomMessageService; + channelListeners = new Map(); + dynamicChannelService: DynamicChannelService; constructor() { super(); @@ -38,6 +46,9 @@ export default class DiscordController extends EventEmitter { this.activityService = new ActivityService(); this.dmService = new DmService(); this.customMessageService = new CustomMessageService(); + this.dynamicChannelService = new DynamicChannelService(); + + var channelListeners = new Map(); // log when running client.once("ready", async () => { await this.setActivity(); @@ -58,6 +69,58 @@ export default class DiscordController extends EventEmitter { client.on("guildMemberAdd", async (member) => { await this.greetingService.welcome(member); }); + + client.on(Events.VoiceStateUpdate, async (oldState: VoiceState, newState: VoiceState) => { + // check if user joined a vc + if (!oldState.channelId && newState.channelId) { + // check if right vc + if (newState.channelId === config.discord.vchannelIdForTwo || newState.channelId === config.discord.vchannelIdForThree || newState.channelId === config.discord.vchannelIdForFour || newState.channelId === config.discord.vchannelIdForGroup) { + const channel = newState.channel; + if (!channel) { + console.error("channel not found"); + return; + } + try { + // create new channel with same settings + /*const newChannel = await channel.clone({ + name: channel.name + "; " + newState.member?.displayName, + position: channel.position + });*/ + + + const newChannel = await this.dynamicChannelService.createChannel(oldState, newState, channel); + + // move user in new channel + await newState.setChannel(newChannel); + + // create specific listener for channel + const channelListener = async (oldState: VoiceState, newState: VoiceState) => { + /*if (oldState.channelId === newChannel.id || newState.channelId === newChannel.id) { + // check if channel empty + if (newChannel.members.size === 0) { + newChannel.delete() + .catch(console.error); + // delete listener for this channel + client.removeListener(Events.VoiceStateUpdate, channelListener); + channelListeners.delete(newChannel.id); + } + }*/ + channelListeners = await this.dynamicChannelService.deleteChannel(oldState, newState, newChannel, channelListeners, channelListener); + + }; + // save listener in map + channelListeners.set(newChannel.id, channelListener); + + // add listener + client.on(Events.VoiceStateUpdate, channelListener); + + } catch (error) { + console.error("error while duplicating channel", error); + } + } + } + }); + } async setActivity() { diff --git a/src/lib/client.ts b/src/lib/client.ts index 827dcf5..0202479 100644 --- a/src/lib/client.ts +++ b/src/lib/client.ts @@ -1,28 +1,23 @@ import config from "config"; -import { - Client, - GatewayIntentBits, - Partials, - IntentsBitField, -} from "discord.js"; +import { Client, GatewayIntentBits, Partials, ChannelType, Events, IntentsBitField } from "discord.js"; const client = new Client({ - intents: [ - IntentsBitField.Flags.Guilds, - IntentsBitField.Flags.GuildMembers, - IntentsBitField.Flags.GuildModeration, - IntentsBitField.Flags.GuildMessages, - IntentsBitField.Flags.GuildMessageReactions, - IntentsBitField.Flags.GuildMessagePolls, - IntentsBitField.Flags.MessageContent, - IntentsBitField.Flags.DirectMessages, - IntentsBitField.Flags.DirectMessageReactions, - IntentsBitField.Flags.DirectMessageTyping, - IntentsBitField.Flags.DirectMessagePolls, - GatewayIntentBits.DirectMessages, - GatewayIntentBits.MessageContent, - ], - partials: [Partials.Channel, Partials.Message], + intents: [IntentsBitField.Flags.Guilds, + IntentsBitField.Flags.GuildMembers, + IntentsBitField.Flags.GuildModeration, + IntentsBitField.Flags.GuildMessages, + IntentsBitField.Flags.GuildMessageReactions, + IntentsBitField.Flags.GuildMessagePolls, + IntentsBitField.Flags.MessageContent, + IntentsBitField.Flags.DirectMessages, + IntentsBitField.Flags.DirectMessageReactions, + IntentsBitField.Flags.DirectMessageTyping, + IntentsBitField.Flags.DirectMessagePolls, + GatewayIntentBits.DirectMessages, + GatewayIntentBits.MessageContent, + GatewayIntentBits.Guilds, + GatewayIntentBits.GuildVoiceStates,], + partials: [Partials.Channel, Partials.Message] }); await client.login(config.discord.token);