Skip to content
Snippets Groups Projects
Commit 8e90be50 authored by Patric Plattner's avatar Patric Plattner
Browse files

Adding per server locale support and adding German locale.

parent 60f91040
No related branches found
No related tags found
No related merge requests found
Showing
with 677 additions and 245 deletions
const { SlashCommandBuilder } = require('@discordjs/builders'); const {SlashCommandBuilder} = require('@discordjs/builders');
module.exports = new SlashCommandBuilder() const locale = require('../util/locale');
.setName('add_role_menu')
.setDescription('Create a new role menu') module.exports = (loc) => {
.addChannelOption( option => const commandLocale = locale.get(loc).slashCommands.add_role_menu;
option.setName('channel') return new SlashCommandBuilder()
.setDescription('Channel the menu will be put inside.') .setName('add_role_menu')
.setRequired(true)) .setDescription(commandLocale.description)
.addStringOption( option => .addChannelOption(option =>
option.setName('message') option.setName('channel')
.setDescription('Message content.') .setDescription(commandLocale.channelDescription)
.setRequired(true)) .setRequired(true))
.addStringOption( option => .addStringOption(option =>
option.setName('type') option.setName('message')
.setDescription('NOT YET IMPLEMENTED Sets type of role menu.') .setDescription(commandLocale.messageDescription)
.setRequired(true) .setRequired(true))
.addChoice('One of N', 'one') .addStringOption(option =>
.addChoice('M of N', 'm') option.setName('type')
.addChoice('Any of N', 'any')) .setDescription(commandLocale.typeDescription)
.addBooleanOption( option => .setRequired(true)
option.setName('verification') .addChoice(commandLocale.typeChoices.oneOfN, 'one')
.setDescription('NOT YET IMPLEMENTED Sets verification state of role menu.') .addChoice(commandLocale.typeChoices.mOfN, 'm')
.setRequired(true)) .addChoice(commandLocale.typeChoices.anyOfN, 'any'))
\ No newline at end of file .addBooleanOption(option =>
option.setName('verification')
.setDescription(commandLocale.verificationDescription)
.setRequired(true));
}
\ No newline at end of file
const { SlashCommandBuilder } = require('@discordjs/builders'); const {SlashCommandBuilder} = require('@discordjs/builders');
module.exports = new SlashCommandBuilder() const locale = require('../util/locale');
.setName('add_role_to_menu')
.setDescription('Add role to a menu.') module.exports = (loc) => {
.addChannelOption( option => const commandLocale = locale.get(loc).slashCommands.add_role_to_menu;
option.setName('channel') return new SlashCommandBuilder()
.setDescription('Channel of the menu.') .setName('add_role_to_menu')
.setRequired(true)) .setDescription(commandLocale.description)
.addStringOption( option => .addChannelOption(option =>
option.setName('message_id') option.setName('channel')
.setDescription('Message ID of the menu.') .setDescription(commandLocale.channelDescription)
.setRequired(true)) .setRequired(true))
.addRoleOption(option => .addStringOption(option =>
option.setName('role') option.setName('message_id')
.setDescription('Role to add to the menu.') .setDescription(commandLocale.messageIDDescription)
.setRequired(true)) .setRequired(true))
\ No newline at end of file .addRoleOption(option =>
option.setName('role')
.setDescription(commandLocale.roleDescription)
.setRequired(true));
}
\ No newline at end of file
const { SlashCommandBuilder } = require('@discordjs/builders'); const {SlashCommandBuilder} = require('@discordjs/builders');
module.exports = new SlashCommandBuilder() const locale = require('../util/locale');
.setName('assign_permission')
.setDescription('Assign a permission to a role') module.exports = (loc) => {
.addStringOption(option => const commandLocale = locale.get(loc).slashCommands.assign_permission;
option.setName('permission') return new SlashCommandBuilder()
.setDescription('Permission to assign.') .setName('assign_permission')
.setRequired(true) .setDescription(commandLocale.description)
.addChoice("Role Menus", 'role_menu')) .addStringOption(option =>
.addRoleOption(option => option.setName('permission')
option.setName('role') .setDescription(commandLocale.permissionDescription)
.setDescription('Role to assign permission to.') .setRequired(true)
.setRequired(true)) .addChoice(commandLocale.permissionChoices.roleMenus, 'role_menu')
\ No newline at end of file .addChoice(commandLocale.permissionChoices.locale, 'locale'))
.addRoleOption(option =>
option.setName('role')
.setDescription(commandLocale.roleDescription)
.setRequired(true));
}
\ No newline at end of file
const {SlashCommandBuilder} = require('@discordjs/builders');
const fs = require('fs');
locale = require('../util/locale');
module.exports = (loc, pathToLocales) => {
const commandLocale = locale.get(loc).slashCommands.change_locale;
return new SlashCommandBuilder()
.setName('change_locale')
.setDescription(commandLocale.description)
.addStringOption((option) => {
option.setName('locale')
.setDescription(commandLocale.localeDescription)
.setRequired(true)
const localeFiles = fs.readdirSync('./locales').filter(file => file.endsWith('.json'));
for (const localeFile of localeFiles) {
const locale1 = JSON.parse(fs.readFileSync(`./locales/${localeFile}`));
option.addChoice(locale1.commonName, locale1.locale);
}
return option
});
}
const { SlashCommandBuilder } = require('@discordjs/builders'); const {SlashCommandBuilder} = require('@discordjs/builders');
module.exports = new SlashCommandBuilder() const locale = require('../util/locale');
.setName('delete_role_from_menu')
.setDescription('Delete role from menu.') module.exports = (loc) => {
.addChannelOption( option => const commandLocale = locale.get(loc).slashCommands.delete_role_from_menu;
option.setName('channel') return new SlashCommandBuilder()
.setDescription('Channel of the menu.') .setName('delete_role_from_menu')
.setRequired(true)) .setDescription(commandLocale.description)
.addStringOption( option => .addChannelOption(option =>
option.setName('message_id') option.setName('channel')
.setDescription('Message ID of the menu.') .setDescription(commandLocale.channelDescription)
.setRequired(true)) .setRequired(true))
.addRoleOption(option => .addStringOption(option =>
option.setName('role') option.setName('message_id')
.setDescription('Role to remove from the menu.') .setDescription(commandLocale.messageIDDescription)
.setRequired(true)) .setRequired(true))
\ No newline at end of file .addRoleOption(option =>
option.setName('role')
.setDescription(commandLocale.roleDescription)
.setRequired(true));
}
\ No newline at end of file
const { SlashCommandBuilder } = require('@discordjs/builders'); const {SlashCommandBuilder} = require('@discordjs/builders');
module.exports = new SlashCommandBuilder() const locale = require('../util/locale');
.setName('delete_role_menu')
.setDescription('Delete a role menu') module.exports = (loc) => {
.addChannelOption( option => const commandLocale = locale.get(loc).slashCommands.delete_role_menu;
option.setName('channel') return new SlashCommandBuilder()
.setDescription('Select channel of to be deleted menu') .setName('delete_role_menu')
.setRequired(true)) .setDescription(commandLocale.description)
.addStringOption( option => .addChannelOption(option =>
option.setName('message_id') option.setName('channel')
.setDescription('Message ID of to be deleted menu') .setDescription(commandLocale.channelDescription)
.setRequired(true)) .setRequired(true))
\ No newline at end of file .addStringOption(option =>
option.setName('message_id')
.setDescription(commandLocale.messageIDDescription)
.setRequired(true));
}
\ No newline at end of file
const { SlashCommandBuilder } = require('@discordjs/builders'); const {SlashCommandBuilder} = require('@discordjs/builders');
module.exports = new SlashCommandBuilder() const locale = require('../util/locale');
.setName('edit_role_menu')
.setDescription('Edit an existing role menu.') module.exports = (loc) => {
.addChannelOption( option => const commandLocale = locale.get(loc).slashCommands.edit_role_menu;
option.setName('channel') return new SlashCommandBuilder()
.setDescription('Channel of the menu.') .setName('edit_role_menu')
.setRequired(true)) .setDescription(commandLocale.description)
.addStringOption( option => .addChannelOption(option =>
option.setName('message_id') option.setName('channel')
.setDescription('Message ID of the menu.') .setDescription(commandLocale.channelDescription)
.setRequired(true)) .setRequired(true))
.addStringOption( option => .addStringOption(option =>
option.setName('message') option.setName('message_id')
.setDescription('Message content.')) .setDescription(commandLocale.messageIDDescription)
.addStringOption( option => .setRequired(true))
option.setName('type') .addStringOption(option =>
.setDescription('NOT YET IMPLEMENTED') option.setName('message')
.addChoice('One of N', 'one') .setDescription(commandLocale.messageDescription))
.addChoice('M of N', 'm') .addStringOption(option =>
.addChoice('Any of N', 'any')) option.setName('type')
.addBooleanOption( option => .setDescription(commandLocale.typeDescription)
option.setName('verification') .addChoice(commandLocale.typeChoices.oneOfN, 'one')
.setDescription('NOT YET IMPLEMENTED Sets verification state of role menu.')) .addChoice(commandLocale.typeChoices.mOfN, 'm')
\ No newline at end of file .addChoice(commandLocale.typeChoices.anyOfN, 'any'))
.addBooleanOption(option =>
option.setName('verification')
.setDescription(commandLocale.verificationDescription));
}
\ No newline at end of file
const { SlashCommandBuilder } = require('@discordjs/builders'); const {SlashCommandBuilder} = require('@discordjs/builders');
module.exports = new SlashCommandBuilder() const locale = require('../util/locale');
.setName('list_permissions')
.setDescription('List all Permissions for this Server.'); module.exports = (loc) => {
\ No newline at end of file const commandLocale = locale.get(loc).slashCommands.list_permissions;
return new SlashCommandBuilder()
.setName('list_permissions')
.setDescription(commandLocale.description);
}
\ No newline at end of file
const { SlashCommandBuilder } = require('@discordjs/builders'); const { SlashCommandBuilder } = require('@discordjs/builders');
module.exports = new SlashCommandBuilder() const locale = require('../util/locale');
.setName('revoke_permission')
.setDescription('Revoke a permission from a role') module.exports = (loc) => {
.addStringOption(option => const commandLocale = locale.get(loc).slashCommands.revoke_permission;
option.setName('permission') return new SlashCommandBuilder()
.setDescription('Permission to revoke.') .setName('revoke_permission')
.setRequired(true) .setDescription(commandLocale.description)
.addChoice("Role Menus", 'role_menu')) .addStringOption(option =>
.addRoleOption(option => option.setName('permission')
option.setName('role') .setDescription(commandLocale.permissionDescription)
.setDescription('Role to revoke permission from.') .setRequired(true)
.setRequired(true)) .addChoice(commandLocale.permissionChoices.roleMenus, 'role_menu'))
\ No newline at end of file .addRoleOption(option =>
option.setName('role')
.setDescription(commandLocale.roleDescription)
.setRequired(true));
}
\ No newline at end of file
const ServerConfig = require('../models/ServerConfig');
const RoleMenu = require('../models/RoleMenu');
const checkPerm = require("../util/perm");
const locales = require('../util/locale');
const registerCommands = require('../util/registerCommands');
async function changeLocale(interaction) {
let config = await ServerConfig.findOne({guildID: interaction.guild.id.toString()});
let locale = 'en';
if (config != null) {
locale = config.locale;
}
const functionLocale = locales.get(locale).components.locale;
if (!(await checkPerm(interaction.member, 'locale'))) {
await interaction.reply({content: functionLocale.authDenied, ephemeral: true})
return;
}
const newLocale = interaction.options.getString('locale');
if (newLocale !== config.locale) {
if (config == null) {
config = new ServerConfig()
config.guildID = interaction.guild.id.toString();
config.permissions = {
roleMenu: [], locale: []
}
}
config.locale = newLocale;
const messageLocale = locales.get(newLocale).misc.messageTemplates.roleDropdown
const menus = await RoleMenu.find({guildID: interaction.guild.id.toString()});
await Promise.all(menus.map(async (menu) => {
console.log(menu);
const message = await (await interaction.guild.channels.fetch(menu.channelID)).messages.fetch(menu.messageID);
let components = message.components;
components[0].components[0].options[components[0].components[0].options.map((option) => option.value).indexOf('select_reset')].label = messageLocale.selectResetText;
components[0].components[0].placeholder = messageLocale.placeholder;
await message.edit({content: message.content, components});
return true;
}));
await config.save();
await interaction.reply({content: locales.get(newLocale).components.locale.localeApplied, ephemeral: true});
await registerCommands(interaction.guild.id.toString(), './commands');
} else {
await interaction.reply({content: functionLocale.sameLocale, ephemeral: true});
}
}
const slashCommandHandler = async (interaction) => {
if (interaction.commandName === 'change_locale') {
await changeLocale(interaction);
}
}
module.exports = {
slashCommandHandler
}
const ServerConfig = require('../models/ServerConfig'); const ServerConfig = require('../models/ServerConfig');
const {Permissions} = require('discord.js'); const {Permissions} = require('discord.js');
const locales = require("../util/locale");
async function assignPermission(interaction) { async function assignPermission(interaction) {
let config = await ServerConfig.findOne({guildID: interaction.guild.id.toString()});
let locale = 'en';
if (config != null) {
locale = config.locale;
}
const functionLocale = locales.get(locale).components.permConfig;
if (interaction.member.permissions.has(Permissions.FLAGS.ADMINISTRATOR)) { if (interaction.member.permissions.has(Permissions.FLAGS.ADMINISTRATOR)) {
const role = interaction.options.getRole('role'); const role = interaction.options.getRole('role');
const perm = interaction.options.getString('permission'); const perm = interaction.options.getString('permission');
let config = await ServerConfig.findOne({guildID: interaction.guild.id.toString()});
if (config == null) { if (config == null) {
config = new ServerConfig(); config = new ServerConfig();
config.guildID = interaction.guild.id.toString(); config.guildID = interaction.guild.id.toString();
...@@ -15,15 +21,30 @@ async function assignPermission(interaction) { ...@@ -15,15 +21,30 @@ async function assignPermission(interaction) {
if (!config.permissions.roleMenu.includes(role.id.toString())) { if (!config.permissions.roleMenu.includes(role.id.toString())) {
config.permissions.roleMenu.push(role.id.toString()); config.permissions.roleMenu.push(role.id.toString());
} }
} else if (perm === 'locale') {
if (!config.permissions.locale.includes(role.id.toString())) {
config.permissions.locale.push(role.id.toString());
}
} }
await config.save(); await config.save();
await interaction.reply({content: `Role ${role.name} has been added to ${perm} permission.`, ephemeral: true}); await interaction.reply({
content: functionLocale.replyAssign
.replaceAll("$$ROLE$$", `${role.name}`)
.replaceAll('$$PERM$$', `${perm}`),
ephemeral: true
});
} else { } else {
await interaction.reply({content: 'Only administrators can access this command.', ephemeral: true}); await interaction.reply({content: functionLocale.accessDenied, ephemeral: true});
} }
} }
async function revokePermission(interaction) { async function revokePermission(interaction) {
let config = await ServerConfig.findOne({guildID: interaction.guild.id.toString()});
let locale = 'en';
if (config != null) {
locale = config.locale;
}
const functionLocale = locales.get(locale).components.permConfig;
if (interaction.member.permissions.has(Permissions.FLAGS.ADMINISTRATOR)) { if (interaction.member.permissions.has(Permissions.FLAGS.ADMINISTRATOR)) {
const role = interaction.options.getRole('role'); const role = interaction.options.getRole('role');
const perm = interaction.options.getString('permission'); const perm = interaction.options.getString('permission');
...@@ -37,18 +58,30 @@ async function revokePermission(interaction) { ...@@ -37,18 +58,30 @@ async function revokePermission(interaction) {
if (config.permissions.roleMenu.includes(role.id.toString())) { if (config.permissions.roleMenu.includes(role.id.toString())) {
config.permissions.roleMenu.splice(config.permissions.roleMenu.indexOf(role.id.toString()), 1); config.permissions.roleMenu.splice(config.permissions.roleMenu.indexOf(role.id.toString()), 1);
} }
} else if (perm === 'locale') {
if (config.permissions.locale.includes(role.id.toString())) {
config.permissions.locale.splice(config.permissions.locale.indexOf(role.id.toString()), 1);
}
} }
await config.save(); await config.save();
await interaction.reply({ await interaction.reply({
content: `Role ${role.name} has been removed from ${perm} permission.`, content: functionLocale.replyRevoke
.replaceAll("$$ROLE$$", `${role.name}`)
.replaceAll('$$PERM$$', `${perm}`),
ephemeral: true ephemeral: true
}); });
} else { } else {
await interaction.reply({content: 'Only administrators can access this command.', ephemeral: true}); await interaction.reply({content: functionLocale.accessDenied, ephemeral: true});
} }
} }
async function listPermissions(interaction) { async function listPermissions(interaction) {
let config = await ServerConfig.findOne({guildID: interaction.guild.id.toString()});
let locale = 'en';
if (config != null) {
locale = config.locale;
}
const functionLocale = locales.get(locale).components.permConfig;
if (interaction.member.permissions.has(Permissions.FLAGS.ADMINISTRATOR)) { if (interaction.member.permissions.has(Permissions.FLAGS.ADMINISTRATOR)) {
let config = await ServerConfig.findOne({guildID: interaction.guild.id.toString()}); let config = await ServerConfig.findOne({guildID: interaction.guild.id.toString()});
if (config == null) { if (config == null) {
...@@ -58,7 +91,12 @@ async function listPermissions(interaction) { ...@@ -58,7 +91,12 @@ async function listPermissions(interaction) {
} }
const roles = await Promise.all(config.permissions.roleMenu.map(async (roleID) => (await interaction.guild.roles.fetch(roleID)).name)); const roles = await Promise.all(config.permissions.roleMenu.map(async (roleID) => (await interaction.guild.roles.fetch(roleID)).name));
await interaction.reply({content: `List of Permissions:\n\nRole Menu: ${roles.join(',')}`, ephemeral: true}); await interaction.reply({
content: functionLocale.replyList.replaceAll('$$ROLEMENU$$', `${roles.join(',')}`),
ephemeral: true
});
} else {
await interaction.reply({content: functionLocale.accessDenied, ephemeral: true});
} }
} }
......
const RoleMenu = require('../models/RoleMenu'); const RoleMenu = require('../models/RoleMenu');
const ServerConfig = require('../models/ServerConfig');
const {MessageActionRow, MessageSelectMenu} = require("discord.js"); const {MessageActionRow, MessageSelectMenu} = require("discord.js");
const checkPerm = require('../util/perm'); const checkPerm = require('../util/perm');
const locales = require("../util/locale");
// slashcommand handler // slashcommand handler
async function addRoleMenu(interaction) { async function addRoleMenu(interaction) {
const config = await ServerConfig.findOne({guildID: interaction.guild.id.toString()});
let locale = 'en';
if (config != null) {
locale = config.locale;
}
const functionLocale = locales.get(locale).components.roleMenu;
const messageLocale = locales.get(locale).misc.messageTemplates.roleDropdown;
if (!(await checkPerm(interaction.member, 'role_menu'))) { if (!(await checkPerm(interaction.member, 'role_menu'))) {
await interaction.reply({content: 'You are not authorized to do this.', ephemeral: true}) await interaction.reply({content: functionLocale.accessDenied, ephemeral: true});
return; return;
} }
const channel = interaction.options.getChannel('channel'); const channel = interaction.options.getChannel('channel');
...@@ -16,10 +25,10 @@ async function addRoleMenu(interaction) { ...@@ -16,10 +25,10 @@ async function addRoleMenu(interaction) {
.addComponents( .addComponents(
new MessageSelectMenu() new MessageSelectMenu()
.setCustomId('selectRole') .setCustomId('selectRole')
.setPlaceholder("Select a role.") .setPlaceholder(messageLocale.placeholder)
.addOptions([ .addOptions([
{ {
label: 'Reset Selection', label: messageLocale.selectResetText,
value: 'select_reset' value: 'select_reset'
} }
]) ])
...@@ -33,42 +42,64 @@ async function addRoleMenu(interaction) { ...@@ -33,42 +42,64 @@ async function addRoleMenu(interaction) {
menu.type = type; menu.type = type;
menu.roles = []; menu.roles = [];
await menu.save(); await menu.save();
await interaction.reply({content: 'Role Menu has been created.', ephemeral: true}); await interaction.reply({content: functionLocale.roleMenuCreated, ephemeral: true});
} }
async function deleteRoleMenu(interaction) { async function deleteRoleMenu(interaction) {
const config = await ServerConfig.findOne({guildID: interaction.guild.id.toString()});
let locale = 'en';
if (config != null) {
locale = config.locale;
}
const functionLocale = locales.get(locale).components.roleMenu;
if (!(await checkPerm(interaction.member, 'role_menu'))) { if (!(await checkPerm(interaction.member, 'role_menu'))) {
await interaction.reply({content: 'You are not authorized to do this.', ephemeral: true}) await interaction.reply({content: functionLocale.accessDenied, ephemeral: true})
return; return;
} }
const channel = interaction.options.getChannel('channel'); const channel = interaction.options.getChannel('channel');
const messageID = interaction.options.getString('message_id'); const messageID = interaction.options.getString('message_id');
let menu = await RoleMenu.findOne({guildID: channel.guild.id, channelID: channel.id, messageID}); let menu = await RoleMenu.findOne({guildID: channel.guild.id, channelID: channel.id, messageID});
if (channel.isText()) { if (channel.isText()) {
const message = await channel.messages.fetch(messageID); var message = null;
if (message != null) { var messageSuccess = true;
if (menu != null) { try {
try { message = await channel.messages.fetch(messageID);
await RoleMenu.deleteOne({_id: menu._id}); } catch (e) {
await message.delete(); messageSuccess = false;
await interaction.reply({content: 'Menu has been deleted.', ephemeral: true}); }
} catch (e) { if (messageSuccess) {
await interaction.reply({content: 'Deleting the Menu has failed.', ephemeral: true}); if (message != null) {
if (menu != null) {
try {
await RoleMenu.deleteOne({_id: menu._id});
await message.delete();
await interaction.reply({content: functionLocale.roleMenuDeleted, ephemeral: true});
} catch (e) {
await interaction.reply({content: functionLocale.roleMenuDeletedFailed, ephemeral: true});
}
} else {
await interaction.reply({content: functionLocale.notMenuMessage, ephemeral: true});
} }
} else { } else {
await interaction.reply({content: 'Please select a menu message.', ephemeral: true}); await interaction.reply({content: functionLocale.messageIdNotFound, ephemeral: true});
} }
} else { } else {
await interaction.reply({content: 'The message ID does not exist in this channel.', ephemeral: true}); await interaction.reply({content: functionLocale.invalidMessageId, ephemeral: true})
} }
} else { } else {
await interaction.reply({content: 'Please select a text channel.', ephemeral: true}); await interaction.reply({content: functionLocale.channelNotText, ephemeral: true});
} }
} }
async function addRoleToMenu(interaction) { async function addRoleToMenu(interaction) {
const config = await ServerConfig.findOne({guildID: interaction.guild.id.toString()});
let locale = 'en';
if (config != null) {
locale = config.locale;
}
const functionLocale = locales.get(locale).components.roleMenu;
if (!(await checkPerm(interaction.member, 'role_menu'))) { if (!(await checkPerm(interaction.member, 'role_menu'))) {
await interaction.reply({content: 'You are not authorized to do this.', ephemeral: true}) await interaction.reply({content: functionLocale.accessDenied, ephemeral: true})
return; return;
} }
const channel = interaction.options.getChannel('channel'); const channel = interaction.options.getChannel('channel');
...@@ -104,37 +135,38 @@ async function addRoleToMenu(interaction) { ...@@ -104,37 +135,38 @@ async function addRoleToMenu(interaction) {
await menu.save(); await menu.save();
await message.edit({content: message.content, components}); await message.edit({content: message.content, components});
await interaction.reply({ await interaction.reply({
content: `Role ${role.name} has been added to the message.`, content: functionLocale.roleAdded.replaceAll("$$ROLE$$", `${role.name}`),
ephemeral: true ephemeral: true
}); });
} else { } else {
await interaction.reply({ await interaction.reply({
content: `Role ${role.name} is already added to the message.`, content: functionLocale.roleAlreadyAdded.replaceAll('$$ROLE$$', `${role.name}`),
ephemeral: true ephemeral: true
}); });
} }
} else { } else {
await interaction.reply({content: 'Please select a menu message.', ephemeral: true}); await interaction.reply({content: functionLocale.notMenuMessage, ephemeral: true});
} }
} else { } else {
await interaction.reply({ await interaction.reply({content: functionLocale.messageIdNotFound, ephemeral: true});
content: 'The message ID does not exist in this channel.',
ephemeral: true
});
} }
} else { } else {
await interaction.reply({ await interaction.reply({content: functionLocale.invalidMessageId, ephemeral: true})
content: 'The message ID was invalid.', ephemeral: true
})
} }
} else { } else {
await interaction.reply({content: 'Please select a text channel.', ephemeral: true}); await interaction.reply({content: functionLocale.channelNotText, ephemeral: true});
} }
} }
async function deleteRoleFromMenu(interaction) { async function deleteRoleFromMenu(interaction) {
const config = await ServerConfig.findOne({guildID: interaction.guild.id.toString()});
let locale = 'en';
if (config != null) {
locale = config.locale;
}
const functionLocale = locales.get(locale).components.roleMenu;
if (!(await checkPerm(interaction.member, 'role_menu'))) { if (!(await checkPerm(interaction.member, 'role_menu'))) {
await interaction.reply({content: 'You are not authorized to do this.', ephemeral: true}) await interaction.reply({content: functionLocale.accessDenied, ephemeral: true})
return; return;
} }
const channel = interaction.options.getChannel('channel'); const channel = interaction.options.getChannel('channel');
...@@ -166,33 +198,39 @@ async function deleteRoleFromMenu(interaction) { ...@@ -166,33 +198,39 @@ async function deleteRoleFromMenu(interaction) {
.splice(index2, 1); .splice(index2, 1);
await message.edit({content: message.content, components: message.components}); await message.edit({content: message.content, components: message.components});
await menu.save(); await menu.save();
await interaction.reply({content: 'Role has been removed from menu.', ephemeral: true}); await interaction.reply({
content: functionLocale.roleRemoved.replaceAll('$$ROLE$$', `${role.name}`),
ephemeral: true
});
} else { } else {
await interaction.reply({ await interaction.reply({
content: 'This role has not been added to the menu.', content: functionLocale.roleNotRemoved.replaceAll('$$ROLE$$', `${role.name}`),
ephemeral: true ephemeral: true
}); });
} }
} else { } else {
await interaction.reply({content: 'Please select a menu message.', ephemeral: true}); await interaction.reply({content: functionLocale.notMenuMessage, ephemeral: true});
} }
} else { } else {
await interaction.reply({ await interaction.reply({content: functionLocale.messageIdNotFound, ephemeral: true});
content: 'The message ID does not exist in this channel.',
ephemeral: true
});
} }
} else { } else {
await interaction.reply({ await interaction.reply({content: functionLocale.invalidMessageId, ephemeral: true})
content: 'The message ID was invalid.', ephemeral: true
})
} }
} else {
await interaction.reply({content: functionLocale.channelNotText, ephemeral: true});
} }
} }
async function editRoleMenu(interaction) { async function editRoleMenu(interaction) {
const config = await ServerConfig.findOne({guildID: interaction.guild.id.toString()});
let locale = 'en';
if (config != null) {
locale = config.locale;
}
const functionLocale = locales.get(locale).components.roleMenu;
if (!(await checkPerm(interaction.member, 'role_menu'))) { if (!(await checkPerm(interaction.member, 'role_menu'))) {
await interaction.reply({content: 'You are not authorized to do this.', ephemeral: true}) await interaction.reply({content: functionLocale.accessDenied, ephemeral: true})
return; return;
} }
const channel = interaction.options.getChannel('channel'); const channel = interaction.options.getChannel('channel');
...@@ -226,21 +264,19 @@ async function editRoleMenu(interaction) { ...@@ -226,21 +264,19 @@ async function editRoleMenu(interaction) {
menu.verification = verification; menu.verification = verification;
} }
await menu.save(); await menu.save();
await interaction.reply({content: 'Menu has been updated.', ephemeral: true}); await interaction.reply({content: functionLocale.menuUpdated, ephemeral: true});
} else { } else {
await interaction.reply({content: 'Please select a menu message.', ephemeral: true}); await interaction.reply({content: functionLocale.notMenuMessage, ephemeral: true});
} }
} else { } else {
await interaction.reply({ await interaction.reply({content: functionLocale.messageIdNotFound, ephemeral: true});
content: 'The message ID does not exist in this channel.',
ephemeral: true
});
} }
} else { } else {
await interaction.reply({ await interaction.reply({content: functionLocale.invalidMessageId, ephemeral: true})
content: 'The message ID was invalid.', ephemeral: true
})
} }
} else {
await interaction.reply({content: functionLocale.channelNotText, ephemeral: true});
} }
} }
...@@ -264,6 +300,12 @@ const slashCommandHandler = async (interaction) => { ...@@ -264,6 +300,12 @@ const slashCommandHandler = async (interaction) => {
const selectMenuHandler = async (interaction) => { const selectMenuHandler = async (interaction) => {
if (interaction.customId === 'selectRole') { if (interaction.customId === 'selectRole') {
const config = await ServerConfig.findOne({guildID: interaction.guild.id.toString()});
let locale = 'en';
if (config != null) {
locale = config.locale;
}
const functionLocale = locales.get(locale).components.roleMenu;
const guild = interaction.guild; const guild = interaction.guild;
const channel = interaction.channel; const channel = interaction.channel;
const message = interaction.message; const message = interaction.message;
...@@ -272,32 +314,20 @@ const selectMenuHandler = async (interaction) => { ...@@ -272,32 +314,20 @@ const selectMenuHandler = async (interaction) => {
channelID: channel.id, channelID: channel.id,
messageID: message.id messageID: message.id
}); });
console.log("1");
if (interaction.values[0] != 'placeholder') {
console.log("2");
if (menu != null) { if (menu != null) {
console.log("3");
if (interaction.values[0] === 'select_reset') { if (interaction.values[0] === 'select_reset') {
console.log("4");
const roleIDs = menu.roles.map(role => role.roleID); const roleIDs = menu.roles.map(role => role.roleID);
roleIDs.forEach(roleID => interaction.member.roles.remove(roleID)); roleIDs.forEach(roleID => interaction.member.roles.remove(roleID));
await interaction.reply({content: 'Your role has been removed.', ephemeral: true}); await interaction.reply({content: functionLocale.interactionMessage.roleRemoved, ephemeral: true});
} else { } else {
console.log("5");
const roleIDs = menu.roles.map(role => role.roleID); const roleIDs = menu.roles.map(role => role.roleID);
roleIDs.forEach(roleID => interaction.member.roles.remove(roleID)); roleIDs.forEach(roleID => interaction.member.roles.remove(roleID));
interaction.member.roles.add(roleIDs[menu.roles.map(role => role.value).indexOf(interaction.values[0])]); interaction.member.roles.add(roleIDs[menu.roles.map(role => role.value).indexOf(interaction.values[0])]);
await interaction.reply({content: 'You have received your role.', ephemeral: true}); await interaction.reply({content: functionLocale.interactionMessage.roleAdded, ephemeral: true});
} }
} else { } else {
await interaction.reply({content: 'Internal Error. Please contact admins.', ephemeral: true}); await interaction.reply({content: functionLocale.internalError, ephemeral: true});
} }
} else {
await interaction.reply({
content: 'This message does not have any roles yet. Please contact your administrator.',
ephemeral: true
});
}
} }
} }
module.exports = { module.exports = {
......
...@@ -5,10 +5,16 @@ const botConfig = { ...@@ -5,10 +5,16 @@ const botConfig = {
token: process.env.DISCORD_API_TOKEN, token: process.env.DISCORD_API_TOKEN,
}; };
const locale = require('./util/locale')
// Connecting Database. // Connecting Database.
connectDB(); connectDB();
// Initializing localizations
locale.init()
// Collecting all bot components. // Collecting all bot components.
const components = []; const components = [];
...@@ -19,6 +25,8 @@ for (const file of componentFiles) { ...@@ -19,6 +25,8 @@ for (const file of componentFiles) {
components.push(component); components.push(component);
} }
// Creating the discord client.
const client = new Client({intents: [Intents.FLAGS.GUILDS]}); const client = new Client({intents: [Intents.FLAGS.GUILDS]});
client.on('ready', () => { client.on('ready', () => {
...@@ -27,26 +35,20 @@ client.on('ready', () => { ...@@ -27,26 +35,20 @@ client.on('ready', () => {
client.on('interactionCreate', async (interaction) => { client.on('interactionCreate', async (interaction) => {
if (interaction.isCommand()) { if (interaction.isCommand()) {
components.forEach( component => { components.forEach(component => {
if (component.slashCommandHandler != null) { if (component.slashCommandHandler !== undefined) {
component.slashCommandHandler(interaction); component.slashCommandHandler(interaction);
}
});
} else if (interaction.isButton()) {
components.forEach( component => {
if (component.buttonHandler != null) {
component.buttonHandler(interaction);
} }
}); });
} else if (interaction.isButton()) { } else if (interaction.isButton()) {
components.forEach( component => { components.forEach(component => {
if (component.buttonHandler != null) { if (component.buttonHandler !== undefined) {
component.buttonHandler(interaction); component.buttonHandler(interaction);
} }
}); });
} else if (interaction.isSelectMenu()) { } else if (interaction.isSelectMenu()) {
components.forEach( component => { components.forEach(component => {
if (component.selectMenuHandler != null) { if (component.selectMenuHandler !== undefined) {
component.selectMenuHandler(interaction); component.selectMenuHandler(interaction);
} }
}); });
......
{
"locale": "de",
"commonName": "Deutsch",
"translations": {
"slashCommands": {
"assign_permission": {
"description": "Eine Berechtigung einer Rolle hinzufügen.",
"permissionDescription": "Berechtigung, die hinzugefügt werden soll.",
"permissionChoices": {
"roleMenus": "Rollenmenüs",
"locale": "Sprache wechseln"
},
"roleDescription": "Rolle, der die Berechtigung hinzugefügt werden soll."
},
"revoke_permission": {
"description": "Einer Rolle eine Berechtigung entziehen.",
"permissionDescription": "Berechtigung, die entzogen werden soll.",
"permissionChoices": {
"roleMenus": "Rollenmenüs",
"locale": "Sprache wechseln"
},
"roleDescription": "Rolle, der die Berechtigung entzogen werden soll."
},
"add_role_menu": {
"description": "Ein neues Rollenmenü erstellen.",
"channelDescription": "Kanal, in den das Rollenmenü gelegt werden soll",
"messageDescription": "Nachricht des Rollenmenüs.",
"typeDescription": "NOT YET IMPLEMENTED Typ des Rollenmenüs.",
"typeChoices": {
"oneOfN": "Bis zu eine Rolle aus N",
"mOfN": "Bis zu M Rollen aus N",
"anyOfN": "Bis zu N Rollen aus N"
},
"verificationDescription": "NOT YET IMPLEMENTED Ist Fllaami Verifikation für dieses Rollenmenü notwendig?"
},
"delete_role_menu": {
"description": "Ein Rollenmenü löschen.",
"channelDescription": "Kanal des zu löschenden Menüs.",
"messageIDDescription": "Nachrichten ID des zu löschenden Menüs."
},
"edit_role_menu": {
"description": "Ein Rollenmenü bearbeiten.",
"channelDescription": "Kanal des zu bearbeitenden Menüs.",
"messageIDDescription": "Nachrichten ID des zu bearbeitenden Menüs.",
"messageDescription": "Message content.",
"typeDescription": "NOT YET IMPLEMENTED Typ des Rollenmenüs.",
"typeChoices": {
"oneOfN": "Bis zu eine Rolle aus N",
"mOfN": "Bis zu M Rollen aus N",
"anyOfN": "Bis zu N Rollen aus N"
},
"verificationDescription": "NOT YET IMPLEMENTED Ist Fllaami Verifikation für dieses Rollenmenü notwendig?."
},
"delete_role_from_menu": {
"description": "Rolle aus Rollenmenü löschen.",
"channelDescription": "Kanal des zu bearbeitenden Menüs.",
"messageIDDescription": "Nachrichten ID des zu bearbeitenden Menüs.",
"roleDescription": "Zu entfernende Rolle."
},
"add_role_to_menu": {
"description": "Einem Rollenmenü eine Rolle hinzufügen.",
"channelDescription": "Kanal des zu bearbeitenden Menüs.",
"messageIDDescription": "Nachrichten ID des zu bearbeitenden Menüs.",
"roleDescription": "Hinzuzufügende Rolle."
},
"list_permissions": {
"description": "Alle Berechtigungen des Servers auflisten."
},
"change_locale": {
"description": "Die Sprache des Servers wechseln.",
"localeDescription": "Sprache, zu der gewechselt werden soll."
}
},
"components": {
"locale": {
"authDenied": "Du darfst die Sprache des Bots auf diesem Server nicht ändern.",
"localeApplied": "Du hast die Sprache des Bots auf Deutsch gestellt.",
"sameLocale": "Die Sprache des Servers ist bereits Deutsch."
},
"permConfig": {
"replyAssign": "Der Rolle $$ROLE$$ wurde die $$PERM$$ Berechtigung hinzugefügt.",
"replyRevoke": "Der Rolle $ROLE$$ wurde die $$PERM$$ Berechtigung entzogen.",
"replyList": "Liste der Berechtigungen:\n\nRollenmenüs: $$ROLEMENU$$\nSprache ändern $$LOCALE$$",
"accessDenied": "Nur administratoren dürfen diesen Command nutzen."
},
"roleMenu": {
"accessDenied": "Du darfst diesen Command nicht nutzen.",
"channelNotText": "Bitte wähle einen Textkanal aus.",
"invalidMessageId": "Die Nachrichten ID war nicht gültig (falsches Format).'",
"messageIdNotFound": "Die Nachrichten ID gehört nicht zu einer Nachricht in diesem Kanal.",
"notMenuMessage": "Bitte wähle eine Nachricht, die zu einem Rollenmenü gehört, aus.",
"roleMenuCreated": "Das Rollenmenü wurde erstellt.",
"roleMenuDeleted": "Das Rollenmenü wurde gelöscht.",
"roleMenuDeletedFailed": "Das Rollenmenü konnte nicht gelöscht werden.",
"roleAdded": "Die Rolle $$ROLE$$ wurde dem Menü hinzugefügt.",
"roleAlreadyAdded": "Die Rolle $$ROLE$$ ist bereits in der Nachricht.",
"roleRemoved": "Die Rolle $$ROLE$$ Wurde aus dem Menü entfernt.",
"roleNotRemoved": "Die Rolle $$ROLE$$ wurde dem Menü noch nicht hinzugefügt.",
"menuUpdated": "Das Menü wurde aktualisiert.",
"interactionMessage": {
"roleAdded": "Du hast deine Rolle erhalten.",
"roleRemoved": "Deine Rolle wurde entfernt."
},
"internalError": "Interner Fehler. Bitte kontaktier einen Admin."
}
},
"misc": {
"messageTemplates": {
"roleDropdown": {
"placeholder": "Wähle eine Rolle aus.",
"selectResetText": "Auswahl zurücksetzen."
}
}
}
}
}
\ No newline at end of file
{
"locale": "en",
"commonName": "English",
"translations": {
"slashCommands": {
"assign_permission": {
"description": "Assign a permission to a role.",
"permissionDescription": "Permission to assign.",
"permissionChoices": {
"roleMenus": "Role Menus",
"locale": "Change Locales"
},
"roleDescription": "Role to assign permission to."
},
"revoke_permission": {
"description": "Revoke a permission from a role.",
"permissionDescription": "Permission to revoke.",
"permissionChoices": {
"roleMenus": "Role Menus",
"locale": "Change Locales"
},
"roleDescription": "Role to revoke permission from."
},
"add_role_menu": {
"description": "Create a new role menu.",
"channelDescription": "Channel the menu will be put inside.",
"messageDescription": "Message content.",
"typeDescription": "NOT YET IMPLEMENTED Sets type of role menu.",
"typeChoices": {
"oneOfN": "One of N",
"mOfN": "M of N",
"anyOfN": "Any of N"
},
"verificationDescription": "NOT YET IMPLEMENTED Sets verification state of role menu."
},
"delete_role_menu": {
"description": "Delete a role menu.",
"channelDescription": "Select channel of to be deleted menu.",
"messageIDDescription": "Message ID of to be deleted menu."
},
"edit_role_menu": {
"description": "Edit an existing role menu.",
"channelDescription": "Channel of the menu.",
"messageIDDescription": "Message ID of the menu.",
"messageDescription": "Message content.",
"typeDescription": "NOT YET IMPLEMENTED Sets type of role menu.",
"typeChoices": {
"oneOfN": "One of N",
"mOfN": "M of N",
"anyOfN": "Any of N"
},
"verificationDescription": "NOT YET IMPLEMENTED Sets verification state of role menu."
},
"delete_role_from_menu": {
"description": "Delete role from menu.",
"channelDescription": "Channel of the menu.",
"messageIDDescription": "Message ID of the menu.",
"roleDescription": "Role to remove from the menu."
},
"add_role_to_menu": {
"description": "Add role to a menu.",
"channelDescription": "Channel of the menu.",
"messageIDDescription": "Message ID of the menu.",
"roleDescription": "Role to add to the menu."
},
"list_permissions": {
"description": "List all permissions for this Server."
},
"change_locale": {
"description": "Change locale of the bot on this server.",
"localeDescription": "Locale you want to change to bot to."
}
},
"components": {
"locale": {
"authDenied": "You are not authorized to change the locale of the server.",
"localeApplied": "You changed the locale of the bot to English.",
"sameLocale": "The locale of the server is already english."
},
"permConfig": {
"replyAssign": "Role $$ROLE$$ has been aded to $$PERM$$ permission.",
"replyRevoke": "Role $$ROLE$$ has been removed from $$PERM$$ permission.",
"replyList": "List of Permissions:\n\nRole Menu: $$ROLEMENU$$\nLocale: $$LOCALE$$",
"accessDenied": "Only administrators can access this command."
},
"roleMenu": {
"accessDenied": "You are not authorized to do this.",
"channelNotText": "Please select a text channel.",
"invalidMessageId": "The message ID was invalid.'",
"messageIdNotFound": "The message ID does not exist in this channel.",
"notMenuMessage": "Please select a menu message.",
"roleMenuCreated": "Role Menu has been created.",
"roleMenuDeleted": "Role Menu has been deleted.",
"roleMenuDeletedFailed": "Deleting the Menu has failed.",
"roleAdded": "Role $$ROLE$$ has been added to the message.",
"roleAlreadyAdded": "Role $$ROLE$$ is already added to the message.",
"roleRemoved": "Role $$ROLE$$ has been removed from menu.",
"roleNotRemoved": "Role $$ROLE$$ has not yet been added to the menu.",
"menuUpdated": "Menu has been updated",
"interactionMessage": {
"roleAdded": "You have received your role.",
"roleRemoved": "Your role has been removed."
},
"internalError": "Internal Error. Please contact admins."
}
},
"misc": {
"messageTemplates": {
"roleDropdown": {
"placeholder": "Select a role.",
"selectResetText": "Reset selection."
}
}
}
}
}
\ No newline at end of file
...@@ -2,28 +2,16 @@ const {Schema, model} = require('mongoose'); ...@@ -2,28 +2,16 @@ const {Schema, model} = require('mongoose');
const RoleMenuSchema = new Schema({ const RoleMenuSchema = new Schema({
guildID: { guildID: {
type: Number, type: String,
required: true, required: true,
validate: {
validator: Number.isInteger,
message: '{VALUE} is not an integer value'
}
}, },
channelID: { channelID: {
type: Number, type: String,
required: true, required: true,
validate: {
validator: Number.isInteger,
message: '{VALUE} is not an integer value'
}
}, },
messageID: { messageID: {
type: Number, type: String,
required: true, required: true,
validate: {
validator: Number.isInteger,
message: '{VALUE} is not an integer value'
}
}, },
verification: { verification: {
type: Boolean, type: Boolean,
......
...@@ -6,10 +6,19 @@ const ServerConfigSchema = new Schema({ ...@@ -6,10 +6,19 @@ const ServerConfigSchema = new Schema({
unique: true, unique: true,
required: true required: true
}, },
locale: {
type: String,
required: true,
default: 'en'
},
permissions: { permissions: {
roleMenu: [{ roleMenu: [{
type: String type: String
}]} }],
locale: [{
type: String
}]
}
}); });
......
const {REST} = require('@discordjs/rest');
const {Routes} = require('discord-api-types/v9');
const {Client, Intents} = require('discord.js'); const {Client, Intents} = require('discord.js');
const fs = require('fs'); const connectDB = require('./util/db');
const botConfig = { const botConfig = {
token: process.env.DISCORD_API_TOKEN, token: process.env.DISCORD_API_TOKEN,
applicationID: process.env.DISCORD_APP_ID applicationID: process.env.DISCORD_APP_ID
}; };
const locale = require('./util/locale');
const registerCommands = require('./util/registerCommands');
// Initializing Database
connectDB()
// Initializing locales
locale.init();
// Loading discord client to get all guild IDs // Loading discord client to get all guild IDs
const client = new Client({intents: [Intents.FLAGS.GUILDS]}); const client = new Client({intents: [Intents.FLAGS.GUILDS]});
client.on('ready', () => { client.on('ready', () => {
// Getting all command Javascript files from the commands folder
commands = []
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
commands.push(command);
}
const rest = new REST({version: '9'}).setToken(botConfig.token);
(async () => { (async () => {
try { try {
...@@ -29,15 +27,10 @@ client.on('ready', () => { ...@@ -29,15 +27,10 @@ client.on('ready', () => {
const guildIDs = await client.guilds.cache.map(guild => guild.id); const guildIDs = await client.guilds.cache.map(guild => guild.id);
console.log(guildIDs) console.log(guildIDs)
//Going through all guildIDs and registering the commands with each one. //Going through all guildIDs and registering the commands with each one.
await Promise.all(guildIDs.map(async (guildID) => { await Promise.all(guildIDs.map(async (guildID) => registerCommands(guildID, './commands')));
console.log(guildID);
await rest.put(
Routes.applicationGuildCommands(botConfig.applicationID, guildID),
{body: commands},
);
}));
console.log('Successfully reloaded application (/) commands.'); console.log('Successfully reloaded application (/) commands.');
process.exit(0);
} catch (error) { } catch (error) {
console.error(error); console.error(error);
} }
......
const fs = require('fs');
let locales = null;
module.exports = {
get: (locale) => {
if (locales[locale] !== undefined) {
return locales[locale];
} else {
return locales["en"];
}
},
init: () => {
locales = new Object();
const localeFiles = fs.readdirSync('./locales').filter(file => file.endsWith('.json'));
localeFiles.forEach((localeFile) => {
const locale = JSON.parse(fs.readFileSync(`./locales/${localeFile}`));
locales[locale.locale] = locale.translations;
});
}
}
\ No newline at end of file
...@@ -8,10 +8,9 @@ module.exports = async (member, permission) => { ...@@ -8,10 +8,9 @@ module.exports = async (member, permission) => {
return false; return false;
} }
if (permission === 'role_menu') { if (permission === 'role_menu') {
//console.log(member.roles.cache);
//console.log(config.permissions.roleMenu);
console.log("ASD");
return member.roles.cache.some((role) => config.permissions.roleMenu.some((prole) => prole == role.id.toString())) || member.permissions.has(Permissions.FLAGS.ADMINISTRATOR); return member.roles.cache.some((role) => config.permissions.roleMenu.some((prole) => prole == role.id.toString())) || member.permissions.has(Permissions.FLAGS.ADMINISTRATOR);
} else if (permission === 'locale') {
return member.roles.cache.some((role) => config.permissions.locale.some((prole) => prole == role.id.toString())) || member.permissions.has(Permissions.FLAGS.ADMINISTRATOR);
} }
return false; return false;
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment