Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v0.28.0 #651

Merged
merged 4 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions src/modules/voice/applicationCommands/voice.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = class Voice extends ApplicationCommand {
constructor() {

super('voice', {
global : false,
global : true,
directMessage : false,
category : 'voice',
description : 'Manage Hubs and temporary voice channels',
Expand Down Expand Up @@ -47,6 +47,7 @@ module.exports = class Voice extends ApplicationCommand {
required : false,
choices : {
'Public' : 'public',
'Inherit' : 'inherit',
'Locked' : 'locked',
'Private' : 'private'
}
Expand Down Expand Up @@ -115,7 +116,7 @@ module.exports = class Voice extends ApplicationCommand {
}));
}

async createHub(interaction, { name, 'default-size' : defaultSize = 10, 'default-type' : defaultType = 'public' }) {
async createHub(interaction, { name, 'default-size' : defaultSize = 10, 'default-type' : defaultType = 'inherit' }) {

const { HubService } = this.services();

Expand Down
11 changes: 10 additions & 1 deletion src/modules/voice/interactions/control.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Control extends Interaction {

return {
setPublic : { method : 'setPublic', customId : 'voice:control:set_public' },
setInherit : { method : 'setInherit', customId : 'voice:control:set_inherit' },
setLocked : { method : 'setLocked', customId : 'voice:control:set_locked' },
setPrivate : { method : 'setPrivate', customId : 'voice:control:set_private' },

Expand All @@ -33,7 +34,7 @@ class Control extends Interaction {
}

/**
* @param {import('discord.js').MessageComponentInteraction} interaction
* @param {import('discord.js').MessageComponentInteraction} interaction
* @param {function(channel : TemporaryChannel) : Promise<TemporaryChannel|null>} handler
*/
async handle(interaction, handler) {
Expand Down Expand Up @@ -114,6 +115,14 @@ class Control extends Interaction {
return this.changeType(interaction, 'public');
}

/**
* @param {import('discord.js').ButtonInteraction} interaction
**/
setInherit(interaction) {

return this.changeType(interaction, 'inherit');
}

/**
* @param {import('discord.js').ButtonInteraction} interaction
**/
Expand Down
40 changes: 40 additions & 0 deletions src/modules/voice/listeners/channelPermissionUpdated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

const { Events, ChannelType } = require('discord.js');

const { Listener } = require('../../../core');

module.exports = class ChannelPermissionUpdatedListener extends Listener {

constructor() {

super(Events.ChannelUpdate, { emitter : 'client' });
}

/**
* @param {import('discord.js').GuildChannel} oldChannel
* @param {import('discord.js').GuildChannel} newChannel
*/
async exec(oldChannel, newChannel) {

const { TemporaryChannelService } = this.services();

if (!newChannel.guildId) {

return;
}

if (newChannel.type === ChannelType.GuildCategory) {

for (const chanelId of newChannel.children.cache.keys()) {

const channel = await TemporaryChannelService.getTemporaryChannel(newChannel.guildId, chanelId);

if (channel && channel.config.type === 'inherit') {

await TemporaryChannelService.updatePermission(channel);
}
}
}
}
};
76 changes: 52 additions & 24 deletions src/modules/voice/services/temporaryChannel.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

// eslint-disable-next-line no-unused-vars
const { Snowflake, GuildMember, VoiceChannel, PermissionsBitField, ChannelType } = require('discord.js');
const { DiscordAPIError } = require('@discordjs/rest');
const { Snowflake, GuildMember, VoiceChannel, PermissionsBitField, ChannelType, OverwriteData } = require('discord.js');
const { DiscordAPIError } = require('@discordjs/rest');

const { Service, Util } = require('../../../core');

Expand All @@ -14,12 +14,12 @@ class TemporaryChannelService extends Service {

/**
* @typedef {Object} TemporaryChannel
* @property {import('discord.js').GuildVoice} channel
* @property {import('discord.js').VoiceChannel} channel
* @property {import('discord.js').GuildMember} owner
* @property {Object} config
* @property {Snowflake} config.ownerId
* @property {Snowflake} config.messageId
* @property {'public'|'locked'|'private'} config.type
* @property {'public'|'inherit'|'locked'|'private'} config.type
* @property {Map<Snowflake, { id : Snowflake, type : 'role'|'user' }>} config.whitelist
* @property {Map<Snowflake, { id : Snowflake, type : 'role'|'user' }>} config.blacklist
* @property {number} config.size
Expand All @@ -28,7 +28,7 @@ class TemporaryChannelService extends Service {
static get cron() {

return {
build : {
cleanupOldChannels : {
schedule : '0 */10 * * * *',
job : 'cleanupOldChannels'
}
Expand All @@ -51,12 +51,13 @@ class TemporaryChannelService extends Service {
}

/**
* @param {import('discord.js').Guild} guild
* @param {TemporaryChannel['config']} config
* @param {TemporaryChannel} temporaryChannel
*
* @returns {import('discord.js').OverwriteResolvable[]}
*/
buildPermissions(guild, config) {
buildPermissions(temporaryChannel) {

const { channel, config } = temporaryChannel;

const view = [
PermissionsBitField.Flags.ReadMessageHistory,
Expand All @@ -69,6 +70,7 @@ class TemporaryChannelService extends Service {
PermissionsBitField.Flags.Speak
];

/** @type {import('discord.js').OverwriteData[]} */
const permissions = [
{ id : config.ownerId, allow : [...view, ...access] },
{ id : this.client.user.id, allow : [PermissionsBitField.Flags.ManageChannels, ...view, ...access] }
Expand All @@ -78,21 +80,36 @@ class TemporaryChannelService extends Service {

case 'public': {

permissions.push({ id : guild.roles.everyone.id, allow : access });
permissions.push({ id : channel.guild.roles.everyone.id, allow : access });

break;
}

case 'inherit': {

if (channel.parent) {

const parentPermissions = channel.parent.permissionOverwrites.cache.values();

for (const overwrite of parentPermissions) {

permissions.push(overwrite);
}
}

break;
}

case 'locked': {

permissions.push({ id : guild.roles.everyone.id, allow : view, deny : access });
permissions.push({ id : channel.guild.roles.everyone.id, allow : view, deny : access });

break;
}

case 'private': {

permissions.push({ id : guild.roles.everyone.id, deny : [...view, ...access] });
permissions.push({ id : channel.guild.roles.everyone.id, deny : [...view, ...access] });

break;
}
Expand Down Expand Up @@ -132,33 +149,33 @@ class TemporaryChannelService extends Service {
};

const channel = await hub.channel.guild.channels.create({
name : `${ owner.displayName }'s channel`,
type : ChannelType.GuildVoice,
parent : hub.channel.parent,
userLimit : config.size,
permissionOverwrites : this.buildPermissions(hub.channel.guild, config)
name : `${ owner.displayName }'s channel`,
type : ChannelType.GuildVoice,
parent : hub.channel.parent,
userLimit : config.size
});

const message = await channel.send(ControlView.controlMessage({ channel, owner, config }));

config.messageId = message.id;

await Promise.all([
this.update({ channel, owner, config }),
this.store.set('control', channel.guild.id, message.id, { channelId : channel.id })
]);

this.client.logger.info({
msg : `Created temporary channel ${ channel.name } in ${ hub.channel.guild.name } for : ${ owner.displayName }`,
emitter : `${ this.module }.${ this.id }`,
event : 'createTemporaryChannel',
metadata : {
guildId : hub.guildId,
guildId : channel.guildId,
channelId : channel.id,
ownerId : owner.id
}
});

await Promise.all([
this.update({ channel, owner, config }),
this.updatePermission({ channel, owner, config }),
this.store.set('control', channel.guild.id, message.id, { channelId : channel.id })
]);

return { channel, owner, config };
}

Expand Down Expand Up @@ -250,9 +267,20 @@ class TemporaryChannelService extends Service {
/**
* @param {TemporaryChannel} temporaryChannel
*/
async updatePermission({ channel, config }) {
async updatePermission(temporaryChannel) {

await temporaryChannel.channel.permissionOverwrites.set(this.buildPermissions(temporaryChannel));

await channel.permissionOverwrites.set(this.buildPermissions(channel.guild, config));
this.client.logger.info({
msg : `Updated permission for temporary channel ${ temporaryChannel.channel.name } in ${ temporaryChannel.channel.guild.name }`,
emitter : `${ this.module }.${ this.id }`,
event : 'updatePermissionTemporaryChannel',
metadata : {
guildId : temporaryChannel.channel.guildId,
channelId : temporaryChannel.channel.id,
ownerId : temporaryChannel.config.ownerId
}
});
}

/**
Expand Down
11 changes: 10 additions & 1 deletion src/modules/voice/views/control.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { ActionRowBuilder, ButtonBuilder, UserSelectMenuBuilder, MentionableSelectMenuBuilder, StringSelectMenuBuilder, ButtonStyle } = require('discord.js');
const { userMention, inlineCode, roleMention } = require('discord.js');

const { View } = require('../../../core');
const { View, Util } = require('../../../core');

const { interactions } = require('../interactions/control');

Expand Down Expand Up @@ -44,8 +44,11 @@ class ControlView extends View {
.setAuthor({ iconURL : owner.user.displayAvatarURL(), name : `Temporary channel control interface` })
.addFields([
{ name : '📢 Public', value : 'Channel is open for everyone to join except blacklisted members', inline : true },
{ name : '📂 Inherit', value : 'Channel inherit default permission from the category plus allow whitelisted members and block blacklisted members', inline : true },
{ name : Util.BLANK_CHAR, value : Util.BLANK_CHAR, inline : true },
{ name : '🔒 Locked', value : 'Channel is visible to everyone except blacklisted members, only whitelisted members can join', inline : true },
{ name : '🥷 Private', value : 'Channel is hidden, only whitelisted members can see it and join', inline : true },
{ name : Util.BLANK_CHAR, value : Util.BLANK_CHAR, inline : true },
{ name : `👑 Owner :`, value : userMention(owner.user.id), inline : true },
{ name : `🌎 Region :`, value : inlineCode(channel.rtcRegion ?? 'Automatic'), inline : true },
{ name : `🔢 Limit :`, value : inlineCode(channel.userLimit || '∞'), inline : true },
Expand All @@ -64,6 +67,12 @@ class ControlView extends View {
.setDisabled(config.type === 'public')
.setLabel('Public')
.setEmoji({ name : '📢' }),
new ButtonBuilder()
.setCustomId(interactions.setInherit.customId)
.setStyle(config.type === 'inherit' ? ButtonStyle.Primary : ButtonStyle.Secondary)
.setDisabled(config.type === 'inherit')
.setLabel('Inherit')
.setEmoji({ name : '📂' }),
new ButtonBuilder()
.setCustomId(interactions.setLocked.customId)
.setStyle(config.type === 'locked' ? ButtonStyle.Primary : ButtonStyle.Secondary)
Expand Down
1 change: 1 addition & 0 deletions src/modules/voice/views/hub.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class HubView extends View {
.setMinValues(1).setMaxValues(1)
.setOptions([
{ label : 'Public', value : 'public', emoji : { name : '📢' }, default : hub.config.defaultType === 'public' },
{ label : 'Inherit', value : 'inherit', emoji : { name : '📂' }, default : hub.config.defaultType === 'inherit' },
{ label : 'Locked', value : 'locked', emoji : { name : '🔒' }, default : hub.config.defaultType === 'locked' },
{ label : 'Private', value : 'private', emoji : { name : '🥷' }, default : hub.config.defaultType === 'private' }
])
Expand Down
Loading