Commit d73437f7 authored by Yashin's avatar Yashin
Browse files

First working version of role distribution.

parent a30180de
.git
node_modules
npm-debug.log
.env
.env.example
.DS_store
\ No newline at end of file
DISCORD_API_TOKEN="enter_token_here"
DISCORD_APP_ID="enter_application_id_here"
MONGO_ROOT_USERNAME="root"
MONGO_ROOT_PASSWORD="admin"
MONGO_PORT=27017
\ No newline at end of file
### Node template
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
.env.test
# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
### Node template
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
.env.test
# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
.env
.DS_store
\ No newline at end of file
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/temp" />
<excludeFolder url="file://$MODULE_DIR$/.tmp" />
<excludeFolder url="file://$MODULE_DIR$/tmp" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptLibraryMappings">
<includedPredefinedLibrary name="Node.js Core" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/finf-discord-js.iml" filepath="$PROJECT_DIR$/.idea/finf-discord-js.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
FROM node:16
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "npm", "run", "dev" ]
\ No newline at end of file
const { SlashCommandBuilder } = require('@discordjs/builders');
module.exports = new SlashCommandBuilder()
.setName('add_role_menu')
.setDescription('Create a new role menu')
.addChannelOption( option =>
option.setName('channel')
.setDescription('Channel the menu will be put inside.')
.setRequired(true))
.addStringOption( option =>
option.setName('message')
.setDescription('Message content.')
.setRequired(true))
.addStringOption( option =>
option.setName('type')
.setDescription('NOT YET IMPLEMENTED Sets type of role menu.')
.setRequired(true)
.addChoice('One of N', 'one')
.addChoice('M of N', 'm')
.addChoice('Any of N', 'any'))
.addBooleanOption( option =>
option.setName('verification')
.setDescription('NOT YET IMPLEMENTED Sets verification state of role menu.')
.setRequired(true))
\ No newline at end of file
const { SlashCommandBuilder } = require('@discordjs/builders');
module.exports = new SlashCommandBuilder()
.setName('add_role_to_menu')
.setDescription('Add role to a menu.')
.addChannelOption( option =>
option.setName('channel')
.setDescription('Channel of the menu.')
.setRequired(true))
.addStringOption( option =>
option.setName('message_id')
.setDescription('Message ID of the menu.')
.setRequired(true))
.addRoleOption(option =>
option.setName('role')
.setDescription('Role to add to the menu.')
.setRequired(true))
\ No newline at end of file
const { SlashCommandBuilder } = require('@discordjs/builders');
module.exports = new SlashCommandBuilder()
.setName('delete_role_from_menu')
.setDescription('Delete role from menu.')
.addChannelOption( option =>
option.setName('channel')
.setDescription('Channel of the menu.')
.setRequired(true))
.addStringOption( option =>
option.setName('message_id')
.setDescription('Message ID of the menu.')
.setRequired(true))
.addRoleOption(option =>
option.setName('role')
.setDescription('Role to remove from the menu.')
.setRequired(true))
\ No newline at end of file
const { SlashCommandBuilder } = require('@discordjs/builders');
module.exports = new SlashCommandBuilder()
.setName('delete_role_menu')
.setDescription('Delete a role menu')
.addChannelOption( option =>
option.setName('channel')
.setDescription('Select channel of to be deleted menu')
.setRequired(true))
.addStringOption( option =>
option.setName('message_id')
.setDescription('Message ID of to be deleted menu')
.setRequired(true))
\ No newline at end of file
const { SlashCommandBuilder } = require('@discordjs/builders');
module.exports = new SlashCommandBuilder()
.setName('edit_role_menu')
.setDescription('Edit an existing role menu.')
.addChannelOption( option =>
option.setName('channel')
.setDescription('Channel of the menu.')
.setRequired(true))
.addStringOption( option =>
option.setName('message_id')
.setDescription('Message ID of the menu.')
.setRequired(true))
.addStringOption( option =>
option.setName('message')
.setDescription('Message content.'))
.addStringOption( option =>
option.setName('type')
.setDescription('NOT YET IMPLEMENTED')
.addChoice('One of N', 'one')
.addChoice('M of N', 'm')
.addChoice('Any of N', 'any'))
.addBooleanOption( option =>
option.setName('verification')
.setDescription('NOT YET IMPLEMENTED Sets verification state of role menu.'))
\ No newline at end of file
const RoleMenu = require('../models/RoleMenu');
const {MessageActionRow, MessageSelectMenu} = require("discord.js");
// slashcommand handler
async function addRoleMenu(interaction) {
const channel = interaction.options.getChannel('channel');
const messageContent = interaction.options.getString('message');
const type = interaction.options.getString('type');
const verification = interaction.options.getBoolean('verification');
const row1 = new MessageActionRow()
.addComponents(
new MessageSelectMenu()
.setCustomId('selectRole')
.setPlaceholder("Select a role.")
.addOptions([
{
label: 'Placeholder',
value: 'placeholder'
}
])
);
const msg = await channel.send({content: messageContent, components: [row1]});
let menu = new RoleMenu();
menu.guildID = channel.guild.id;
menu.channelID = channel.id;
menu.messageID = msg.id;
menu.verification = verification;
menu.type = type;
menu.roles = [];
await menu.save();
await interaction.reply({content: 'Role Menu has been created.', ephemeral: true});
}
async function deleteRoleMenu(interaction) {
const channel = interaction.options.getChannel('channel');
const messageID = interaction.options.getString('message_id');
let menu = await RoleMenu.findOne({guildID: channel.guild.id, channelID: channel.id, messageID});
if (channel.isText()) {
const message = await channel.messages.fetch(messageID);
if (message != null) {
if (menu != null) {
try {
await RoleMenu.deleteOne({_id: menu._id});
await message.delete();
await interaction.reply({content: 'Menu has been deleted.', ephemeral: true});
} catch (e) {
await interaction.reply({content: 'Deleting the Menu has failed.', ephemeral: true});
}
} else {
await interaction.reply({content: 'Please select a menu message.', ephemeral: true});
}
} else {
await interaction.reply({content: 'The message ID does not exist in this channel.', ephemeral: true});
}
} else {
await interaction.reply({content: 'Please select a text channel.', ephemeral: true});
}
}
async function addRoleToMenu(interaction) {
const channel = interaction.options.getChannel('channel');
const messageID = interaction.options.getString('message_id');
const role = interaction.options.getRole('role')
if (channel.isText()) {
var message = null;
var messageSuccess = true;
try {
message = await channel.messages.fetch(messageID);
} catch (e) {
messageSuccess = false;
}
if (messageSuccess) {
if (message != null) {
let menu = await RoleMenu.findOne({
guildID: channel.guild.id,
channelID: channel.id,
messageID
});
if (menu != null) {
let components = message.components;
if (components[0].components[0].options[0].value === 'placeholder') {
components[0].components[0].options = [];
}
if (!menu.roles.map((role) => role.roleID).includes(role.id.toString())) {
components[0].components[0].addOptions({
label: role.name,
value: role.name.toLowerCase().replace(" ", "_")
});
menu.roles.push({
text: role.name,
value: role.name.toLowerCase().replace(" ", "_"),
roleID: role.id
});
await menu.save();
await message.edit({content: message.content, components});
await interaction.reply({
content: `Role ${role.name} has been added to the message.`,
ephemeral: true
});
} else {
await interaction.reply({
content: `Role ${role.name} is already added to the message.`,
ephemeral: true
});
}
} else {
await interaction.reply({content: 'Please select a menu message.', ephemeral: true});
}
} else {
await interaction.reply({
content: 'The message ID does not exist in this channel.',
ephemeral: true
});
}
} else {
await interaction.reply({
content: 'The message ID was invalid.', ephemeral: true
})
}
} else {
await interaction.reply({content: 'Please select a text channel.', ephemeral: true});
}
}
async function deleteRoleFromMenu(interaction) {
const channel = interaction.options.getChannel('channel');
const messageID = interaction.options.getString('message_id');
const role = interaction.options.getRole('role');
if (channel.isText()) {
var message = null;
var messageSuccess = true;
try {
message = await channel.messages.fetch(messageID);
} catch (e) {
messageSuccess = false;
}
if (messageSuccess) {
if (message != null) {
let menu = await RoleMenu.findOne({
guildID: channel.guild.id,
channelID: channel.id,
messageID
});
if (menu != null) {
if (menu.roles.map((role) => role.roleID).includes(role.id.toString())) {
const value = menu.roles[menu.roles.map(role => role.roleID).indexOf(role.id.toString())].value;
const index1 = menu.roles.map(role => role.value).indexOf(value);
menu.roles.splice(index1, 1);
if (message.components[0].components[0].options.length === 1) {
message.components[0].components[0].addOptions({
label: "Placeholder",
value: "placeholder"
});
}
const index2 = message.components[0].components[0].options
.map(option => option.value).indexOf(value);
message.components[0].components[0].options
.splice(index2, 1);
await message.edit({content: message.content, components: message.components});
await menu.save();
await interaction.reply({content: 'Role has been removed from menu.', ephemeral: true});
} else {
await interaction.reply({
content: 'This role has not been added to the menu.',
ephemeral: true
});
}
} else {
await interaction.reply({content: 'Please select a menu message.', ephemeral: true});
}
} else {
await interaction.reply({
content: 'The message ID does not exist in this channel.',
ephemeral: true
});
}
} else {
await interaction.reply({
content: 'The message ID was invalid.', ephemeral: true
})
}
}
}
async function editRoleMenu(interaction) {
const channel = interaction.options.getChannel('channel');
const messageID = interaction.options.getString('message_id');
const messageContent = interaction.options.getString('message');
const type = interaction.options.getString('type');
const verification = interaction.options.getBoolean('verification');
if (channel.isText()) {
var message = null;
var messageSuccess = true;
try {
message = await channel.messages.fetch(messageID);
} catch (e) {
messageSuccess = false;
}
if (messageSuccess) {
if (message != null) {
let menu = await RoleMenu.findOne({
guildID: channel.guild.id,
channelID: channel.id,
messageID
});