Skip to content
Open
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
155 changes: 90 additions & 65 deletions Settings.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,106 @@
const fs = require("fs");
const _ = require('lodash');
const _ = require("lodash");

/**
* @class Settings
* @classdesc loads and saves settings for the entire app.
*/
class Settings {
/**
* Global settings loaded in memory.
* @private
* @var {object} setting
*/
setting = {};
permVoiceTemplate = _.template(`g-<%= guildId %>.permVoice.c-<%= channelId %>`);
/**
* Global settings loaded in memory.
* @private
* @var {object} setting
*/
setting = {};
permVoiceTemplate = _.template(
`g-<%= guildId %>.permVoice.c-<%= channelId %>`,
);

/**
* Loads settings from file into memory.
*/
constructor() {
let file = {};
try {
if (!fs.existsSync(process.env.SETTINGSFILE)) {
const pathPieces = process.env.SETTINGSFILE.split('/');
pathPieces.pop();
fs.mkdirSync(pathPieces.join('/'), { recursive: true });
fs.writeFileSync(process.env.SETTINGSFILE, '{}', 'utf8');
}
file = fs.readFileSync(process.env.SETTINGSFILE, 'utf8');
} catch(e) {
console.error(`Error loading settings file from ${process.env.SETTINGSFILE}`, e.message);
}

try {
this.setting = JSON.parse(file);
} catch (e) {
console.error('Error parsing JSON from settings file!!', e.message);
}
// Will save after 10 seconds of the last update call, but will save every 60 seconds no matter what.
this._save = _.debounce(_.bind(this._save, this), 10000, { 'maxWait': 60000 });
/**
* Loads settings from file into memory.
*/
constructor() {
let file = {};
try {
if (!fs.existsSync(process.env.SETTINGSFILE)) {
const pathPieces = process.env.SETTINGSFILE.split("/");
pathPieces.pop();
fs.mkdirSync(pathPieces.join("/"), { recursive: true });
fs.writeFileSync(process.env.SETTINGSFILE, "{}", "utf8");
}
file = fs.readFileSync(process.env.SETTINGSFILE, "utf8");
} catch (e) {
console.error(
`Error loading settings file from ${process.env.SETTINGSFILE}`,
e.message,
);
}

/**
* async save/update settings file after an update to the data has been made.
* This function is debounced
* @private
*/
_save() {
fs.writeFile(process.env.SETTINGSFILE, JSON.stringify(this.setting), 'utf8', (err) => {
if (err) {
console.error('Settings have been updated but failed to save!', err.message);
} else {
console.log('Settings have been updated');
}
});
try {
this.setting = JSON.parse(file);
} catch (e) {
console.error("Error parsing JSON from settings file!!", e.message);
}
// Will save after 10 seconds of the last update call, but will save every 60 seconds no matter what.
this._save = _.debounce(_.bind(this._save, this), 10000, {
maxWait: 60000,
});
}

/**
* Toggles the permission for voice of a channel
* @param {string} guildId The guild id to get the settings for
* @param {string} channelId The channel id to add or delete from th settings file
*/
togglePermVoiceChannel(guildId, channelId) {
_.set(this.setting,
this.permVoiceTemplate({ guildId, channelId }),
!_.get(this.setting, this.permVoiceTemplate({ guildId, channelId }), false));
this._save();
}
/**
* async save/update settings file after an update to the data has been made.
* This function is debounced
* @private
*/
_save() {
fs.writeFile(
process.env.SETTINGSFILE,
JSON.stringify(this.setting),
"utf8",
(err) => {
if (err) {
console.error(
"Settings have been updated but failed to save!",
err.message,
);
} else {
console.log("Settings have been updated");
}
},
);
}

/**
* Checks if a channel has permission or not.
* @param {string} guildId The guild id to get the settings for
* @param {string} channelId The channel id to check
* @return {boolean}
*/
doesChannelHavePermVoice(guildId, channelId) {
return _.get(this.setting, this.permVoiceTemplate({ guildId, channelId }), false);
}
/**
* Toggles the permission for voice of a channel
* @param {string} guildId The guild id to get the settings for
* @param {string} channelId The channel id to add or delete from th settings file
*/
togglePermVoiceChannel(guildId, channelId) {
_.set(
this.setting,
this.permVoiceTemplate({ guildId, channelId }),
!_.get(
this.setting,
this.permVoiceTemplate({ guildId, channelId }),
false,
),
);
this._save();
}

/**
* Checks if a channel has permission or not.
* @param {string} guildId The guild id to get the settings for
* @param {string} channelId The channel id to check
* @return {boolean}
*/
doesChannelHavePermVoice(guildId, channelId) {
return _.get(
this.setting,
this.permVoiceTemplate({ guildId, channelId }),
false,
);
}
}

module.exports = new Settings();
62 changes: 36 additions & 26 deletions commands/channelcommands/bitrate.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,52 @@
const { Client, SlashCommandBuilder, PermissionFlagsBits , ChannelType, GuildChannel } = require('discord.js');
require('dotenv').config();
const { channelOwners } = require('../../methods/channelowner');
const { SlashCommandBuilder } = require("discord.js");
require("dotenv").config();
const { channelOwners } = require("../../methods/channelowner");

module.exports = {
category: 'channelcommands',
category: "channelcommands",
data: new SlashCommandBuilder()
.setName('bitrate')
.setDescription('The bitrate for the channel.')
.addIntegerOption(option =>
option.setName('bitrate')
.setDescription('The bitrate for the channel.')
.setRequired(true)
.setMinValue(8)
.setMaxValue(96)),
async execute(interaction) {
const guild = interaction.guild
.setName("bitrate")
.setDescription("The bitrate for the channel.")
.addIntegerOption((option) =>
option
.setName("bitrate")
.setDescription("The bitrate for the channel.")
.setRequired(true)
.setMinValue(8)
.setMaxValue(96),
),
async execute(interaction) {
const member = await interaction.guild.members.fetch(interaction.user.id);
const currentChannel = member.voice.channel.id;
const bitrate = interaction.options.getInteger('bitrate');

//Check if the user is in a voice channel
if (!channelOwners.has(currentChannel)) {
return interaction.reply({ content: 'You must be in a temporary channel.', ephemeral: true });
return interaction.reply({
content: "You must be in a temporary channel.",
ephemeral: true,
});
}

//Check if the user is the owner of the channel
if (channelOwners.get(currentChannel) !== member.id) {
return interaction.reply({ content: 'You do not have permission to use this command.', ephemeral: true });
return interaction.reply({
content: "You do not have permission to use this command.",
ephemeral: true,
});
}

//Method to set the channel permanet status toggles
try {
await interaction.reply({ content:`This feature is a work in progress.`, ephemeral: true });

} catch (error) {
await interaction.reply({ content:`There was an error while using the command.`, ephemeral: true });
console.log(error);
}

try {
await interaction.reply({
content: `This feature is a work in progress.`,
ephemeral: true,
});
} catch (error) {
await interaction.reply({
content: `There was an error while using the command.`,
ephemeral: true,
});
console.log(error);
}
},
};
};
68 changes: 39 additions & 29 deletions commands/channelcommands/limit.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,57 @@
const { Client, SlashCommandBuilder, PermissionsBitField , ChannelType, GuildChannel } = require('discord.js');
require('dotenv').config();
const { channelOwners } = require('../../methods/channelowner');
const { toggleLock } = require('../../methods/locks');
const { SlashCommandBuilder } = require("discord.js");
require("dotenv").config();
const { channelOwners } = require("../../methods/channelowner");

module.exports = {
category: 'limir',
category: "limir",
data: new SlashCommandBuilder()
.setName('limit')
.setDescription('Change the user limit.')
.addIntegerOption(option =>
option.setName('limit')
.setDescription('The user limit for the channel.')
.setRequired(true)
.setMinValue(1)
.setMaxValue(99)),
.setName("limit")
.setDescription("Change the user limit.")
.addIntegerOption((option) =>
option
.setName("limit")
.setDescription("The user limit for the channel.")
.setRequired(true)
.setMinValue(1)
.setMaxValue(99),
),
async execute(interaction) {
const guild = interaction.guild
const guild = interaction.guild;
const member = await interaction.guild.members.fetch(interaction.user.id);
const currentChannel = member.voice.channel.id;
const targetChannel = guild.channels.cache.get(currentChannel);
const limit = interaction.options.getInteger('limit');
const limit = interaction.options.getInteger("limit");

//Check if the user is in a voice channel
if (!channelOwners.has(currentChannel)) {
return interaction.reply({ content: 'You must be in a temporary channel.', ephemeral: true });
return interaction.reply({
content: "You must be in a temporary channel.",
ephemeral: true,
});
}

//Check if the user is the owner of the channel
if (channelOwners.get(currentChannel) !== member.id) {
return interaction.reply({ content: 'You do not have permission to use this command.', ephemeral: true });
return interaction.reply({
content: "You do not have permission to use this command.",
ephemeral: true,
});
}

//Public to private
try {

// Remove the channel permissions for @everyone
targetChannel.setUserLimit(limi)
return interaction.reply({ content: `the limit was adjusted to ${limit}`, ephemeral: true });

} catch (error) {
await interaction.reply({ content:`There was an error while using the command.`, ephemeral: true });
console.log(error);
}

try {
// Remove the channel permissions for @everyone
targetChannel.setUserLimit(limit);
return interaction.reply({
content: `the limit was adjusted to ${limit}`,
ephemeral: true,
});
} catch (error) {
await interaction.reply({
content: `There was an error while using the command.`,
ephemeral: true,
});
console.log(error);
}
},
};
};
Loading