use existing private channels

This commit is contained in:
Baipyrus 2024-02-07 20:04:16 +01:00
parent a29e2074a5
commit f3e2715703
3 changed files with 60 additions and 24 deletions

View File

@ -1,4 +1,4 @@
import { SlashCommandBuilder } from 'discord.js'; import { ChannelType, SlashCommandBuilder } from 'discord.js';
import { VoiceChannel } from '../../../database.js'; import { VoiceChannel } from '../../../database.js';
export const data = new SlashCommandBuilder() export const data = new SlashCommandBuilder()
@ -39,13 +39,16 @@ export async function execute(interaction) {
step = 'create'; step = 'create';
// Create new channel // Create new channel
const channel = await guild.create({ const channel = await guild.channels.create({
name, type: ChannelType.GuildVoice name, type: ChannelType.GuildVoice
}); });
// Save channel data // Save channel data
step = 'save'; step = 'save';
await VoiceChannel.create({ id: channel.id }); await VoiceChannel.create({
id: channel.id,
create: true
});
// Reply success to acknowledge command // Reply success to acknowledge command
await interaction.reply({ await interaction.reply({
@ -64,7 +67,7 @@ export async function execute(interaction) {
// Save channel data // Save channel data
step = 'save'; step = 'save';
await VoiceChannel.create({ id }); await VoiceChannel.create({ id, create: true });
// Reply success to acknowledge command // Reply success to acknowledge command
await interaction.reply({ await interaction.reply({

View File

@ -1,45 +1,70 @@
import { ChannelType, Events, PermissionsBitField } from 'discord.js'; import { ChannelType, Events, PermissionsBitField } from 'discord.js';
import { VoiceChannel } from '../../database.js'; import { VoiceChannel } from '../../database.js';
const getchannel = async (member, channels) => {
// Check database for existing channel
const ownCh = await VoiceChannel.findOne({
where: {
owner: member.user.id
}
});
if (ownCh !== null) {
return await channels.fetch(ownCh.id);
}
// Create private channel with all permissions
const name = member.user.username;
const chName = `${name}${name.endsWith('s') ? "'" : "'s"} channel`;
const privCh = await channels.create({
name: chName,
type: ChannelType.GuildVoice,
permissionOverwrites: [
{
id: member.id,
allow: [
PermissionsBitField.All
]
}
]
});
// Save newly created channel
await VoiceChannel.create({
id: privCh.id,
owner: member.user.id
})
return privCh;
};
export const name = Events.VoiceStateUpdate; export const name = Events.VoiceStateUpdate;
export async function execute(_, state) { export async function execute(_, state) {
if (!state.channel) return; if (!state.channel) return;
// Find channel by id, return if not registered for customs // Find channel by id, return if not registered for customs
const channel = await VoiceChannel.findOne({ const createCh = await VoiceChannel.findOne({
where: { where: {
id: state.channel.id id: state.channel.id,
create: true,
} }
}); });
if (channel === null) return; if (createCh === null) return;
// Extract user data // Extract user data
const member = state.member; const member = state.member;
const name = member.user.username;
// Extract channel data // Extract channel data
const channels = state.guild.channels; const channels = state.guild.channels;
let step = 'create';
try { try {
// Create private channel with all permissions const privCh = await getchannel(member, channels);
const chName = `${name}${name.endsWith('s') ? "'" : "'s"} channel`;
const privCh = await channels.create({
name: chName,
type: ChannelType.GuildVoice,
permissionOverwrites: [
{
id: member.id,
allow: [
PermissionsBitField.All
]
}
]
});
step = 'move to';
// Move user to private channel // Move user to private channel
await state.setChannel(privCh); await state.setChannel(privCh);
console.info(`[INFO] User '${name}' created private channel with ID ${privCh.id}.`); console.info(`[INFO] User '${name}' created private channel with ID ${privCh.id}.`);
} catch (error) { } catch (error) {
console.error(error); console.error(error);
await member.send('Could no create or move to channel! Please contact server staff.'); await member.send(`Failed to ${step} channel! Please contact server staff.`);
} }
} }

View File

@ -1,10 +1,18 @@
import { DataTypes } from 'sequelize'; import { DataTypes } from 'sequelize';
export default function (sequelize) { export default function(sequelize) {
return sequelize.define('VoiceChannel', { return sequelize.define('VoiceChannel', {
id: { id: {
type: DataTypes.STRING, type: DataTypes.STRING,
primaryKey: true primaryKey: true
},
create: {
type: DataTypes.BOOLEAN
},
owner: {
type: DataTypes.STRING,
defaultValue: false,
allowNull: true
} }
}); });
} }