From 11fa1827695aabd982f96bc7772b05c0410730c5 Mon Sep 17 00:00:00 2001 From: dev-737 <73829355+dev-737@users.noreply.github.com> Date: Sun, 20 Aug 2023 09:56:48 +0530 Subject: [PATCH 1/5] feat(network): thread and forum post support --- prisma/schema.prisma | 3 +- src/Commands/Main/hub.ts | 2 +- src/Events/messageCreate.ts | 2 + src/Scripts/hub/join.ts | 4 +- src/Scripts/network/displaySettings.ts | 82 ++++++++++++++++---------- src/Scripts/network/initialize.ts | 50 +++++++++++----- src/Scripts/support/server.ts | 2 +- 7 files changed, 96 insertions(+), 49 deletions(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index b013c82e..20d9ea0b 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -62,7 +62,8 @@ model blacklistedUsers { model connectedList { id String @id @default(auto()) @map("_id") @db.ObjectId - channelId String @unique + channelId String @unique // channel can be thread, or a normal channel + parentId String? // ID of the parent channel, if it's a thread @map("parentChannelId") serverId String connected Boolean compact Boolean diff --git a/src/Commands/Main/hub.ts b/src/Commands/Main/hub.ts index f1545276..efeec369 100644 --- a/src/Commands/Main/hub.ts +++ b/src/Commands/Main/hub.ts @@ -36,7 +36,7 @@ export default { .addChannelOption(channelOption => channelOption .setName('channel') - .addChannelTypes(ChannelType.GuildText) + .addChannelTypes(ChannelType.GuildText, ChannelType.PublicThread, ChannelType.PrivateThread) .setDescription('The channel that will be used to connect to the hub') .setRequired(true), ) diff --git a/src/Events/messageCreate.ts b/src/Events/messageCreate.ts index 6efe8a2d..e93c69c6 100644 --- a/src/Events/messageCreate.ts +++ b/src/Events/messageCreate.ts @@ -105,6 +105,7 @@ export default { content: connection?.profFilter ? message.censored_content : message.content, embeds: replyEmbed ? [replyEmbed] : undefined, files: attachment ? [attachment] : [], + threadId: connection.parentId ? connection.channelId : undefined, allowedMentions: { parse: [] }, }; } @@ -115,6 +116,7 @@ export default { username: message.client.user.username, avatarURL: message.client.user.avatarURL() || undefined, files: attachment ? [attachment] : [], + threadId: connection.parentId ? connection.channelId : undefined, allowedMentions: { parse: [] }, }; } diff --git a/src/Scripts/hub/join.ts b/src/Scripts/hub/join.ts index 56c64888..928a7ca6 100644 --- a/src/Scripts/hub/join.ts +++ b/src/Scripts/hub/join.ts @@ -10,7 +10,7 @@ export async function execute(interaction: ChatInputCommandInteraction) { const db = getDb(); const name = interaction.options.getString('name') || undefined; const invite = interaction.options.getString('invite') || undefined; - const channel = interaction.options.getChannel('channel', true, [ChannelType.GuildText]); + const channel = interaction.options.getChannel('channel', true, [ChannelType.GuildText, ChannelType.PublicThread, ChannelType.PrivateThread]); const channelConnected = await db.connectedList.findFirst({ where: { channelId: channel.id } }); let hubExists: hubs | null = null; @@ -112,5 +112,5 @@ export async function execute(interaction: ChatInputCommandInteraction) { // TODO: make an onboarding function and show them rules and stuff initialize.execute(interaction, hubExists, channel) - .then(success => { if (success) displaySettings.execute(interaction, channel.id); }); + .then(success => { if (success) displaySettings.execute(interaction, success.channelId); }); } diff --git a/src/Scripts/network/displaySettings.ts b/src/Scripts/network/displaySettings.ts index 3d199524..739833c8 100644 --- a/src/Scripts/network/displaySettings.ts +++ b/src/Scripts/network/displaySettings.ts @@ -1,4 +1,4 @@ -import { ChatInputCommandInteraction, ButtonBuilder, ActionRowBuilder, ButtonStyle, GuildTextBasedChannel, EmbedBuilder, ChannelType, ComponentType, StringSelectMenuBuilder, StringSelectMenuOptionBuilder, Interaction, ChannelSelectMenuBuilder, ModalBuilder, TextInputBuilder, TextInputStyle, TextChannel, ButtonInteraction, AnySelectMenuInteraction } from 'discord.js'; +import { ChatInputCommandInteraction, ButtonBuilder, ActionRowBuilder, ButtonStyle, GuildTextBasedChannel, EmbedBuilder, ChannelType, ComponentType, StringSelectMenuBuilder, StringSelectMenuOptionBuilder, Interaction, ChannelSelectMenuBuilder, ModalBuilder, TextInputBuilder, TextInputStyle, TextChannel, ButtonInteraction, AnySelectMenuInteraction, Webhook, ThreadChannel } from 'discord.js'; import { reconnect, disconnect } from '../../Structures/network'; import { colors, getDb } from '../../Utils/functions/utils'; import logger from '../../Utils/logger'; @@ -98,11 +98,6 @@ export = { filter, componentType: ComponentType.Button, }); - const selectCollector = setupMessage.createMessageComponentCollector({ - filter, - idle: 60_000 * 5, - componentType: ComponentType.StringSelect, - }); /* ------------------- Button Responce collectors ---------------------- */ buttonCollector.on('collect', async (component) => { @@ -171,7 +166,14 @@ export = { }); - /* ------------------- SelectMenu Responce collectors ---------------------- */ + + /* ------------------- Replying to SelectMenus ---------------------- */ + const selectCollector = setupMessage.createMessageComponentCollector({ + filter, + idle: 60_000 * 5, + componentType: ComponentType.StringSelect, + }); + selectCollector.on('collect', async (settingsMenu) => { const updConnection = await db.connectedList.findFirst({ where: { channelId: connection.channelId } }); if (!updConnection) { @@ -184,7 +186,7 @@ export = { switch (settingsMenu.values[0]) { /* Compact / Normal mode toggle */ - case 'compact':{ + case 'compact': { await db.connectedList.update({ where: { channelId: updConnection.channelId }, data: { compact: !updConnection.compact }, @@ -192,68 +194,85 @@ export = { break; } /* Profanity toggle */ - case 'profanity': + case 'profanity': { await db.connectedList.update({ where: { channelId: updConnection.channelId }, data: { profFilter: !updConnection.profFilter }, }); break; + } - /* Change channel request Response */ case 'change_channel': { const channelMenu = new ActionRowBuilder() .addComponents( new ChannelSelectMenuBuilder() .setCustomId('newChannelSelect') .setPlaceholder('Select new channel') - .addChannelTypes(ChannelType.GuildText), + .addChannelTypes(ChannelType.GuildText, ChannelType.PublicThread, ChannelType.PrivateThread), ); const changeMsg = await settingsMenu.reply({ content: 'Please select a channel within the next 20 seconds.', components: [channelMenu], ephemeral: true, + fetchReply: true, }); const selected = await changeMsg.awaitMessageComponent({ componentType: ComponentType.ChannelSelect, - idle: 20_000, + time: 20_000, }).catch(() => null); if (!selected) return; - const oldchannel = selected.guild?.channels.cache.get(`${updConnection?.channelId}`) as TextChannel; - const channel = selected.guild?.channels.cache.get(selected?.values[0]) as TextChannel; + const newchannel = selected.guild?.channels.cache.get(selected?.values[0]) as TextChannel | ThreadChannel; + const newchannelInDb = await db.connectedList.findFirst({ where: { channelId: newchannel.id } }); - if (await db.connectedList.findFirst({ where: { channelId: channel.id } })) { + // if the hubId doesn't match with the already connected channel + // don't let to switch channel as it is already connected to another hub + if (newchannelInDb && newchannelInDb.channelId !== updConnection.channelId) { await selected.update({ - content: `${emoji.normal.no} Channel ${channel} is already connected to a hub. Please leave that hub first or select another channel.`, + content: `${emoji.normal.no} Channel ${newchannel} has already joined a hub. Either leave that hub first or select another channel.`, components: [], }); return; } - // delete the old webhook - oldchannel?.fetchWebhooks() - .then(promisehook => promisehook.find((hook) => hook.owner?.id === hook.client.user?.id)?.delete().catch(() => null)) - .catch(() => null); + let webhook: Webhook | null = null; + if (newchannel.type === ChannelType.GuildText) { + const webhooks = await newchannel.fetchWebhooks(); + const interchatHook = webhooks?.find((hook) => hook.owner?.id === hook.client.user?.id); - // create a webhook in the new channel - const webhook = await channel?.createWebhook({ - name: 'InterChat Network', - avatar: selected.client.user.avatarURL(), - }); + // create a webhook in the new channel + webhook = interchatHook || + await newchannel.createWebhook({ + name: 'InterChat Network', + avatar: newchannel.client.user.avatarURL(), + }); + } + + else if (newchannel.isThread() && newchannel.parent) { + const webhooks = await newchannel.parent.fetchWebhooks(); + const interchatHook = webhooks?.find((hook) => hook.owner?.id === hook.client.user?.id); + + webhook = interchatHook || + await newchannel.parent.createWebhook({ + name: 'InterChat Network', + avatar: newchannel.client.user.avatarURL(), + }); + } await db.connectedList.update({ - where: { channelId: updConnection.channelId }, + where: { channelId: connection.channelId }, data: { - channelId: channel?.id, - webhookURL: webhook.url, + channelId: newchannel.id, + parentId: newchannel?.isThread() ? newchannel.parentId : { unset: true }, + webhookURL: webhook?.url, }, }); - await selected?.update({ - content: `Successfully switched connection from ${oldchannel} to ${channel}!`, + await selected.update({ + content: `${emoji.normal.yes} Channel has been changed to ${newchannel}!`, components: [], }); break; @@ -342,3 +361,6 @@ export = { }); }, }; + +// TODO: Hub leave command shows channel and now thread names in autocomplete +// TODO: channelId is no longer unique, either make it unique or fix the whole code diff --git a/src/Scripts/network/initialize.ts b/src/Scripts/network/initialize.ts index 8ec74b08..c79b9e7b 100644 --- a/src/Scripts/network/initialize.ts +++ b/src/Scripts/network/initialize.ts @@ -1,5 +1,5 @@ import { stripIndents } from 'common-tags'; -import { ChannelType, ChatInputCommandInteraction, Collection, GuildTextBasedChannel } from 'discord.js'; +import { ChannelType, ChatInputCommandInteraction, Collection, TextChannel, ThreadChannel } from 'discord.js'; import { disconnect } from '../../Structures/network'; import { hubs } from '@prisma/client'; import logger from '../../Utils/logger'; @@ -9,7 +9,7 @@ import { getDb } from '../../Utils/functions/utils'; const onboardingInProgress = new Collection(); export = { - async execute(interaction: ChatInputCommandInteraction, hub: hubs, networkChannel: GuildTextBasedChannel) { + async execute(interaction: ChatInputCommandInteraction, hub: hubs, networkChannel: TextChannel | ThreadChannel) { const emoji = interaction.client.emotes.normal; // Check if server is already attempting to join a hub @@ -30,36 +30,58 @@ export = { return; } + let createdConnection; try { - if (networkChannel.type !== ChannelType.GuildText) { - interaction.followUp(`${emoji.no} You can only connect **text channels** to the InterChat network!`); - return; + let webhook; + if (networkChannel.parent?.type === ChannelType.GuildForum || networkChannel.parent?.type === ChannelType.GuildText) { + const webhooks = await networkChannel.parent.fetchWebhooks(); + const webhookCreated = webhooks.find(w => w.owner?.id === interaction.client.user?.id); + + if (webhookCreated) { + webhook = webhookCreated; + } + else { + webhook = await networkChannel.parent.createWebhook({ + name: 'InterChat Network', + avatar: interaction.client.user?.avatarURL(), + }); + } + } + else if (networkChannel.type === ChannelType.GuildText) { + webhook = await networkChannel.createWebhook({ + name: 'InterChat Network', + avatar: interaction.client.user?.avatarURL(), + }); + } + else { + return interaction.followUp('This channel is not supported for InterChat. Please use a text channel or a thread.'); } - const webhook = await networkChannel.createWebhook({ - name: 'InterChat Network', - avatar: interaction.client.user?.avatarURL(), - }); const { connectedList } = getDb(); - await connectedList.create({ + createdConnection = await connectedList.create({ data:{ channelId: networkChannel.id, + parentId: networkChannel.isThread() ? networkChannel.id : undefined, serverId: networkChannel.guild.id, + webhookURL: webhook.url, connected: true, profFilter: true, compact: false, - webhookURL: webhook.url, hub: { connect: { id: hub.id } }, }, }); const numOfConnections = await connectedList.count({ where: { hub: { id: hub.id } } }); if (numOfConnections > 1) { - await networkChannel?.send(`This channel has been connected with ${hub.name}. You are currently with ${numOfConnections - 1} other servers, Enjoy! ${emoji.clipart}`); + await networkChannel?.send(`This channel has been connected with ${hub.name}. `); } else { - await networkChannel?.send(`This channel has been connected with ${hub.name}, though no one else is there currently... *cricket noises* ${emoji.clipart}`); + await networkChannel?.send(`This channel has been connected with ${hub.name}. ${ + numOfConnections > 1 + ? `You are currently with ${numOfConnections - 1} other servers, Enjoy! ${emoji.clipart}` + : `It seems no one else is there currently... *cricket noises* ${emoji.clipart}` + }`); } } // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -88,6 +110,6 @@ export = { **Server Name:** __${interaction.guild?.name}__ **Member Count:** __${interaction.guild?.memberCount}__ `, { id: hub.id }); - return true; // just a marker to show that the setup was successful + return createdConnection; // just a marker to show that the setup was successful }, }; diff --git a/src/Scripts/support/server.ts b/src/Scripts/support/server.ts index 49fc6fc4..725601c5 100644 --- a/src/Scripts/support/server.ts +++ b/src/Scripts/support/server.ts @@ -4,7 +4,7 @@ import { colors } from '../../Utils/functions/utils'; export = { async execute(interaction: ChatInputCommandInteraction) { const embed = new EmbedBuilder() - .setTitle('InterChat HQ') + .setTitle('InterChat Central') .setDescription('[Click Here]()') .setColor(colors('chatbot')) .setTimestamp(); From de1023f7b1107f6413d29b66a21cadcb357c0a98 Mon Sep 17 00:00:00 2001 From: dev-737 <73829355+dev-737@users.noreply.github.com> Date: Sun, 20 Aug 2023 14:21:19 +0530 Subject: [PATCH 2/5] fix(hub): prevent joining same hub from different channels --- src/Commands/Main/hub.ts | 6 ++---- src/Scripts/hub/join.ts | 10 +++++----- src/Scripts/network/initialize.ts | 9 ++++++--- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/Commands/Main/hub.ts b/src/Commands/Main/hub.ts index efeec369..11170a9b 100644 --- a/src/Commands/Main/hub.ts +++ b/src/Commands/Main/hub.ts @@ -242,11 +242,9 @@ export default { async execute(interaction: ChatInputCommandInteraction) { const subcommand = interaction.options.getSubcommand(); const subcommandGroup = interaction.options.getSubcommandGroup(); - const extra = subcommand === 'leave' + const extra = subcommand === 'leave' || subcommand === 'delete' ? interaction.options.getString('hub', true) - : subcommand === 'delete' - ? interaction.options.getString('hub', true) - : null; + : null; require(`../../Scripts/hub/${subcommandGroup || subcommand}`).execute(interaction, extra); }, diff --git a/src/Scripts/hub/join.ts b/src/Scripts/hub/join.ts index 928a7ca6..39994052 100644 --- a/src/Scripts/hub/join.ts +++ b/src/Scripts/hub/join.ts @@ -2,7 +2,6 @@ import { ChatInputCommandInteraction, ChannelType } from 'discord.js'; import { getDb } from '../../Utils/functions/utils'; import initialize from '../network/initialize'; import displaySettings from '../network/displaySettings'; -import { connectedList, hubs } from '@prisma/client'; export async function execute(interaction: ChatInputCommandInteraction) { if (!interaction.inCachedGuild()) return; @@ -11,8 +10,7 @@ export async function execute(interaction: ChatInputCommandInteraction) { const name = interaction.options.getString('name') || undefined; const invite = interaction.options.getString('invite') || undefined; const channel = interaction.options.getChannel('channel', true, [ChannelType.GuildText, ChannelType.PublicThread, ChannelType.PrivateThread]); - const channelConnected = await db.connectedList.findFirst({ where: { channelId: channel.id } }); - let hubExists: hubs | null = null; + let hubExists; if (!interaction.member.permissionsIn(channel).has(['ManageChannels'])) { return await interaction.reply({ @@ -27,6 +25,7 @@ export async function execute(interaction: ChatInputCommandInteraction) { ephemeral: true, }); } + const channelConnected = await db.connectedList.findFirst({ where: { channelId: channel.id } }); if (channelConnected) { return await interaction.reply({ content: `${channel} is already connected to a hub! Please leave the hub or choose a different channel.`, @@ -69,9 +68,10 @@ export async function execute(interaction: ChatInputCommandInteraction) { }); } - else if ((hubExists as hubs & { connections: connectedList}).connections.channelId === channel.id) { + const guildInHub = hubExists.connections.find(c => c.serverId === channel.guildId); + if (guildInHub) { return await interaction.reply({ - content: `This server is already connected to hub **${hubExists?.name}** from another channel!`, + content: `This server is already connected to hub **${hubExists?.name}** from <#${guildInHub.channelId}>! Please leave the hub from that channel first.`, ephemeral: true, }); } diff --git a/src/Scripts/network/initialize.ts b/src/Scripts/network/initialize.ts index c79b9e7b..ebc0795e 100644 --- a/src/Scripts/network/initialize.ts +++ b/src/Scripts/network/initialize.ts @@ -33,7 +33,7 @@ export = { let createdConnection; try { let webhook; - if (networkChannel.parent?.type === ChannelType.GuildForum || networkChannel.parent?.type === ChannelType.GuildText) { + if (networkChannel.isThread() && networkChannel.parent) { const webhooks = await networkChannel.parent.fetchWebhooks(); const webhookCreated = webhooks.find(w => w.owner?.id === interaction.client.user?.id); @@ -74,7 +74,7 @@ export = { const numOfConnections = await connectedList.count({ where: { hub: { id: hub.id } } }); if (numOfConnections > 1) { - await networkChannel?.send(`This channel has been connected with ${hub.name}. `); + await networkChannel?.send(`This channel has been connected with ${hub.name}.`); } else { await networkChannel?.send(`This channel has been connected with ${hub.name}. ${ @@ -94,7 +94,7 @@ export = { : interaction.reply(errMsg); } else { - const errMsg = `An error occurred while connecting to the InterChat network! \`\`\`js\n${err.message}\`\`\``; + const errMsg = `An error occurred while connecting to the InterChat network! Please report this to the developers: \`\`\`js\n${err.message}\`\`\``; interaction.deferred || interaction.replied ? interaction.followUp(errMsg) : interaction.reply(errMsg); @@ -104,6 +104,9 @@ export = { return; } + // dispose of the in-progress marker + onboardingInProgress.delete(networkChannel.id); + interaction.client.sendInNetwork(stripIndents` A new server has joined us! ${emoji.clipart} From 1c5556a1da0a11edf760b5e55696e3760e513c61 Mon Sep 17 00:00:00 2001 From: dev-737 <73829355+dev-737@users.noreply.github.com> Date: Sun, 20 Aug 2023 14:51:07 +0530 Subject: [PATCH 3/5] chore(eslint): updage tslint rules --- .eslintrc.json | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index aceaad1a..d32e1cee 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,30 +1,26 @@ { - "ignorePatterns": ["build/"], - "env": { - "node": true, - "es2021": true - }, + "ignorePatterns": ["build/", "node_modules/"], + "env": { "node": true, "es6": true }, "extends": [ "eslint:recommended", "plugin:@typescript-eslint/recommended" ], "parser": "@typescript-eslint/parser", - "parserOptions": { - "ecmaVersion": "latest" - }, - "plugins": [ - "@typescript-eslint" - ], + "parserOptions": { "ecmaVersion": "latest" }, + "plugins": [ "@typescript-eslint" ], + "root": true, + "rules": { "@typescript-eslint/no-var-requires": "off", "@typescript-eslint/no-unused-vars": "error", + "@typescript-eslint/indent": ["error", 2], + "@typescript-eslint/no-explicit-any": "warn", + "arrow-spacing": "error", "brace-style": [ "error", "stroustrup", - { - "allowSingleLine": true - } + { "allowSingleLine": true } ], "comma-dangle": [ "error", @@ -43,7 +39,6 @@ ], "handle-callback-err": "off", "indent": "off", - "@typescript-eslint/indent": ["error", 2], "keyword-spacing": "error", "max-nested-callbacks": [ "error", From 5102b861804b89a53fab9f5989ab26c804aec909 Mon Sep 17 00:00:00 2001 From: dev-737 <73829355+dev-737@users.noreply.github.com> Date: Sat, 26 Aug 2023 08:56:58 +0530 Subject: [PATCH 4/5] fix(hub): Prevent duplicate hub joins with invite --- src/Scripts/hub/join.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Scripts/hub/join.ts b/src/Scripts/hub/join.ts index 39994052..19ad14a5 100644 --- a/src/Scripts/hub/join.ts +++ b/src/Scripts/hub/join.ts @@ -28,7 +28,7 @@ export async function execute(interaction: ChatInputCommandInteraction) { const channelConnected = await db.connectedList.findFirst({ where: { channelId: channel.id } }); if (channelConnected) { return await interaction.reply({ - content: `${channel} is already connected to a hub! Please leave the hub or choose a different channel.`, + content: `${channel} is already part of a hub! Please leave the hub or choose a different channel.`, ephemeral: true, }); } @@ -45,9 +45,10 @@ export async function execute(interaction: ChatInputCommandInteraction) { ephemeral: true, }); } - else if (inviteExists.hub.connections.find((c) => c.channelId === channel.id)) { + const guildInHub = inviteExists.hub.connections.find((c) => c.serverId === channel.guildId); + if (guildInHub) { return await interaction.reply({ - content: `This server is already connected to hub **${inviteExists.hub.name}** from another channel!`, + content: `This server has already joined hub **${inviteExists.hub.name}** from from <#${guildInHub.channelId}>! Please leave the hub from that channel first, or change the channel using \`/network manage\`.!`, ephemeral: true, }); } @@ -71,7 +72,7 @@ export async function execute(interaction: ChatInputCommandInteraction) { const guildInHub = hubExists.connections.find(c => c.serverId === channel.guildId); if (guildInHub) { return await interaction.reply({ - content: `This server is already connected to hub **${hubExists?.name}** from <#${guildInHub.channelId}>! Please leave the hub from that channel first.`, + content: `This server has already joined hub **${hubExists?.name}** from <#${guildInHub.channelId}>! Please leave the hub from that channel first, or change the channel using \`/network manage\`.`, ephemeral: true, }); } From 2cd3b8a30c0db6c2c5d5896a0ec4e45e74f1c0d8 Mon Sep 17 00:00:00 2001 From: dev-737 <73829355+dev-737@users.noreply.github.com> Date: Tue, 29 Aug 2023 14:37:51 +0530 Subject: [PATCH 5/5] fix(hub): fix onboarding marker deletion --- src/Scripts/hub/join.ts | 5 +-- .../{initialize.ts => createConnection.ts} | 39 +++++++++---------- src/Scripts/network/onboarding.ts | 12 +++--- 3 files changed, 26 insertions(+), 30 deletions(-) rename src/Scripts/network/{initialize.ts => createConnection.ts} (81%) diff --git a/src/Scripts/hub/join.ts b/src/Scripts/hub/join.ts index 19ad14a5..0114e653 100644 --- a/src/Scripts/hub/join.ts +++ b/src/Scripts/hub/join.ts @@ -1,6 +1,6 @@ import { ChatInputCommandInteraction, ChannelType } from 'discord.js'; import { getDb } from '../../Utils/functions/utils'; -import initialize from '../network/initialize'; +import createConnection from '../network/createConnection'; import displaySettings from '../network/displaySettings'; export async function execute(interaction: ChatInputCommandInteraction) { @@ -111,7 +111,6 @@ export async function execute(interaction: ChatInputCommandInteraction) { return; } - // TODO: make an onboarding function and show them rules and stuff - initialize.execute(interaction, hubExists, channel) + createConnection.execute(interaction, hubExists, channel) .then(success => { if (success) displaySettings.execute(interaction, success.channelId); }); } diff --git a/src/Scripts/network/initialize.ts b/src/Scripts/network/createConnection.ts similarity index 81% rename from src/Scripts/network/initialize.ts rename to src/Scripts/network/createConnection.ts index ebc0795e..8c53c5c3 100644 --- a/src/Scripts/network/initialize.ts +++ b/src/Scripts/network/createConnection.ts @@ -8,13 +8,16 @@ import { getDb } from '../../Utils/functions/utils'; const onboardingInProgress = new Collection(); -export = { +export default { async execute(interaction: ChatInputCommandInteraction, hub: hubs, networkChannel: TextChannel | ThreadChannel) { const emoji = interaction.client.emotes.normal; // Check if server is already attempting to join a hub if (onboardingInProgress.has(networkChannel.id)) { - const err = `${emoji.no} There has already been an attempt to join a hub in ${networkChannel}. Please wait for that to finish before trying again!`; + const err = { + content: `${emoji.no} There has already been an attempt to join a hub in ${networkChannel}. Please wait for that to finish before trying again!`, + ephemeral: true, + }; interaction.deferred || interaction.replied ? interaction.followUp(err) : interaction.reply(err); @@ -24,11 +27,11 @@ export = { onboardingInProgress.set(networkChannel.id, networkChannel.id); // Show new users rules & info about network - const onboardingStatus = await onboarding.execute(interaction); - if (!onboardingStatus) { - onboardingInProgress.delete(networkChannel.id); - return; - } + const onboardingStatus = await onboarding.execute(interaction, hub.name); + // remove in-progress marker as onboarding has either been cancelled or completed + onboardingInProgress.delete(networkChannel.id); + // if user cancelled onboarding or didn't click any buttons, stop here + if (!onboardingStatus) return; let createdConnection; try { @@ -73,16 +76,11 @@ export = { }); const numOfConnections = await connectedList.count({ where: { hub: { id: hub.id } } }); - if (numOfConnections > 1) { - await networkChannel?.send(`This channel has been connected with ${hub.name}.`); - } - else { - await networkChannel?.send(`This channel has been connected with ${hub.name}. ${ - numOfConnections > 1 - ? `You are currently with ${numOfConnections - 1} other servers, Enjoy! ${emoji.clipart}` - : `It seems no one else is there currently... *cricket noises* ${emoji.clipart}` - }`); - } + await networkChannel?.send(`This channel has been connected with **${hub.name}**. ${ + numOfConnections > 1 + ? `You are currently with ${numOfConnections - 1} other servers, Enjoy! ${emoji.clipart}` + : `It seems no one else is there currently... *cricket noises* ${emoji.clipart}` + }`); } // eslint-disable-next-line @typescript-eslint/no-explicit-any catch (err: any) { @@ -104,15 +102,14 @@ export = { return; } - // dispose of the in-progress marker - onboardingInProgress.delete(networkChannel.id); - interaction.client.sendInNetwork(stripIndents` A new server has joined us! ${emoji.clipart} **Server Name:** __${interaction.guild?.name}__ **Member Count:** __${interaction.guild?.memberCount}__ `, { id: hub.id }); - return createdConnection; // just a marker to show that the setup was successful + + // return the created connection so we can use it in the next step + return createdConnection; }, }; diff --git a/src/Scripts/network/onboarding.ts b/src/Scripts/network/onboarding.ts index be923257..5e713661 100644 --- a/src/Scripts/network/onboarding.ts +++ b/src/Scripts/network/onboarding.ts @@ -4,17 +4,17 @@ import { colors, rulesEmbed } from '../../Utils/functions/utils'; /* Make user accept and understand important info on first setup */ export default { - async execute(interaction: ChatInputCommandInteraction) { + async execute(interaction: ChatInputCommandInteraction, hubName: string) { const embed = new EmbedBuilder() - .setTitle('👋 Hey there! Welcome to the InterChat network.') + .setTitle(`👋 Hey there! Welcome to ${hubName}!`) .setDescription(stripIndents` - To keep things organized, it's recommended to create a separate channel for the network. But don't worry, you can always change this later. + To keep things organized, it's recommended to use a separate channel for just for this hub. But don't worry, you can always change this later. - Before we dive in, take a moment to review our network rules. We want everyone to have a smooth and fun experience. + Before we dive in, take a moment to review our rules. We want everyone to have a smooth and fun experience. - **How it works:** the InterChat Network is like a magic bridge that links channels on different servers. So, you can chat with people from all over! + **How it works:** the InterChat Network is like a magic bridge that links channels on different servers that are with us in this hub. So, you can chat with people from all over! - And hey, if you have any cool ideas for new features, let us know! We're always looking to improve. + Developer Note: And hey, if you have any cool ideas for new features, let us know! We're always looking to improve. `) .setColor(colors('chatbot')) .setFooter({ text: `InterChat Network | Version ${interaction.client.version}` });