Skip to content

Commit

Permalink
Add a command to change timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuSchl committed Oct 1, 2023
1 parent 81d704f commit dc97564
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 5 deletions.
2 changes: 1 addition & 1 deletion clientDiscord/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: "3"
services:
karassistant-client-discord:
#build: .
image: codyisthesenate/karassistant-client-discord:1.1.1
image: codyisthesenate/karassistant-client-discord:1.1.2
restart: always
container_name: karassistant-client-discord
volumes:
Expand Down
14 changes: 14 additions & 0 deletions clientDiscord/events/interactionCreate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { Events } = require("discord.js");
const listCommands = require("../utils/loadCommands").listCommands;

module.exports.run = async (client) => {
client.on(Events.InteractionCreate, (interaction) => {
if (interaction.isChatInputCommand()) {
try {
listCommands[interaction.commandName](interaction);
} catch (error) {
interaction.reply("There is an error with the command");
}
}
});
};
4 changes: 3 additions & 1 deletion clientDiscord/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ async function start() {
});
});

client.login(TOKEN).then(() => {});
client.login(TOKEN).then(() => {
require("./utils/loadCommands").run(client);
});
}

start();
62 changes: 62 additions & 0 deletions clientDiscord/interactions/commands/setTimeZone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const { ApplicationCommandOptionType } = require("discord.js");
const updateUser = require("../../utils/updateUser").updateUser;

const listOfTimezones = [
"Pacific/Kiritimati",
"Pacific/Tongatapu",
"Pacific/Apia",
"Asia/Anadyr",
"Asia/Kamchatka",
"Asia/Tokyo",
"Asia/Hong_Kong",
"Asia/Kolkata",
"Asia/Dubai",
"Europe/Moscow",
"Europe/Paris",
"Europe/London",
"Africa/Cairo",
"Africa/Nairobi",
"Asia/Kuwait",
"Europe/Athens",
"Asia/Baghdad",
"Europe/Istanbul",
"Asia/Tehran",
"Asia/Kabul",
"Asia/Baku",
"Asia/Ashgabat",
"Asia/Muscat",
"Asia/Tashkent",
"Asia/Almaty",
];

const choices = [];
for (const timezone of listOfTimezones) {
choices.push({ name: timezone, value: timezone });
}

module.exports = {
data: {
name: "settimezone",
description: "Set the time zone, to get the time right",
options: [
{
type: ApplicationCommandOptionType.String,
name: "timezone",
description: "Value for the new time zone",
required: true,
choices,
},
],
},
async execute(interaction) {
await interaction.deferReply({ ephemeral: true });
const timeZone = interaction.options.get("timezone").value;
const userName = interaction.user.userName;
const userId = interaction.user.id;

const result = await updateUser({ userName: userName, userId: userId, data: { timeZone } });
return interaction.followUp(
result ? "Time zone has been updated" : "ERROR: Could not update time zone. Try again later."
);
},
};
4 changes: 2 additions & 2 deletions clientDiscord/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion clientDiscord/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "clientdiscord",
"version": "1.1.1",
"version": "1.1.2",
"description": "A discord bot for KarAssistant",
"main": "index.js",
"scripts": {
Expand Down
40 changes: 40 additions & 0 deletions clientDiscord/utils/loadCommands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const { REST, Routes } = require("discord.js");
const fs = require("fs");
require("dotenv").config();

const GUILD_ID = process.env.GUILD_ID;
const TOKEN = process.env.DISCORD_TOKEN;

const listCommands = {};
module.exports.listCommands = listCommands;

module.exports.run = async (client) => {
// Create all commands
const commands = [];
const commandFolders = fs.readdirSync(__dirname + "/../interactions/commands/");

for (const folder of commandFolders) {
// Grab all the command files from the commands directory you created earlier
const commandsPath = __dirname + "/../interactions/commands/" + folder;

const command = require(commandsPath);
if ("data" in command && "execute" in command) {
const data = command.data;
listCommands[data.name] = command.execute;
commands.push(data);
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}

// Construct and prepare an instance of the REST module
const rest = new REST().setToken(TOKEN);

// and deploy your commands!
try {
client.application.commands.set(commands, GUILD_ID);
} catch (error) {
// And of course, make sure you catch and log any errors!
console.error(error);
}
};

0 comments on commit dc97564

Please sign in to comment.