-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(webhook): add support for webhooks in guilds
- Loading branch information
Showing
8 changed files
with
218 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
import { type CommandOptions, Command } from "@sapphire/framework"; | ||
import { ApplyOptions } from "@sapphire/decorators"; | ||
import { EmbedBuilder, codeBlock, InteractionContextType, ChannelType, PermissionFlagsBits } from "discord.js"; | ||
import { isNullOrUndefinedOrEmpty } from "@sapphire/utilities"; | ||
import { PayloadCommand } from "#lib/structs/commands/PayloadCommand"; | ||
import { LanguageKeys } from "#lib/i18n/all"; | ||
import PayloadColors from "#utils/colors"; | ||
import { guild, webhook } from "#root/drizzle/schema"; | ||
import { eq } from "drizzle-orm"; | ||
import { fetchT, getLocalizedData } from "@sapphire/plugin-i18next"; | ||
import { generate } from "generate-password"; | ||
import { sendTest } from "#lib/api/utils/webhook-helper"; | ||
|
||
@ApplyOptions<CommandOptions>({ | ||
description: LanguageKeys.Commands.Webhook.Description, | ||
detailedDescription: LanguageKeys.Commands.Webhook.DetailedDescription, | ||
}) | ||
export class UserCommand extends PayloadCommand { | ||
async chatInputRun(interaction: Command.ChatInputCommandInteraction) { | ||
const t = await fetchT(interaction); | ||
const { client } = this.container; | ||
|
||
const [guildWebhook] = await this.database | ||
.select({ webhookId: guild.webhookId }) | ||
.from(guild) | ||
.where(eq(guild.id, interaction.guildId)); | ||
|
||
switch (interaction.commandName) { | ||
case "add": { | ||
const channel = interaction.options.getChannel("channel"); | ||
|
||
const didSucceed = await sendTest(this.container.client, "channels", channel?.id); | ||
|
||
if (!didSucceed) { | ||
const embed = new EmbedBuilder({ | ||
author: { | ||
name: client.user.username, | ||
iconURL: client.user.displayAvatarURL(), | ||
}, | ||
title: t(LanguageKeys.Commands.Webhook.EmbedTitle), | ||
description: t(LanguageKeys.Commands.GuildWebhook.AddFailed), | ||
color: PayloadColors.Payload, | ||
}); | ||
|
||
await interaction.reply({ embeds: [embed], ephemeral: true }); | ||
|
||
return; | ||
} | ||
|
||
const secret = generate({ numbers: true, length: 24 }); | ||
|
||
const [createdWebhook] = await this.database | ||
.insert(webhook) | ||
.values({ | ||
id: interaction.guildId, | ||
type: "channels", | ||
value: secret, | ||
}) | ||
.returning(); | ||
|
||
await this.database | ||
.insert(guild) | ||
.values({ | ||
id: channel.id, | ||
webhookId: createdWebhook.id, | ||
}) | ||
.onConflictDoUpdate({ | ||
set: { | ||
webhookId: createdWebhook.id, | ||
}, | ||
target: guild.id, | ||
}); | ||
|
||
const embed = new EmbedBuilder({ | ||
title: t(LanguageKeys.Commands.Webhook.EmbedTitle), | ||
description: codeBlock(createdWebhook.value), | ||
color: PayloadColors.Payload, | ||
}); | ||
|
||
await interaction.reply({ embeds: [embed], ephemeral: true }); | ||
|
||
return; | ||
} | ||
case "delete": { | ||
await this.container.database.delete(webhook).where(eq(webhook.id, interaction.user.id)); | ||
|
||
await interaction.reply({ | ||
content: t(LanguageKeys.Commands.Webhook.DeletedWebhook), | ||
ephemeral: true, | ||
}); | ||
|
||
return; | ||
} | ||
case "show": { | ||
const embed = new EmbedBuilder({ | ||
title: t(LanguageKeys.Commands.Webhook.EmbedTitle), | ||
description: t(LanguageKeys.Commands.Webhook.NoWebhook), | ||
color: PayloadColors.Payload, | ||
}); | ||
|
||
if (!isNullOrUndefinedOrEmpty(guildWebhook)) { | ||
const [wbhk] = await this.database | ||
.select({ value: webhook.value }) | ||
.from(webhook) | ||
.where(eq(webhook.id, guildWebhook.webhookId)); | ||
|
||
embed.setDescription(codeBlock(wbhk.value)); | ||
} | ||
|
||
await interaction.reply({ embeds: [embed], ephemeral: true }); | ||
|
||
return; | ||
} | ||
} | ||
} | ||
|
||
public override registerApplicationCommands(registry: Command.Registry) { | ||
const rootNameLocalizations = getLocalizedData(LanguageKeys.Commands.GuildWebhook.Name); | ||
const rootDescriptionLocalizations = getLocalizedData(this.description); | ||
|
||
const addNameLocalizations = getLocalizedData(LanguageKeys.Commands.GuildWebhook.AddName); | ||
const addDescriptionLocalizations = getLocalizedData(LanguageKeys.Commands.GuildWebhook.AddDescription); | ||
const channelIdNameLocalizations = getLocalizedData(LanguageKeys.Commands.GuildWebhook.AddChannelIdName); | ||
const channelIdDescriptionLocalizations = getLocalizedData( | ||
LanguageKeys.Commands.GuildWebhook.AddChannelIdDescription, | ||
); | ||
|
||
const removeNameLocalizations = getLocalizedData(LanguageKeys.Commands.GuildWebhook.RemoveName); | ||
const removeDescriptionLocalizations = getLocalizedData(LanguageKeys.Commands.GuildWebhook.RemoveDescription); | ||
|
||
const showNameLocalizations = getLocalizedData(LanguageKeys.Commands.GuildWebhook.ShowName); | ||
const showDescriptionLocalizations = getLocalizedData(LanguageKeys.Commands.GuildWebhook.ShowDescription); | ||
|
||
registry.registerChatInputCommand(builder => | ||
builder | ||
.setName(this.name) | ||
.setDescription(rootDescriptionLocalizations.localizations["en-US"]) | ||
.setContexts(InteractionContextType.Guild) | ||
.setDefaultMemberPermissions(PermissionFlagsBits.Administrator) | ||
.setDescriptionLocalizations(rootDescriptionLocalizations.localizations) | ||
.setNameLocalizations(rootNameLocalizations.localizations) | ||
.addSubcommand(sub => | ||
sub | ||
.setName("add") | ||
.setDescription(addDescriptionLocalizations.localizations["en-US"]) | ||
.setNameLocalizations(addNameLocalizations.localizations) | ||
.setDescriptionLocalizations(addDescriptionLocalizations.localizations) | ||
.addChannelOption(input => | ||
input | ||
.setName("channel") | ||
.setDescription(channelIdDescriptionLocalizations.localizations["en-US"]) | ||
.setNameLocalizations(channelIdNameLocalizations.localizations) | ||
.setDescriptionLocalizations(channelIdDescriptionLocalizations.localizations) | ||
.addChannelTypes(ChannelType.GuildText), | ||
), | ||
) | ||
.addSubcommand(sub => | ||
sub | ||
.setName("delete") | ||
.setDescription(removeDescriptionLocalizations.localizations["en-US"]) | ||
.setNameLocalizations(removeNameLocalizations.localizations) | ||
.setDescriptionLocalizations(removeDescriptionLocalizations.localizations), | ||
) | ||
.addSubcommand(sub => | ||
sub | ||
.setName("show") | ||
.setDescription(showDescriptionLocalizations.localizations["en-US"]) | ||
.setNameLocalizations(showNameLocalizations.localizations) | ||
.setDescriptionLocalizations(showDescriptionLocalizations.localizations), | ||
), | ||
); | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
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,17 @@ | ||
{ | ||
"name": "guildwebhook", | ||
"description": "Provides a set of actions to work with the Payload webhook integration", | ||
"detailedDescription": { | ||
"usages": [], | ||
"details": "See more on (Github)[https://github.com/payload-bot/payload-logs-plugin]" | ||
}, | ||
"addName": "add", | ||
"addDescription": "Creates a webhook for a given channel", | ||
"addChannelIdName": "channelid", | ||
"addChannelIdDescription": "The channel id to send webhooks to", | ||
"addFailed": "Failed to create the webhook", | ||
"removeName": "remove", | ||
"removeDescription": "Removes the webhook", | ||
"showName": "show", | ||
"showDescription": "Shows the webhook secret for the channel" | ||
} |
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,15 @@ | ||
import { T } from "#lib/types"; | ||
|
||
export const Name = T<string>("commands/guild-webhook:name"); | ||
export const Description = T<string>("commands/guild-webhook:description"); | ||
export const DetailedDescription = T<string>("commands/guild-webhook:detailedDescription"); | ||
|
||
export const AddName = T<string>("commands/guild-webhook:addName"); | ||
export const AddDescription = T<string>("commands/guild-webhook:addDescription"); | ||
export const AddChannelIdName = T<string>("commands/guild-webhook:addChannelIdName"); | ||
export const AddChannelIdDescription = T<string>("commands/guild-webhook:addChannelIdDescription"); | ||
export const AddFailed = T<string>("commands/guild-webhook:addFailed"); | ||
export const RemoveName = T<string>("commands/guild-webhook:removeName"); | ||
export const RemoveDescription = T<string>("commands/guild-webhook:removeDescription"); | ||
export const ShowName = T<string>("commands/guild-webhook:removeName"); | ||
export const ShowDescription = T<string>("commands/guild-webhook:removeDescription"); |
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