This repository has been archived by the owner on Apr 23, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from DynamicaBot/sebasptsch/issue19
Channel Join Requests
- Loading branch information
Showing
6 changed files
with
172 additions
and
2 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
13 changes: 13 additions & 0 deletions
13
prisma/migrations/20211220103416_add_joinrequestoption/migration.sql
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,13 @@ | ||
-- RedefineTables | ||
PRAGMA foreign_keys=OFF; | ||
CREATE TABLE "new_Guild" ( | ||
"id" TEXT NOT NULL PRIMARY KEY, | ||
"textChannelsEnabled" BOOLEAN NOT NULL DEFAULT false, | ||
"allowJoinRequests" BOOLEAN NOT NULL DEFAULT false | ||
); | ||
INSERT INTO "new_Guild" ("id", "textChannelsEnabled") SELECT "id", "textChannelsEnabled" FROM "Guild"; | ||
DROP TABLE "Guild"; | ||
ALTER TABLE "new_Guild" RENAME TO "Guild"; | ||
CREATE UNIQUE INDEX "Guild_id_key" ON "Guild"("id"); | ||
PRAGMA foreign_key_check; | ||
PRAGMA foreign_keys=ON; |
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,46 @@ | ||
import { SlashCommandBuilder } from "@discordjs/builders"; | ||
import { checkPermissions } from "../lib/checks/permissions"; | ||
import { ErrorEmbed, SuccessEmbed } from "../lib/discordEmbeds"; | ||
import { db } from "../lib/prisma"; | ||
import { Command } from "./command"; | ||
|
||
export const allowjoin: Command = { | ||
data: new SlashCommandBuilder() | ||
.setName("allowjoin") | ||
.setDescription("Allow users to request to join a locked channel.") | ||
.addBooleanOption((option) => | ||
option | ||
.setName("state") | ||
.setDescription("Whether to enable or disable join requests.") | ||
.setRequired(true) | ||
), | ||
async execute(interaction) { | ||
if (!interaction.guild) { | ||
interaction.reply({ | ||
embeds: [ErrorEmbed("Must be in a valid guild.")], | ||
ephemeral: true, | ||
}); | ||
return; | ||
} | ||
if (!(await checkPermissions(interaction))) { | ||
interaction.reply({ | ||
embeds: [ErrorEmbed("You don't have permission.")], | ||
ephemeral: true, | ||
}); | ||
return; | ||
} | ||
const state = interaction.options.getBoolean("state", true); | ||
await db.guild.update({ | ||
where: { id: interaction.id }, | ||
data: { | ||
allowJoinRequests: state, | ||
}, | ||
}); | ||
await interaction.reply({ | ||
ephemeral: true, | ||
embeds: [ | ||
SuccessEmbed(`${state ? "Disabled" : "Enabled"} Join Requests"`), | ||
], | ||
}); | ||
}, | ||
}; |
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,108 @@ | ||
import { SlashCommandBuilder } from "@discordjs/builders"; | ||
import { | ||
CommandInteraction, | ||
MessageActionRow, | ||
MessageButton, | ||
} from "discord.js"; | ||
import { ErrorEmbed, SuccessEmbed } from "../lib/discordEmbeds"; | ||
import { getChannel } from "../lib/getCached"; | ||
import { db } from "../lib/prisma"; | ||
import { Command } from "./command"; | ||
|
||
export const join: Command = { | ||
data: new SlashCommandBuilder() | ||
.setName("join") | ||
.setDescription(`Request to join a locked voice channel.`) | ||
.addChannelOption((options) => | ||
options | ||
.addChannelType(2) | ||
.setRequired(true) | ||
.setName("channel") | ||
.setDescription("The channel you wish to join.") | ||
), | ||
async execute(interaction: CommandInteraction) { | ||
const channel = interaction.options.getChannel("channel", true); | ||
const channelConfig = await db.secondary.findUnique({ | ||
where: { id: channel.id }, | ||
include: { guild: true }, | ||
}); | ||
if (!channelConfig) { | ||
interaction.reply("Not a valid Dynamica channel."); | ||
return; | ||
} | ||
if (!channelConfig.guild.allowJoinRequests) { | ||
interaction.reply({ | ||
ephemeral: true, | ||
embeds: [ErrorEmbed("Join requests are not enabled on this server.")], | ||
}); | ||
return; | ||
} | ||
|
||
const { creator } = channelConfig; | ||
|
||
const row = new MessageActionRow().addComponents( | ||
new MessageButton({ | ||
customId: "channeljoinaccept", | ||
style: "SUCCESS", | ||
label: "Allow", | ||
}), | ||
new MessageButton({ | ||
customId: "channeljoindeny", | ||
style: "DANGER", | ||
label: "Deny", | ||
}) | ||
); | ||
interaction.reply({ | ||
components: [row], | ||
content: `Does <@${interaction.user.id}> have permission to join <#${channel.id}> ? As the creator <@${creator}>, are they allowed to join?`, | ||
}); | ||
interaction.channel | ||
.createMessageComponentCollector({ componentType: "BUTTON" }) | ||
.once("collect", async (collected) => { | ||
if (collected.user.id !== channelConfig.creator) { | ||
collected.reply({ | ||
ephemeral: true, | ||
embeds: [ | ||
SuccessEmbed("You're not the user who created the channel."), | ||
], | ||
}); | ||
return; | ||
} | ||
const button = collected; | ||
if (button.customId === "channeljoinaccept") { | ||
const discordChannel = await getChannel( | ||
interaction.guild.channels, | ||
channel.id | ||
); | ||
if (!discordChannel.isVoice()) return; | ||
|
||
await discordChannel.permissionOverwrites.create(interaction.user, { | ||
CONNECT: true, | ||
}); | ||
await interaction.editReply({ | ||
content: null, | ||
components: [], | ||
embeds: [ | ||
SuccessEmbed("You have been granted access to the channel."), | ||
], | ||
}); | ||
await collected.reply({ | ||
ephemeral: true, | ||
embeds: [SuccessEmbed("You have granted access to the channel.")], | ||
}); | ||
} else if (button.customId === "channeljoindeny") { | ||
await interaction.editReply({ | ||
content: null, | ||
components: [], | ||
embeds: [ErrorEmbed("You have been denied access to the channel.")], | ||
}); | ||
await collected.reply({ | ||
embeds: [SuccessEmbed("You have denied access to the channel.")], | ||
ephemeral: true, | ||
}); | ||
} else { | ||
interaction.reply("Wrong button"); | ||
} | ||
}); | ||
}, | ||
}; |