Skip to content
This repository has been archived by the owner on Apr 23, 2023. It is now read-only.

Commit

Permalink
Merge pull request #32 from DynamicaBot/sebasptsch/issue19
Browse files Browse the repository at this point in the history
Channel Join Requests
  • Loading branch information
sebasptsch authored Dec 20, 2021
2 parents 10bdb9c + 83b6974 commit cc90c54
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 2 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"scripts": {
"start": "node dist/index.js",
"dev": "nodemon src/index.ts --include src/",
"dev": "nodemon src/bot.ts --include src/",
"build": "tsc -p .",
"lint": "prettier --write ./src",
"postinstall": "prisma generate",
Expand All @@ -42,4 +42,4 @@
"ts-node": "^10.4.0",
"typescript": "^4.5.4"
}
}
}
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;
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ model Guild {
primaryChannels Primary[]
secondaryChannels Secondary[]
textChannelsEnabled Boolean @default(false)
allowJoinRequests Boolean @default(false)
aliases Alias[] @relation("AliasOnGuild")
}

Expand Down
46 changes: 46 additions & 0 deletions src/commands/allowjoin.ts
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"`),
],
});
},
};
2 changes: 2 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { about } from "./about";
export { alias } from "./alias";
export { allowjoin } from "./allowjoin";
export { allyourbase } from "./allyourbase";
export { avc } from "./avc";
export { bitrate } from "./bitrate";
Expand All @@ -8,6 +9,7 @@ export { general } from "./general";
export { help } from "./help";
export { info } from "./info";
export { invite } from "./invite";
export { join } from "./join";
export { limit } from "./limit";
export { lock } from "./lock";
export { name } from "./name";
Expand Down
108 changes: 108 additions & 0 deletions src/commands/join.ts
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");
}
});
},
};

0 comments on commit cc90c54

Please sign in to comment.