From facb7bc6a264205474b7d694a09cfd42315054ed Mon Sep 17 00:00:00 2001 From: Sebastian Pietschner Date: Sun, 6 Mar 2022 09:25:18 +0000 Subject: [PATCH] move people when the bot restarts --- dashboard | 1 - src/events/guildCreate.ts | 70 +++++++++++++++++-------------- src/events/ready.ts | 61 +++++++++++++++++---------- src/utils/operations/secondary.ts | 5 ++- 4 files changed, 81 insertions(+), 56 deletions(-) delete mode 160000 dashboard diff --git a/dashboard b/dashboard deleted file mode 160000 index 95a5909..0000000 --- a/dashboard +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 95a5909b51a5e666601249a74380536c5654fcf8 diff --git a/src/events/guildCreate.ts b/src/events/guildCreate.ts index a385d9c..a32b18a 100644 --- a/src/events/guildCreate.ts +++ b/src/events/guildCreate.ts @@ -20,27 +20,27 @@ const basicCommands: { */ description: string; }[] = [ - { - command: "/help", - help: "https://dynamica.dev/docs/commands/help", - description: "Get a list of commands and their descriptions", - }, - { - command: "/invite", - help: "https://dynamica.dev/docs/commands/invite", - description: "Get an invite link for the bot", - }, - { - command: "/create", - help: "https://dynamica.dev/docs/commands/create", - description: "Create a new channel for people to join", - }, - { - command: "/alias", - help: "https://dynamica.dev/docs/commands/alias", - description: "Create a new alias for an activity", - }, -]; + { + command: "/help", + help: "https://dynamica.dev/docs/commands/help", + description: "Get a list of commands and their descriptions", + }, + { + command: "/invite", + help: "https://dynamica.dev/docs/commands/invite", + description: "Get an invite link for the bot", + }, + { + command: "/create", + help: "https://dynamica.dev/docs/commands/create", + description: "Create a new channel for people to join", + }, + { + command: "/alias", + help: "https://dynamica.dev/docs/commands/alias", + description: "Create a new alias for an activity", + }, + ]; const botInfoEmbed = new Embed() .setTitle("Welcome to Dynamica!") @@ -52,8 +52,7 @@ const botInfoEmbed = new Embed() value: basicCommands .map( (command) => - `${hyperlink(`\`${command.command}\``, command.help)} - ${ - command.description + `${hyperlink(`\`${command.command}\``, command.help)} - ${command.description }` ) .join("\n"), @@ -76,16 +75,23 @@ export const guildCreate: Event = { once: false, event: "guildCreate", async execute(guild: Guild) { - if (guild.systemChannel) { - guild.systemChannel.send({ - embeds: [botInfoEmbed], + if (!guild.me.permissions.has("ADMINISTRATOR")) { + const owner = await guild.fetchOwner() + owner.send(`Hi there, I see you tried to invite me into your server. To make sure that the bot works correctly please allow it to have admin permissions and then re-invite it.\n\nIf you need more info as to why the bot needs admin go ${hyperlink('here', "https://dynamica.dev/docs/faq#why-does-this-random-bot-need-admin")}.`) + await guild.leave() + } else { + if (guild.systemChannel) { + guild.systemChannel.send({ + embeds: [botInfoEmbed], + }); + } + await db.guild.create({ + data: { + id: guild.id, + }, }); + logger.debug(`Joined guild ${guild.id} named: ${guild.name}`); } - await db.guild.create({ - data: { - id: guild.id, - }, - }); - logger.debug(`Joined guild ${guild.id} named: ${guild.name}`); + }, }; diff --git a/src/events/ready.ts b/src/events/ready.ts index 51e7609..1f40bc4 100644 --- a/src/events/ready.ts +++ b/src/events/ready.ts @@ -3,34 +3,53 @@ import { Event } from "../Event"; import { db } from "../utils/db"; import { logger } from "../utils/logger"; import { updateActivityCount } from "../utils/operations/general"; -import { editChannel } from "../utils/operations/secondary"; +import { createSecondary, editChannel } from "../utils/operations/secondary"; export const ready: Event = { once: true, event: "ready", async execute(client: Client) { - const activeSecondaries = await db.secondary.findMany(); - for (let index = 0; index < activeSecondaries.length; index++) { - const element = activeSecondaries[index]; - const channel = await client.channels.cache.get(element.id); - if (!channel) { - db.secondary.delete({ where: { id: element.id } }).then((secondary) => { - if (secondary.textChannelId) { - client.channels.cache.get(secondary.textChannelId).delete(); - } - logger.info(`Deleted Stale Secondary ${element.id}`); - }); - - return; + try { + const activeSecondaries = await db.secondary.findMany(); + const primaries = await db.primary.findMany(); + for (let index = 0; index < primaries.length; index++) { + const element = primaries[index]; + const channel = await client.channels.cache.get(element.id) + if (!channel.isVoice()) return; + if (channel.members.size > 0) { + const channelMembers = [...channel.members.values()] + channelMembers[0] + const secondary = await createSecondary(channel.guild.channels, element.id, channelMembers[0]) + channelMembers.slice(1).forEach(channelMember => { + const currentVoice = channelMember.voice.setChannel(secondary) + }); + } } - if (!channel.isVoice()) { - logger.info(`Not a voice channel`); - return; + for (let index = 0; index < activeSecondaries.length; index++) { + const element = activeSecondaries[index]; + const channel = await client.channels.cache.get(element.id); + if (!channel) { + db.secondary.delete({ where: { id: element.id } }).then((secondary) => { + if (secondary.textChannelId) { + client.channels.cache.get(secondary.textChannelId).delete(); + } + logger.info(`Deleted Stale Secondary ${element.id}`); + }); + + return; + } + if (!channel.isVoice()) { + logger.info(`Not a voice channel`); + return; + } + logger.info(`Channel restarted ${channel.id}`); + editChannel({ channel }); } - logger.info(`Channel restarted ${channel.id}`); - editChannel({ channel }); + logger.info(`Ready! Logged in as ${client.user?.tag}`); + updateActivityCount(client); + } catch (error) { + logger.error(error) } - logger.info(`Ready! Logged in as ${client.user?.tag}`); - updateActivityCount(client); + }, }; diff --git a/src/utils/operations/secondary.ts b/src/utils/operations/secondary.ts index 573015d..874a8fa 100644 --- a/src/utils/operations/secondary.ts +++ b/src/utils/operations/secondary.ts @@ -152,6 +152,7 @@ export const createSecondary = async ( logger.debug( `Secondary channel ${secondary.name} created by ${member?.user.tag} in ${channelManager.guild.name}.` ); + return secondary } catch (error) { logger.error(error); } @@ -220,8 +221,8 @@ export async function editChannel({ channel }: { channel: VoiceBasedChannel }) { const str = secondary.name ? secondary.name : !filteredActivityList.length - ? secondary.primary.generalName - : secondary.primary.template; + ? secondary.primary.generalName + : secondary.primary.template; /** * The formatted name