Skip to content

Commit

Permalink
Add final changes to remote
Browse files Browse the repository at this point in the history
  • Loading branch information
SherRao committed Aug 21, 2022
1 parent e9e7503 commit 10782e0
Show file tree
Hide file tree
Showing 10 changed files with 237 additions and 176 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
config.js

# Logs
logs
*.log
Expand Down Expand Up @@ -104,3 +102,5 @@ dist

# TernJS port file
.tern-port

config.js
2 changes: 1 addition & 1 deletion src/commands/minecraft.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const execute = async (interaction) => {
const userId = interaction.author.id;
const verifiedUser = await getVerifiedUser(userId);
if(!verifiedUser)
return await interaction.reply("This command can only be used by users who are verified!");
return await interaction.reply("This command can only be used by users who are verified!");

//get user from firestore table.
//check if user already has a minecraft user in the table.
Expand Down
130 changes: 73 additions & 57 deletions src/commands/team.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
const { SlashCommandBuilder } = require("@discordjs/builders");
const { getVerifiedUser } = require("../util/users");
const { createTeam, deleteTeam, getTeam, inviteUserToTeam, leaveTeam } = require("../util/teams");
const { addUserToTeam, createTeam, getTeam } = require("../util/teams");
const { logger } = require("../index");
const { config } = require("../config");

const data = new SlashCommandBuilder()
.setName("team")
.setDescription("Command for team management.")
.addSubcommand(subCommand =>
subCommand.setName("create")
.setDescription("Create a team.")
.addStringOption(option =>
option.setName("name")
.setDescription("The name of the team.")
.setRequired(true)
)
)

.addSubcommand(subCommand =>
Expand All @@ -23,113 +20,132 @@ const data = new SlashCommandBuilder()
.setDescription("The user to invite.")
.setRequired(true)
)
)

.addSubcommand(subCommand =>
subCommand.setName("delete")
.setDescription("Delete your team only if you created the team.")
)

.addSubcommand(subCommand =>
subCommand.setName("leave")
.setDescription("Leave your team.")
)

.addSubcommand(subCommand =>
subCommand.setName("dconfirm")
.setDescription("Delete your team only if you created the team.")
)

.addSubcommand(subCommand =>
subCommand.setName("lconfirm")
.setDescription("Leave your team.")
);

// .addSubcommand(subCommand =>
// subCommand.setName("delete")
// .setDescription("Delete your team only if you created the team.")
// )

// .addSubcommand(subCommand =>
// subCommand.setName("leave")
// .setDescription("Leave your team.")
// )

// .addSubcommand(subCommand =>
// subCommand.setName("dconfirm")
// .setDescription("Delete your team only if you created the team.")
// )

// .addSubcommand(subCommand =>
// subCommand.setName("lconfirm")
// .setDescription("Leave your team.")
// );

const execute = async (interaction) => {
const userId = interaction.author.id;
const verifiedUser = getVerifiedUser(userId);
const userId = interaction.member.id;
const verifiedUser = await getVerifiedUser(userId);
if(!verifiedUser)
return await interaction.reply("This command can only be used by users who are verified!");
return await interaction.reply("This command can only be used by users who are verified!");

const teamId = verifiedUser.teamId;
const team = teamId ? getTeam(teamId) : null;
const team = teamId ? await getTeam(teamId) : null;
const captainId = team ? team.captain : null;

const subCommand = interaction.options.getSubcommand();
switch(subCommand) {
case "create": {
if(interaction.channelId !== config.discord.teamCreationChannelId)
return await interaction.reply("❌ This command can only be used in the create-a-team channel!");

if(team)
return await interaction.reply("❌ You are already in a team!");

const name = interaction.options.getStringOption("name");
createTeam(verifiedUser, name);
const teamName = await createTeam(userId, interaction.guild);
await interaction.reply("✅ Your team has been created! Go to your team channel to start inviting people!");
logger.info("Created team: " + teamName + " by user: " + interaction.member.user.username);
break;
}

case "invite": {
if(!config.discord.teamTextCategoryIds.includes(interaction.channel.parentId))
return await interaction.reply("❌ This command can only be used in your team channel!");

if(interaction.channelId === config.discord.teamCreationChannelId || interaction.channelId === config.discord.lftChannelId)
return await interaction.reply("❌ This command can only be used in your team channel!");

if(!team)
return await interaction.reply("❌ You are not in a team!");

if(!captainId)
if(captainId != userId)
return await interaction.reply("❌ You are not the captain of your team!");

const targetId = interaction.options.getUserOption("target").id;
const targetId = interaction.options.getUser("target").id;
if(targetId == userId)
return await interaction.reply("❌ You cannot invite yourself!");

const verifiedTarget = getVerifiedUser(targetId);
const verifiedTarget = await getVerifiedUser(targetId);
if(!verifiedTarget)
return await interaction.reply("❌ This user is not verified!");

const targetTeamId = verifiedTarget.teamId;
if(targetTeamId)
return await interaction.reply("❌ This user is already in a team!");

inviteUserToTeam(team, verifiedUser, verifiedTarget);
await interaction.reply("✅ Teammates invited!");
await addUserToTeam(teamId, captainId, targetId, interaction.client);
logger.info("User: " + interaction.member.user.username + " invited user: " + verifiedTarget.email + " to team: " + team.name);
break;
}

case "delete": {
if(!team)
return await interaction.reply("❌ You are not in a team!");
await interaction.reply("❌ This command is not yet implemented!");
// if(!team)
// return await interaction.reply("❌ You are not in a team!");

if(!captainId)
return await interaction.reply("❌ You are not the captain of your team!");
// if(!captainId)
// return await interaction.reply("❌ You are not the captain of your team!");

await interaction.reply("⚠️ If you're sure, please type `/tean DELETECONFIRM`");
// await interaction.reply("⚠️ If you're sure, please type `/tean dconfirm`");
break;
}

case "confirm": {
if(!team)
return await interaction.reply("❌ You are not in a team!");
case "dconfirm": {
await interaction.reply("❌ This command is not yet implemented!");

if(!captainId)
return await interaction.reply("❌ You are not the captain of your team!");
// if(!team)
// return await interaction.reply("❌ You are not in a team!");

deleteTeam(team);
// if(!captainId)
// return await interaction.reply("❌ You are not the captain of your team!");

// deleteTeam(team);
break;
}

case "leave": {
if(!team)
return await interaction.reply("❌ You are not in a team!");
await interaction.reply("❌ This command is not yet implemented!");

// if(!team)
// return await interaction.reply("❌ You are not in a team!");

if(team.captainId == userId)
return await interaction.reply("❌ You can't leave a team you're the captain of! You have to delete your team first!");
// if(team.captainId == userId)
// return await interaction.reply("❌ You can't leave a team you're the captain of! You have to delete your team first!");

await interaction.reply("⚠️ If you're sure, please type `/tean LEAVECONFIRM`");
// await interaction.reply("⚠️ If you're sure, please type `/tean lconfirm`");
break;
}

case "lconfirm": {
if(!team)
return await interaction.reply("❌ You are not in a team!");
await interaction.reply("❌ This command is not yet implemented!");

// if(!team)
// return await interaction.reply("❌ You are not in a team!");

if(team.captainId == userId)
return await interaction.reply("❌ You can't leave a team you're the captain of! You have to delete your team first!");
// if(team.captainId == userId)
// return await interaction.reply("❌ You can't leave a team you're the captain of! You have to delete your team first!");

leaveTeam(team, verifiedUser);
// leaveTeam(team, verifiedUser);
break;
}

Expand Down
31 changes: 20 additions & 11 deletions src/commands/verify.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const { SlashCommandBuilder } = require("@discordjs/builders");
const { getVerifiedUser, getSheetsUser } = require("../util/users");
const { getVerifiedUser, isAccepted } = require("../util/users");
const { addVerifiedUser } = require("../util/users");
const { logger } = require("../index");
const { config } = require("../config");

const data = new SlashCommandBuilder()
.setName("verify")
Expand All @@ -12,18 +14,25 @@ const data = new SlashCommandBuilder()
);

const execute = async (interaction) => {
const userId = interaction.author.id;
const verifiedUser = await getVerifiedUser(userId);
if(verifiedUser)
return await interaction.reply("❌ This command can only be used by users who are NOT verified!");
try {
const user = interaction.user;
const verifiedUser = await getVerifiedUser(user.id);
if(interaction.channelId !== config.discord.verificationChannelId)
return await interaction.reply({content: "❌ This command can only be used in the verification channel!", ephemeral: true});

if(verifiedUser)
return await interaction.reply({content: "❌ The email specified isn't in our list of accepted users!", ephemeral: true});

const email = interaction.options.getStringOption("email");
const sheetsUser = await getSheetsUser(email);
if(!sheetsUser.isAccepted)
return await interaction.reply("❌ This command can only be used by users who have been officially accepted!");
const email = interaction.options.getString("email").trim();
const accepted = await isAccepted(user.id, email);
if(!accepted)
return await interaction.reply({content: "❌ This command can only be used by users who have been officially accepted!", ephemeral: true});

await addVerifiedUser(userId, interaction.author);
await interaction.reply("✔️ You are now verified!");
await interaction.reply({content: "✅ You are now verified! This channel will disappear in 5 seconds!", ephemeral: true});
await addVerifiedUser(user.id, interaction.member);
logger.info("Verified user: " + user.username + " with email: " + email);

} catch(err) { logger.error("ERR while trying to verify!"); }
};

module.exports = { data: data, execute: execute, enabled: true, roleRequired: "" };
9 changes: 6 additions & 3 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ module.exports = {
},

discord: {
teamCategoryId: "970876620648882186",
verifiedRoleId: "962115254018527372"

teamTextCategoryIds: ["970876620648882186", "974828419311337502", "974889688307499078", "974889712080781353"],
teamVoiceCategoryIds: ["974822922306859048", "974828362784706611", "974879777230049300", "974889785657278524"],
teamCreationChannelId: "970876864648347658",
lftChannelId: "962121464692215858",
verifiedRoleId: "962115254018527372",
verificationChannelId: "974726673620680754"
}
}
};
41 changes: 24 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const logger = require("js-logger");
const { Client, Collection, Intents } = require("discord.js");
const { REST } = require("@discordjs/rest");
const { Routes } = require("discord-api-types/v9");
const { NoPermissionEmbed } = require("./embeds/noPermission");
const { config } = require("./config");
require("dotenv").config();

const TOKEN = process.env.DISCORD_TOKEN;
Expand All @@ -17,6 +17,7 @@ const client = new Client({
Intents.FLAGS.GUILD_MEMBERS,
Intents.FLAGS.GUILD_EMOJIS_AND_STICKERS,
Intents.FLAGS.GUILD_PRESENCES,
Intents.FLAGS.DIRECT_MESSAGES
],
partials: ["MESSAGE", "CHANNEL", "REACTION"]
});
Expand All @@ -26,11 +27,11 @@ async function main() {
client.emit("shutdown");

logger.info("Graceful shutdown completed. Exiting...");
console.log("Process terminated");
logger.info("Process terminated");
});

process.on("SIGINT", () => {
console.log("Caught interrupt signal");
logger.info("Caught interrupt signal");
process.exit();
});

Expand All @@ -39,13 +40,19 @@ async function main() {
formatter: (messages, context) =>
messages.unshift(`[${new Date().toUTCString()}] [${context.level.name}]: `)
});
module.exports = { logger };

client.once("ready", () => {
registerCommands();
handleCommands();
logger.info("Bot loaded!");
});

client.on("messageCreate", message => {
if(message.channelId === config.discord.verificationChannelId && message.type != "APPLICATION_COMMAND")
message.delete();
});

client.login(TOKEN);
}

Expand Down Expand Up @@ -98,21 +105,21 @@ function handleCommands() {
if (!command)
return;

const roleRequired = command.roleRequired;
if(interaction.member.roles.cache.has(roleRequired)) {
try {
await command.execute(interaction);
// const roleRequired = command.roleRequired;
// if(interaction.member.roles.cache.has(roleRequired)) {
try {
await command.execute(interaction);

} catch (error) {
logger.error(error);
await interaction.reply({ content: "There was an error while executing this command!", ephemeral: true });
}

} else
await interaction.reply({
embeds: [NoPermissionEmbed],
ephemeral: true,
});
} catch (error) {
logger.error(error);
await interaction.reply({ content: "There was an error while executing this command!", ephemeral: true });
}

// } else
// await interaction.reply({
// embeds: [NoPermissionEmbed],
// ephemeral: true,
// });
});

}
Expand Down
Loading

0 comments on commit 10782e0

Please sign in to comment.