Skip to content
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
28 changes: 23 additions & 5 deletions src/discordTools/SetupGuildCategory.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,48 @@

*/

const Discord = require('discord.js');
const DiscordTools = require('../discordTools/discordTools.js');
const PermissionHandler = require('../handlers/permissionHandler.js');

module.exports = async (client, guild) => {
const instance = client.getInstance(guild.id);
const perms = PermissionHandler.getPermissionsReset(client, guild, false);

let category = undefined;
if (instance.channelId.category !== null) {
category = DiscordTools.getCategoryById(guild.id, instance.channelId.category);
if (category && !botHasChannelPermissions(guild, category)) {
category = undefined;
}
}
if (category === undefined) {
category = await DiscordTools.addCategory(guild.id, 'rustplusplus');
category = await DiscordTools.addCategory(guild.id, 'rustplusplus', perms);
if (!category) {
return undefined;
}
instance.channelId.category = category.id;
client.setInstance(guild.id, instance);
}

const perms = PermissionHandler.getPermissionsReset(client, guild, false);

try {
await category.permissionOverwrites.set(perms);
}
catch (e) {
/* Ignore */
client.log(client.intlGet(null, 'errorCap'),
`Could not set permission overwrites for category ${category.id}: ${e}`, 'error');
}

return category;
};
};

function botHasChannelPermissions(guild, channel) {
const me = guild.members?.me;
if (!me) return true;

const permissions = channel.permissionsFor(me);
return permissions?.has([
Discord.PermissionFlagsBits.ViewChannel,
Discord.PermissionFlagsBits.ManageChannels
]) ?? false;
}
49 changes: 32 additions & 17 deletions src/discordTools/SetupGuildChannels.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@

*/

const Discord = require('discord.js');
const DiscordTools = require('../discordTools/discordTools.js');
const PermissionHandler = require('../handlers/permissionHandler.js');

module.exports = async (client, guild, category) => {
if (!category) {
return;
}

await addTextChannel(client.intlGet(guild.id, 'channelNameInformation'), 'information', client, guild, category);
await addTextChannel(client.intlGet(guild.id, 'channelNameServers'), 'servers', client, guild, category);
await addTextChannel(client.intlGet(guild.id, 'channelNameSettings'), 'settings', client, guild, category);
Expand All @@ -39,47 +44,57 @@ module.exports = async (client, guild, category) => {

async function addTextChannel(name, idName, client, guild, parent, permissionWrite = false) {
const instance = client.getInstance(guild.id);
const perms = PermissionHandler.getPermissionsReset(client, guild, permissionWrite);

let channel = undefined;
if (instance.channelId[idName] !== null) {
channel = DiscordTools.getTextChannelById(guild.id, instance.channelId[idName]);
if (channel && !botCanUseTextChannel(guild, channel)) {
instance.channelId[idName] = null;
client.setInstance(guild.id, instance);
channel = undefined;
}
}
if (channel === undefined) {
channel = await DiscordTools.addTextChannel(guild.id, name);
channel = await DiscordTools.addTextChannel(guild.id, name, parent.id, perms);
if (!channel) {
return;
}
instance.channelId[idName] = channel.id;
client.setInstance(guild.id, instance);

try {
channel.setParent(parent.id);
}
catch (e) {
client.log(client.intlGet(null, 'errorCap'),
client.intlGet(null, 'couldNotSetParent', { channelId: channel.id }), 'error');
}
}

if (instance.firstTime) {
if (channel && (instance.firstTime || channel.parentId !== parent.id)) {
try {
channel.setParent(parent.id);
await channel.setParent(parent.id, { lockPermissions: false });
}
catch (e) {
client.log(client.intlGet(null, 'errorCap'),
client.intlGet(null, 'couldNotSetParent', { channelId: channel.id }), 'error');
`${client.intlGet(null, 'couldNotSetParent', { channelId: channel.id })}: ${e}`, 'error');
}
}

const perms = PermissionHandler.getPermissionsReset(client, guild, permissionWrite);

try {
await channel.permissionOverwrites.set(perms);
}
catch (e) {
/* Ignore */
client.log(client.intlGet(null, 'errorCap'),
`Could not set permission overwrites for channel ${channel.id}: ${e}`, 'error');
}

/* Currently, this halts the entire application... Too lazy to fix...
It is possible to just remove the channels and let the bot recreate them with correct name language */
//channel.setName(name);
}

function botCanUseTextChannel(guild, channel) {
const me = guild.members?.me;
if (!me) return true;

channel.lockPermissions();
}
const permissions = channel.permissionsFor(me);
return permissions?.has([
Discord.PermissionFlagsBits.ViewChannel,
Discord.PermissionFlagsBits.SendMessages,
Discord.PermissionFlagsBits.ManageChannels
]) ?? false;
}
18 changes: 12 additions & 6 deletions src/discordTools/discordTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,15 @@ module.exports = {
return undefined;
},

addCategory: async function (guildId, name) {
addCategory: async function (guildId, name, permissionOverwrites = null) {
const guild = module.exports.getGuild(guildId);

if (guild) {
try {
return await guild.channels.create({
name: name,
type: Discord.ChannelType.GuildCategory,
permissionOverwrites: [{
permissionOverwrites: permissionOverwrites ?? [{
id: guild.roles.everyone.id,
deny: [Discord.PermissionFlagsBits.SendMessages]
}]
Expand Down Expand Up @@ -217,19 +217,25 @@ module.exports = {
return true;
},

addTextChannel: async function (guildId, name) {
addTextChannel: async function (guildId, name, parentId = null, permissionOverwrites = null) {
const guild = module.exports.getGuild(guildId);

if (guild) {
try {
return await guild.channels.create({
const options = {
name: name,
type: Discord.ChannelType.GuildText,
permissionOverwrites: [{
permissionOverwrites: permissionOverwrites ?? [{
id: guild.roles.everyone.id,
deny: [Discord.PermissionFlagsBits.SendMessages]
}],
});
};

if (parentId !== null) {
options.parent = parentId;
}

return await guild.channels.create(options);
}
catch (e) {
Client.client.log(Client.client.intlGet(null, 'errorCap'),
Expand Down
26 changes: 25 additions & 1 deletion src/handlers/permissionHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ module.exports = {
});
}

const botId = client.user?.id;
if (botId) {
perms.push({
id: botId,
allow: [
Discord.PermissionFlagsBits.ViewChannel,
Discord.PermissionFlagsBits.SendMessages,
Discord.PermissionFlagsBits.ReadMessageHistory
]
});
}

return perms;
},

Expand All @@ -87,6 +99,18 @@ module.exports = {
deny: [Discord.PermissionFlagsBits.ViewChannel, Discord.PermissionFlagsBits.SendMessages]
});

const botId = client.user?.id;
if (botId) {
perms.push({
id: botId,
allow: [
Discord.PermissionFlagsBits.ViewChannel,
Discord.PermissionFlagsBits.SendMessages,
Discord.PermissionFlagsBits.ReadMessageHistory
]
});
}

return perms;
},

Expand Down Expand Up @@ -121,4 +145,4 @@ module.exports = {
}
}
},
}
}
Loading