Skip to content
This repository was archived by the owner on Apr 20, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions src/rest/commands/general/follow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import Command, {
basicInteractionResponse,
} from "../../common/command/Command.ts";
import { bot } from "../../cache.ts";
import { enTranslate, translate } from "../../common/util/i18next.ts";
import {
DiscordApplicationCommandOptionTypes,
followChannel,
getChannelWebhooks,
snowflakeToBigint,
validatePermissions,
} from "../../../../deps.ts";

const FollowCommand: Command = async (interaction) => {
const guildId: bigint = snowflakeToBigint(interaction.guildId!);

if (
!validatePermissions(snowflakeToBigint(interaction.member!.permissions), [
"MANAGE_WEBHOOKS",
])
) {
return basicInteractionResponse(
interaction.id,
interaction.token,
translate(guildId, "permission:USER_MISSING_PERMISSION", {
permission: "Manage Webhooks",
}),
);
}

const raw = interaction.data?.options?.[0];

if (raw?.name === "announcements") {
const channelId: bigint = snowflakeToBigint(interaction.channelId!);

const channelWebhooks = await getChannelWebhooks(
channelId,
);

if (channelWebhooks.size >= 10) {
return basicInteractionResponse(
interaction.id,
interaction.token,
translate(guildId, "commands/general/follow:WEBHOOK_LIMIT"),
);
}

try {
// 268559149175013376 is the ID of our announcement channel.
await followChannel(snowflakeToBigint("268559149175013376"), channelId);
} catch {
return basicInteractionResponse(
interaction.id,
interaction.token,
translate(guildId, "permission:SELF_MISSING_PERMISSION", {
permission: "Manage Webhooks",
}),
);
}

return basicInteractionResponse(
interaction.id,
interaction.token,
translate(guildId, "commands/general/follow:ANNOUNCEMENTS_FOLLOWED"),
);
} else if (raw?.name === "status") {
const channelId: bigint = snowflakeToBigint(interaction.channelId!);

const channelWebhooks = await getChannelWebhooks(
channelId,
);

if (channelWebhooks.size >= 10) {
return basicInteractionResponse(
interaction.id,
interaction.token,
translate(guildId, "commands/general/follow:WEBHOOK_LIMIT"),
);
}

try {
// 621817852726607882 is the ID of our status channel.
await followChannel(snowflakeToBigint("621817852726607882"), channelId);
} catch {
return basicInteractionResponse(
interaction.id,
interaction.token,
translate(guildId, "permission:SELF_MISSING_PERMISSION", {
permission: "Manage Webhooks",
}),
);
}

return basicInteractionResponse(
interaction.id,
interaction.token,
translate(guildId, "commands/general/follow:STATUS_FOLLOWED"),
);
} else {
return basicInteractionResponse(
interaction.id,
interaction.token,
translate(guildId, "commands/general/follow:INVALID_ARGUMENT"),
);
}
};

FollowCommand.options = {
name: "follow",
description: enTranslate("commands/general/follow:COMMAND_DESCRIPTION"),
options: [
{
required: false,
name: "announcements",
description: enTranslate(
"commands/general/follow:SUBCOMMAND_DESCRIPTION_ANNOUNCEMENTS",
),
type: DiscordApplicationCommandOptionTypes.SubCommand,
},
{
required: false,
name: "status",
description: enTranslate(
"commands/general/follow:SUBCOMMAND_DESCRIPTION_STATUS",
),
type: DiscordApplicationCommandOptionTypes.SubCommand,
},
],
};

bot.commands.add(FollowCommand);
9 changes: 9 additions & 0 deletions src/rest/languages/en_US/commands/general/follow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"COMMAND_DESCRIPTION": "Creates a webhook to follow TypicalBot announcements or status updates in this channel.",
"SUBCOMMAND_DESCRIPTION_ANNOUNCEMENTS": "Follow TypicalBot announcements in this channel.",
"SUBCOMMAND_DESCRIPTION_STATUS": "Follow TypicalBot status updates in this channel.",
"INVALID_ARGUMENT": "Please specify which TypicalBot channel you want to follow.",
"WEBHOOK_LIMIT": "The maximum webhooks that Discord allows in one channel is 10. Please remove a webhook and try again.",
"ANNOUNCEMENTS_FOLLOWED": "You have successfully followed TypicalBot announcements in this channel.",
"STATUS_FOLLOWED": "You have successfully followed TypicalBot status updates in this channel."
}