Skip to content

Commit

Permalink
Add nickname change event for OOO (#318)
Browse files Browse the repository at this point in the history
- fixes THESIS-101

### Notes
This is the main working PR for building out the nickname changer as
setup by a webhook call into Valkyrie. It's hardcoded to have OOO, which
we can also alter to pass rather than embed.

### To-Do

- [x] Setup initial flow to map nickname
- [x] Map userID to matched nickname
  • Loading branch information
Shadowfiend authored Nov 1, 2024
2 parents 7e6940f + 81d534b commit e0db762
Showing 1 changed file with 101 additions and 12 deletions.
113 changes: 101 additions & 12 deletions discord-scripts/discord-webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,82 @@ export default async function webhookDiscord(
}
}

async function getUserIdByName(
guildId: string,
username: string,
): Promise<string | null> {
const guild = discordClient.guilds.cache.get(guildId)
if (!guild) throw new Error("Guild not found")

await guild.members.fetch()
// WIP, output list of all members for matching
guild.members.cache.forEach((member) => {
robot.logger.info(`Username: ${member.user.username}`)
})

const matchedMember = guild.members.cache.find((member) =>
member.user.username.includes(username),
)

return matchedMember ? matchedMember.user.id : null
}

async function updateServerNickname(
username: string,
guildId: string,
addSuffix: boolean,
date?: string,
) {
const guild = discordClient.guilds.cache.get(guildId)
if (!guild) throw new Error("Guild not found")

const userId = await getUserIdByName(guildId, username)
if (!userId) throw new Error("User not found with the specified name")

const member = await guild.members.fetch(userId)
const currentNickname = member.nickname || member.user.username

const suffixWithDate = date ? `(OOO ${date})` : "(OOO)"
const suffixRegex = /\s*\(OOO.*$/

const newNickname = addSuffix
? `${currentNickname
.replace(suffixRegex, "")
.trim()} ${suffixWithDate}`.trim()
: currentNickname.replace(suffixRegex, "").trim()

if (newNickname !== currentNickname) {
await member.setNickname(newNickname)
robot.logger.info(
`${addSuffix ? "Added" : "Removed"} '${suffixWithDate}' for ${
member.user.username
} in ${guild.name}`,
)
}
}

if (process.env.HUBOT_WEBHOOK_URL) {
const webhookUrl = process.env.HUBOT_WEBHOOK_URL
const requiredAuth = process.env.HUBOT_WEBHOOK_AUTH
robot.logger.info("Webhook URL has been set: ", webhookUrl)
robot.logger.info("Webhook Auth has been set: ", requiredAuth)

const handleAuth = (
req: express.Request,
res: express.Response,
next: express.NextFunction,
) => {
const authHeader = req.headers.authorization
if (!authHeader || authHeader !== requiredAuth) {
res.status(401).send("Unauthorized")
} else {
next()
}
}

robot.router.post(
`${webhookUrl}`,
(
req: express.Request,
res: express.Response,
next: express.NextFunction,
) => {
const authHeader = req.headers.authorization
if (!authHeader || authHeader !== requiredAuth) {
res.status(401).send("Unauthorized")
} else {
next()
}
},
handleAuth,
async (req: express.Request, res: express.Response) => {
const isBodyInvalid = ["channelName", "title", "message"].some(
(field) => {
Expand Down Expand Up @@ -97,5 +154,37 @@ export default async function webhookDiscord(
},
)
robot.logger.info("Webhook is now enabled")

robot.router.post("/start-date", handleAuth, async (req, res) => {
try {
const { username, guildId, date } = req.body
if (!username || !guildId) {
return res.status(400).send("Missing username or guildId")
}
await updateServerNickname(username, guildId, true, date)
return res.status(200).send("Nickname updated to add (OOO)")
} catch (error) {
robot.logger.error("Error in start-date route:", error)
return res.status(500).send("Internal Server Error")
}
})

robot.router.post(
"/end-date",
handleAuth,
async (req: express.Request, res: express.Response) => {
const { username, guildId } = req.body
if (!username || !guildId) {
return res.status(400).send("Missing username or guildId")
}

await updateServerNickname(username, guildId, false)
return res.status(200).send("Nickname updated to remove (OOO)")
},
)

robot.logger.info(
"Webhook is now enabled with OOO routes /start-date and /end-date",
)
}
}

0 comments on commit e0db762

Please sign in to comment.