forked from syntaxbullet/AuroraBot-discord
feat: Implement welcome messages for new enrollments using a new webhook utility and refactor the admin webhook command to utilize it.
This commit is contained in:
@@ -47,6 +47,8 @@ export interface GameConfigType {
|
||||
};
|
||||
studentRole: string;
|
||||
visitorRole: string;
|
||||
welcomeChannelId?: string;
|
||||
welcomeMessage?: string;
|
||||
}
|
||||
|
||||
// Initial default config state
|
||||
@@ -106,7 +108,9 @@ const configSchema = z.object({
|
||||
|
||||
}),
|
||||
studentRole: z.string(),
|
||||
visitorRole: z.string()
|
||||
visitorRole: z.string(),
|
||||
welcomeChannelId: z.string().optional(),
|
||||
welcomeMessage: z.string().optional()
|
||||
});
|
||||
|
||||
export function reloadConfig() {
|
||||
@@ -139,6 +143,8 @@ export function reloadConfig() {
|
||||
config.lootdrop = rawConfig.lootdrop;
|
||||
config.studentRole = rawConfig.studentRole;
|
||||
config.visitorRole = rawConfig.visitorRole;
|
||||
config.welcomeChannelId = rawConfig.welcomeChannelId;
|
||||
config.welcomeMessage = rawConfig.welcomeMessage;
|
||||
|
||||
console.log("🔄 Config reloaded from disk.");
|
||||
}
|
||||
|
||||
56
src/lib/webhookUtils.ts
Normal file
56
src/lib/webhookUtils.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { type TextBasedChannel, User, Client } from 'discord.js';
|
||||
|
||||
/**
|
||||
* Sends a message to a channel using a temporary webhook (imitating the bot or custom persona).
|
||||
*
|
||||
* @param channel The channel to send the message to (must support webhooks).
|
||||
* @param payload The message payload (string content or JSON object for embeds/options).
|
||||
* @param clientUser The client user (bot) to fallback for avatar/name if not specified in payload.
|
||||
* @param reason The reason for creating the webhook (for audit logs).
|
||||
*/
|
||||
export async function sendWebhookMessage(
|
||||
channel: TextBasedChannel,
|
||||
payload: any,
|
||||
clientUser: User,
|
||||
reason: string
|
||||
): Promise<void> {
|
||||
|
||||
if (!('createWebhook' in channel)) {
|
||||
throw new Error("Channel does not support webhooks.");
|
||||
}
|
||||
|
||||
// Normalize payload if it's just a string, wrap it in content
|
||||
if (typeof payload === 'string') {
|
||||
payload = { content: payload };
|
||||
}
|
||||
|
||||
let webhook;
|
||||
try {
|
||||
webhook = await channel.createWebhook({
|
||||
name: payload.username || `${clientUser.username}`, // Use payload name or bot name
|
||||
avatar: payload.avatar_url || payload.avatarURL || clientUser.displayAvatarURL(),
|
||||
reason: reason
|
||||
});
|
||||
|
||||
// Support snake_case keys for raw API compatibility if passed from config
|
||||
if (payload.avatar_url && !payload.avatarURL) {
|
||||
payload.avatarURL = payload.avatar_url;
|
||||
delete payload.avatar_url;
|
||||
}
|
||||
|
||||
await webhook.send(payload);
|
||||
|
||||
await webhook.delete(reason);
|
||||
|
||||
} catch (error) {
|
||||
// Attempt cleanup if webhook was created but sending failed
|
||||
if (webhook) {
|
||||
try {
|
||||
await webhook.delete("Cleanup after failure");
|
||||
} catch (cleanupError) {
|
||||
console.error("Failed to delete webhook during cleanup:", cleanupError);
|
||||
}
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user