Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Focus mode + Role management #290

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions discord-scripts/focus-mode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import {
ChannelType,
Client,
ButtonStyle,
ButtonBuilder,
ActionRowBuilder,
TextChannel,
Message,
} from "discord.js"
import { Robot } from "hubot"
import manageRole from "./role-management/index.ts"

const focusModeState: Map<string, boolean> = new Map()

async function sendFocusModeMessage(
focusChannel: TextChannel,
guildId: string,
robot: Robot,
originalMessage?: Message,
) {
const isEnabled = focusModeState.get(guildId) || false

const actionRow = new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder()
.setCustomId("enable-focus")
.setLabel("Enable Focus")
.setStyle(ButtonStyle.Success)
.setDisabled(isEnabled),
new ButtonBuilder()
.setCustomId("disable-focus")
.setLabel("Disable Focus")
.setStyle(ButtonStyle.Danger)
.setDisabled(!isEnabled),
)

const messageContent = {
content:
"**Focus Mode**\nSelect an option to enable or disable focus mode.",
components: [actionRow],
}

if (originalMessage) {
await originalMessage.edit(messageContent)
} else {
await focusChannel.send(messageContent)
}

robot.logger.info(
`Focus mode options updated in the focus channel of guild: ${guildId}`,
)
}

export default async function setupFocusChannels(
discordClient: Client,
robot: Robot,
) {
const { application } = discordClient

if (application) {
robot.logger.info("Discord client ready, setting up focus channels.")

discordClient.guilds.cache.forEach(async (guild) => {
const focusChannels = guild.channels.cache.filter(
(channel) =>
channel.type === ChannelType.GuildText &&
channel.name.startsWith("focus"),
)

focusChannels.forEach(async (channel) => {
const focusChannel = channel as TextChannel
const guildId = guild.id

if (!focusModeState.has(guildId)) {
focusModeState.set(guildId, false)
robot.logger.info(
`Initializing focus mode state for guild: ${guild.name}`,
)
}

robot.logger.info(
`Setting up focus mode for channel: ${channel.name} in guild: ${guild.name}`,
)
await sendFocusModeMessage(focusChannel, guildId, robot)
})
})
}

discordClient.on("interactionCreate", async (interaction) => {
if (!interaction.isButton()) return

const guildId = interaction.guildId

Check failure on line 91 in discord-scripts/focus-mode.ts

View workflow job for this annotation

GitHub Actions / lint

Use object destructuring
if (!guildId || !interaction.member) return

let isEnabled = focusModeState.get(guildId) || false

if (interaction.customId === "enable-focus") {
isEnabled = true
await interaction.reply({
content: "Focus mode enabled.",
ephemeral: true,
})
await manageRole(
discordClient,
guildId,
interaction.member.user.id,
"1205116222161813545",
"add",
)
} else if (interaction.customId === "disable-focus") {
isEnabled = false
await interaction.reply({
content: "Focus mode disabled.",
ephemeral: true,
})
await manageRole(
discordClient,
guildId,
interaction.member.user.id,
"1205116222161813545",
"remove",
)
}

focusModeState.set(guildId, isEnabled)
if (interaction.message instanceof Message) {
sendFocusModeMessage(
interaction.channel as TextChannel,
guildId,
robot,
interaction.message,
)
}
})
}
34 changes: 34 additions & 0 deletions discord-scripts/role-management/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Client } from "discord.js"

export default async function manageRole(
discordClient: Client,
guildId: string,
memberId: string,
roleId: string,
action: "add" | "remove",
): Promise<void> {
if (!discordClient) {
throw new Error("Discord client is not initialized.")
}

const guild = await discordClient.guilds.fetch(guildId)
if (!guild) throw new Error("Guild not found.")

const member = await guild.members.fetch(memberId)
if (!member) throw new Error("Member not found.")

const role = await guild.roles.fetch(roleId)
if (!role) throw new Error("Role not found.")

if (action === "add") {
if (member.roles.cache.has(roleId)) {
return
}
await member.roles.add(role)
} else if (action === "remove") {
if (!member.roles.cache.has(roleId)) {
return
}
await member.roles.remove(role)
}
}
Loading