-
Notifications
You must be signed in to change notification settings - Fork 5
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
zuuring
wants to merge
7
commits into
main
Choose a base branch
from
focus-mode
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5b77f7e
Initial commit - Focus mode
zuuring a5192a2
Set button states
zuuring 1cb99f7
Rewrite fules
zuuring c3b7a99
Linter fixes
zuuring f344199
Add role manager
zuuring 1ff4e26
Naming conventions
zuuring 66611ac
Refactor code
zuuring File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import { | ||
ChannelType, | ||
Client, | ||
ButtonStyle, | ||
ButtonBuilder, | ||
ActionRowBuilder, | ||
TextChannel, | ||
Message, | ||
} from "discord.js" | ||
import { Robot } from "hubot" | ||
import manageRole from "./role-management/role-management.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, | ||
) { | ||
discordClient.on("messageCreate", async (message) => { | ||
if (message.author.bot) return | ||
|
||
if ( | ||
message.guild && | ||
message.channel.type === ChannelType.GuildText && | ||
message.channel.name.startsWith("focus") | ||
) { | ||
const guildId = message.guild.id | ||
const focusChannel = message.channel as TextChannel | ||
|
||
if (!focusModeState.has(guildId)) { | ||
focusModeState.set(guildId, false) | ||
robot.logger.info( | ||
`Initializing focus mode state for guild: ${message.guild.name}`, | ||
) | ||
} | ||
|
||
robot.logger.info( | ||
`Message received in focus channel of guild: ${message.guild.name}`, | ||
) | ||
|
||
await sendFocusModeMessage(focusChannel, guildId, robot) | ||
} | ||
}) | ||
|
||
discordClient.on("interactionCreate", async (interaction) => { | ||
if (!interaction.isButton()) return | ||
|
||
const guildId = interaction.guildId | ||
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, | ||
) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a naming note—if we want a subdir, we should have
role-management/index.ts
, think about the subdirectory as a “module”, and whatindex.ts
exports should be the external-facing (i.e. for any other directory including parents) API of the directory. No one else should importrole-management/*
, they should just importrole-management
orrole-management/index.ts
(Those two should be synonyms.)