Skip to content

Commit

Permalink
rides registration functional
Browse files Browse the repository at this point in the history
  • Loading branch information
DinoWw committed Oct 17, 2024
1 parent fdeb651 commit 42f9465
Show file tree
Hide file tree
Showing 13 changed files with 1,557 additions and 305 deletions.
24 changes: 17 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Setup

## Configuration
## 1. Configuration
sheets-credentials/credentials.json
```json
{
Expand All @@ -16,16 +16,26 @@ sheets-credentials/credentials.json
]
}
}
```
```env
.env
```
DISCORD_BOT_TOKEN=
DISCORD_CLIENT_ID=
DISCORD_GUILD_ID= # server ID
```

## Register commands
node deploy_commands.js
## 2. Register commands
`node deploy_commands.js`

## 3. Start bot
`node main.js`
If running with nodemon: `nodemon --ignore 'data/*' main.js`


# Modules
## sheets interface
- must call and await authorize() before any other function calls

## Start bot
node main.js
# Known bugs
- if two people have the same name, only one of them will ever get points.
you can bypass this by registering one of them as Name Surname 1
both in the table and through the bot
2 changes: 1 addition & 1 deletion commands/utility/bod.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const botId = 1164580596006260737;
module.exports = {
data: new SlashCommandBuilder()
.setName('bod')
.setDescription('Fetches relevant information!'),
.setDescription('Unosi bodove korisnicima iz threada'),
async execute(interaction) {
// await is needed so that future responses with followUp() do not raise errors
await interaction.deferReply({ephemeral: true});
Expand Down
2 changes: 1 addition & 1 deletion data/nameById.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"1012645988227358803":"Marting Ple","290249492584857603":"Dino Pel","356564352284753923":"lor"}
{"290249492584857603":"Barbara Bobeta","356564352284753923":"Lorena Ivani","734406365300260944":"Ema Zidar"}
31 changes: 31 additions & 0 deletions handlers/button/scoreOne.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const user = require("../../commands/utility/user.js");
const dataInterface = require("../../scripts/dataInterface.js");
const sheetsInterface = require("../../scripts/sheetsInterface.js");
const messageInterface = require("../../scripts/messageInterface.js");

module.exports = {
prefix: "one",
async execute(interaction) {

const userName = dataInterface.nameById(interaction.customId.split('_', 2)[1]);

const message = (await interaction.channel.messages.fetch({limit: 1, cache: false})).at(0);

const timeStamp = new Date(message.createdTimestamp);
const date = `${timeStamp.getDate()}.${timeStamp.getMonth()+1}.`;

sheetsInterface.enterPoints(userName, date, 1)
.then(() => {
console.log(`Entered one point to ${userName} on ${date}`);
interaction.update(messageInterface.scoreSuccessInfo(userName, date, 1));
}).catch((e) => {
if( e instanceof sheetsInterface.UnknownPersonError){
console.error(`Error entering one point to ${userName} on ${date}, no user ${userName}`);
interaction.update(messageInterface.noName(userName));
}
else{
throw e;
}
});
}
}
36 changes: 36 additions & 0 deletions handlers/button/scoreOneSolo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const user = require("../../commands/utility/user.js");
const dataInterface = require("../../scripts/dataInterface.js");
const sheetsInterface = require("../../scripts/sheetsInterface.js");
const messageInterface = require("../../scripts/messageInterface.js");
const { TimestampStyles } = require("discord.js");

module.exports = {
prefix: "oneSolo",
async execute(interaction) {

const userName = dataInterface.nameById(interaction.customId.split('_', 2)[1]);

const message = (await interaction.channel.messages.fetch({limit: 1, cache: false})).at(0);

const timeStamp = new Date(message.createdTimestamp);
const date = `${timeStamp.getDate()}.${timeStamp.getMonth()+1}.`;

sheetsInterface.enterSolo(userName, date, 1)
.then(() => {
console.log(`Entered one solo point to ${userName} on ${date}`);
interaction.update(messageInterface.scoreSuccessInfo(userName, date, 1, true));
}).catch((e) => {
if( e instanceof sheetsInterface.UnknownPersonError){
console.error(`Error entering one point to ${userName} on ${date}, no user ${userName}`);
interaction.update(messageInterface.noName(userName));
}
else if( e instanceof sheetsInterface.SoloRideError){
console.error(`Error entering one point to ${userName} on ${date}, too many solo rides`);
interaction.update(messageInterface.soloLimit(userName));
}
else{
throw e;
}
});
}
}
31 changes: 31 additions & 0 deletions handlers/button/scoreTwo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const user = require("../../commands/utility/user.js");
const dataInterface = require("../../scripts/dataInterface.js");
const sheetsInterface = require("../../scripts/sheetsInterface.js");
const messageInterface = require("../../scripts/messageInterface.js");

module.exports = {
prefix: "two",
async execute(interaction) {

const userName = dataInterface.nameById(interaction.customId.split('_', 2)[1]);

const message = (await interaction.channel.messages.fetch({limit: 1, cache: false})).at(0);

const timeStamp = new Date(message.createdTimestamp);
const date = `${timeStamp.getDate()}.${timeStamp.getMonth()+1}.`;

sheetsInterface.enterPoints(userName, date, 2)
.then(() => {
console.log(`Entered two points to ${userName} on ${date}`);
interaction.update(messageInterface.scoreSuccessInfo(userName, date, 2));
}).catch((e) => {
if( e instanceof sheetsInterface.UnknownPersonError){
console.error(`Error entering two points to ${userName} on ${date}, no user ${userName}`);
interaction.update(messageInterface.noName(userName));
}
else{
throw e;
}
});
}
}
36 changes: 36 additions & 0 deletions handlers/button/scoreTwoSolo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const user = require("../../commands/utility/user.js");
const dataInterface = require("../../scripts/dataInterface.js");
const sheetsInterface = require("../../scripts/sheetsInterface.js");
const messageInterface = require("../../scripts/messageInterface.js");
const { TimestampStyles } = require("discord.js");

module.exports = {
prefix: "twoSolo",
async execute(interaction) {

const userName = dataInterface.nameById(interaction.customId.split('_', 2)[1]);

const message = (await interaction.channel.messages.fetch({limit: 1, cache: false})).at(0);

const timeStamp = new Date(message.createdTimestamp);
const date = `${timeStamp.getDate()}.${timeStamp.getMonth()+1}.`;

sheetsInterface.enterSolo(userName, date, 2)
.then(() => {
console.log(`Entered two solo points to ${userName} on ${date}`);
interaction.update(messageInterface.scoreSuccessInfo(userName, date, 2, true));
}).catch((e) => {
if( e instanceof sheetsInterface.UnknownPersonError){
console.error(`Error entering two points to ${userName} on ${date}, no user ${userName}`);
interaction.update(messageInterface.noName(userName));
}
else if( e instanceof sheetsInterface.SoloRideError){
console.error(`Error entering two points to ${userName} on ${date}, too many solo rides`);
interaction.update(messageInterface.soloLimit(userName));
}
else{
throw e;
}
});
}
}
1 change: 1 addition & 0 deletions handlers/modal/registerHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = {
prefix: "register",
async execute(interaction, field, data) {

// TODO: error handling, forbid '_' and potentially other special characters in name
dataInterface.addNameForId(field.value, data).then(() => {
return interaction.reply(messageInterface.scoringPrompt(data));
});
Expand Down
11 changes: 4 additions & 7 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, Events, GatewayIntentBits } = require('discord.js');

const dataInterface = require('./scripts/dataInterface');
const messageInterface = require("./scripts/messageInterface.js");

require('dotenv').config()

// add timestamps in front of log messages
require('log-timestamp');

const sheetsInterface = require("./scripts/sheetsInterface.js");
sheetsInterface.authorize().then(()=>console.log("Google Sheets authentification complete"));

// Create a new client instance
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

Expand Down Expand Up @@ -42,7 +42,7 @@ for (const folder of commandFolders) {
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
console.log(`[WARNING] The command at ${filePath} is missing the required "data" or "execute" property.`);
}
}
}
Expand Down Expand Up @@ -81,7 +81,6 @@ for (const file of modalFiles) {
// setup command responses

client.on(Events.InteractionCreate, async interaction => {
// TODO: tu napravit handlere za button inpute
if (interaction.isChatInputCommand()){

const command = interaction.client.commands.get(interaction.commandName);
Expand All @@ -104,9 +103,7 @@ client.on(Events.InteractionCreate, async interaction => {
}

else if ( interaction.isButton() ){

const commandCode = interaction.customId.split('_', 1)[0];
console.log(buttonHandlers, commandCode);
// TODO: wrap in try catch in case interaction is not registered
buttonHandlers[commandCode](interaction);

Expand Down
Loading

0 comments on commit 42f9465

Please sign in to comment.