forked from tabarra/txAdmin
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bot): added whitelist commands + helpers
- Loading branch information
Showing
8 changed files
with
237 additions
and
47 deletions.
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
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,103 @@ | ||
const modulename = 'DiscordBot:cmd:whitelist'; | ||
import { ColorResolvable, CommandInteraction, GuildMember, MessageEmbed, StaticImageURLOptions } from 'discord.js'; | ||
import logger from '@core/extras/console.js'; | ||
import TxAdmin from '@core/txAdmin'; | ||
import { now } from '@core/extras/helpers'; | ||
import { DuplicateKeyError } from '@core/components/PlayerDatabase'; | ||
import { embedder, ensurePermission, logDiscordAdminAction } from '../discordHelpers'; | ||
const { dir, log, logOk, logWarn, logError } = logger(modulename); | ||
|
||
|
||
/** | ||
* Command /whitelist member <mention> | ||
*/ | ||
const handleMemberSubcommand = async (interaction: CommandInteraction, txAdmin: TxAdmin, adminName: string) => { | ||
const avatarOptions: StaticImageURLOptions = { size: 64 }; | ||
const member = interaction.options.getMember('member', true) as GuildMember; | ||
|
||
//Preparing player id/name/avatar | ||
const identifier = `discord:${member.id}`; | ||
const playerName = `${member.nickname ?? member.user.username}#${member.user.discriminator}`; | ||
const playerAvatar = member.displayAvatarURL(avatarOptions) ?? member.user.displayAvatarURL(avatarOptions); | ||
|
||
//Registering approval | ||
try { | ||
txAdmin.playerDatabase.registerWhitelistApprovals({ | ||
identifier, | ||
playerName, | ||
playerAvatar, | ||
tsApproved: now(), | ||
approvedBy: adminName, | ||
}); | ||
} catch (error) { | ||
return await interaction.reply(embedder.danger(`Failed to save whitelist approval: ${(error as Error).message}`)); | ||
} | ||
|
||
const msg = `Added whitelist approval for ${playerName}.`; | ||
logDiscordAdminAction(txAdmin, adminName, msg); | ||
return await interaction.reply(embedder.success(msg)); | ||
} | ||
|
||
|
||
/** | ||
* Command /whitelist request <id> | ||
*/ | ||
const handleRequestSubcommand = async (interaction: CommandInteraction, txAdmin: TxAdmin, adminName: string) => { | ||
const input = interaction.options.getString('id', true); | ||
const reqId = input.trim().toUpperCase(); | ||
if (reqId.length !== 5 || reqId[0] !== 'R') { | ||
return await interaction.reply(embedder.danger('Invalid request ID.')); | ||
} | ||
|
||
//Find request | ||
const requests = txAdmin.playerDatabase.getWhitelistRequests({ id: reqId }); | ||
if (!requests.length) { | ||
return await interaction.reply(embedder.warning(`Whitelist request ID \`${reqId}\` not found.`)); | ||
} | ||
const req = requests[0]; //just getting the first | ||
|
||
//Register whitelistApprovals | ||
const playerName = req.discordTag ?? req.playerDisplayName; | ||
try { | ||
txAdmin.playerDatabase.registerWhitelistApprovals({ | ||
identifier: `license:${req.license}`, | ||
playerName, | ||
playerAvatar: (req.discordAvatar) ? req.discordAvatar : null, | ||
tsApproved: now(), | ||
approvedBy: adminName, | ||
}); | ||
} catch (error) { | ||
if (!(error instanceof DuplicateKeyError)) { | ||
return await interaction.reply(embedder.danger(`Failed to save wl approval: ${(error as Error).message}`)); | ||
} | ||
} | ||
|
||
//Remove record from whitelistRequests | ||
try { | ||
txAdmin.playerDatabase.removeWhitelistRequests({ id: reqId }); | ||
} catch (error) { | ||
return await interaction.reply(embedder.danger(`Failed to remove wl request: ${(error as Error).message}`)); | ||
} | ||
|
||
const msg = `Approved whitelist request \`${reqId}\` from ${playerName}.`; | ||
logDiscordAdminAction(txAdmin, adminName, msg); | ||
return await interaction.reply(embedder.success(msg)); | ||
} | ||
|
||
|
||
/** | ||
* Handler for /whitelist | ||
*/ | ||
export default async (interaction: CommandInteraction, txAdmin: TxAdmin) => { | ||
//Check permissions | ||
const adminName = await ensurePermission(interaction, txAdmin, 'players.whitelist'); | ||
if (typeof adminName !== 'string') return; | ||
|
||
const subcommand = interaction.options.getSubcommand(); | ||
if (subcommand === 'member') { | ||
return await handleMemberSubcommand(interaction, txAdmin, adminName); | ||
} else if (subcommand === 'request') { | ||
return await handleRequestSubcommand(interaction, txAdmin, adminName); | ||
} | ||
throw new Error(`Subcommand ${subcommand} not found.`); | ||
} |
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,64 @@ | ||
const modulename = 'DiscordBot:cmd'; | ||
import logger from '@core/extras/console.js'; | ||
import TxAdmin from "@core/txAdmin"; | ||
import { ColorResolvable, CommandInteraction, MessageEmbed } from "discord.js"; | ||
const { dir, log, logOk, logWarn, logError } = logger(modulename); | ||
|
||
|
||
/** | ||
* Generic embed generation functions | ||
*/ | ||
const genericEmbed = (msg: string, ephemeral = false, color?: ColorResolvable, emoji?: string) => { | ||
return { | ||
ephemeral, | ||
embeds: [new MessageEmbed({ | ||
color, | ||
description: emoji ? `:${emoji}: ${msg}` : msg, | ||
})], | ||
} | ||
} | ||
|
||
export const embedder = { | ||
generic: genericEmbed, | ||
success: (msg: string, ephemeral = false) => genericEmbed(msg, ephemeral, '#0BA70B', 'white_check_mark'), | ||
warning: (msg: string, ephemeral = false) => genericEmbed(msg, ephemeral, '#FFF100', 'warning'), | ||
danger: (msg: string, ephemeral = false) => genericEmbed(msg, ephemeral, '#A70B28', 'no_entry_sign'), | ||
} | ||
|
||
|
||
/** | ||
* Ensure that the discord interaction author has the required permission | ||
*/ | ||
export const ensurePermission = async (interaction: CommandInteraction, txAdmin: TxAdmin, reqPerm: string) => { | ||
const admin = txAdmin.adminVault.getAdminByProviderUID(interaction.user.id); | ||
if (!admin) { | ||
await interaction.reply( | ||
embedder.warning('Your Discord ID is not registered in txAdmin :face_with_monocle:', true) | ||
); | ||
return false; | ||
} | ||
if ( | ||
admin.master !== true | ||
&& !admin.permissions.includes('all_permissions') | ||
&& !admin.permissions.includes(reqPerm) | ||
) { | ||
//@ts-ignore: not important | ||
const permName = txAdmin.adminVault.registeredPermissions[reqPerm] ?? 'Unknown'; | ||
await interaction.reply( | ||
embedder.danger(`You do not have the "${permName}" permissions.`, true) | ||
); | ||
return false; | ||
} | ||
|
||
return admin.name; | ||
} | ||
|
||
|
||
/** | ||
* Equivalent to ctx.utils.logAction() | ||
*/ | ||
export const logDiscordAdminAction = async (txAdmin: TxAdmin, adminName: string, message: string) => { | ||
const logMessage = `[${adminName}] ${message}`; | ||
log(logMessage); | ||
txAdmin.logger.admin.write(logMessage); | ||
} |
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
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
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
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
Oops, something went wrong.