Skip to content

Commit

Permalink
Switch indentation to 2 spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
gruppler committed Jun 17, 2024
1 parent 70fa46a commit b4af28b
Show file tree
Hide file tree
Showing 23 changed files with 2,079 additions and 2,079 deletions.
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"tabWidth": 2,
"useTabs": true
"useTabs": false
}
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# README

1. Install NodeJS and NPM
2. Clone the repo (<https://github.com/humanat/takBot>)
3. Run `npm i`
4. Run `git submodule init && git submodule sync && git submodule update`
5. Run `pushd TPS-Ninja && npm i && popd`
6. Run `cp auth.json.template auth.json; cp results.db.template results.db`
7. Make your own application through Discord (<https://discord.com/developers/applications>)
8. Copy the token from the Bot page and paste it in `auth.json` after `token`.
9. Copy the Application ID from the General Information page and paste it in `auth.json` after `clientId`.
1. Install NodeJS and NPM
2. Clone the repo (<https://github.com/humanat/takBot>)
3. Run `npm i`
4. Run `git submodule init && git submodule sync && git submodule update`
5. Run `pushd TPS-Ninja && npm i && popd`
6. Run `cp auth.json.template auth.json; cp results.db.template results.db`
7. Make your own application through Discord (<https://discord.com/developers/applications>)
8. Copy the token from the Bot page and paste it in `auth.json` after `token`.
9. Copy the Application ID from the General Information page and paste it in `auth.json` after `clientId`.
10. Run `node deploy-commands`
11. Run `node bot` (`Ctrl+C` to end)
12. Invite your bot to your own Discord server by generating an invite link from the OAuth2 page of the Discord portal
1. Add redirect URL `https://discordapp.com/oauth2/authorize?&client_id=<ClientID>&scope=bot` with your client ID.
2. Select scopes `bot` and `messages.read`
3. Select the appropriate permissions
4. Press the Copy button in the Scopes section
5. Paste into a new browser tab
1. Add redirect URL `https://discordapp.com/oauth2/authorize?&client_id=<ClientID>&scope=bot` with your client ID.
2. Select scopes `bot` and `messages.read`
3. Select the appropriate permissions
4. Press the Copy button in the Scopes section
5. Paste into a new browser tab
4 changes: 2 additions & 2 deletions auth.json.template
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"token": "your-token-goes-here",
"clientId": "your-application-id-goes-here"
"token": "your-token-goes-here",
"clientId": "your-application-id-goes-here"
}
194 changes: 97 additions & 97 deletions bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ const path = require("path");
const auth = require("./auth.json");

const {
cleanupFiles,
createClient,
handleMove,
sendMessage,
setTimer,
validPly,
cleanupFiles,
createClient,
handleMove,
sendMessage,
setTimer,
validPly,
} = require("./util");

const { themes } = require("./TPS-Ninja/src/themes");
Expand All @@ -22,124 +22,124 @@ const client = createClient();
client.commands = new Discord.Collection();
const commandsPath = path.join(__dirname, "commands");
const commandFiles = fs
.readdirSync(commandsPath)
.filter((file) => file.endsWith(".js"));
.readdirSync(commandsPath)
.filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
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.`
);
}
const filePath = path.join(commandsPath, file);
const command = require(filePath);
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.`
);
}
}

// Main code

client.on(Discord.Events.ClientReady, () => {
console.log(`Logged in as ${client.user.tag}!`);

// Restore timers
const channelsDir = path.join(__dirname, "data");
if (!fs.existsSync(channelsDir)) {
fs.mkdirSync(channelsDir);
return;
}
const channels = fs.readdirSync(channelsDir);
channels.forEach((channelId) => {
const timersDir = path.join(channelsDir, channelId, "timers");
if (!fs.existsSync(timersDir)) {
return;
}
const timerFiles = fs.readdirSync(timersDir);
timerFiles.forEach((timerFilename) => {
const timerPath = path.join(timersDir, timerFilename);
const timer = require(timerPath);
if (timer && timer.timestamp && timer.type) {
setTimer(timer.type, timer.timestamp, channelId, timer.playerId);
} else {
console.log("Invalid timer:", timerPath);
}
});
});
console.log(`Logged in as ${client.user.tag}!`);

// Restore timers
const channelsDir = path.join(__dirname, "data");
if (!fs.existsSync(channelsDir)) {
fs.mkdirSync(channelsDir);
return;
}
const channels = fs.readdirSync(channelsDir);
channels.forEach((channelId) => {
const timersDir = path.join(channelsDir, channelId, "timers");
if (!fs.existsSync(timersDir)) {
return;
}
const timerFiles = fs.readdirSync(timersDir);
timerFiles.forEach((timerFilename) => {
const timerPath = path.join(timersDir, timerFilename);
const timer = require(timerPath);
if (timer && timer.timestamp && timer.type) {
setTimer(timer.type, timer.timestamp, channelId, timer.playerId);
} else {
console.log("Invalid timer:", timerPath);
}
});
});
});

client.on(Discord.Events.InteractionCreate, async (interaction) => {
if (interaction.isAutocomplete()) {
// Handle autocomplete

const focusedOption = interaction.options.getFocused(true);
if (focusedOption.name === "theme") {
const focusedValue = focusedOption.value.trim().toLowerCase();
return interaction.respond(
themeIDs
.filter((choice) => choice.startsWith(focusedValue))
.map((choice) => ({ name: choice, value: choice }))
);
}
} else if (interaction.isChatInputCommand()) {
// Handle commands

const command = interaction.client.commands.get(interaction.commandName);

if (!command) {
console.error(
`No command matching ${interaction.commandName} was found.`
);
return;
}

try {
await command.execute(interaction, client);
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({
content: "There was an error while executing this command!",
ephemeral: true,
});
} else {
await interaction.reply({
content: "There was an error while executing this command!",
ephemeral: true,
});
}
}
}
if (interaction.isAutocomplete()) {
// Handle autocomplete

const focusedOption = interaction.options.getFocused(true);
if (focusedOption.name === "theme") {
const focusedValue = focusedOption.value.trim().toLowerCase();
return interaction.respond(
themeIDs
.filter((choice) => choice.startsWith(focusedValue))
.map((choice) => ({ name: choice, value: choice }))
);
}
} else if (interaction.isChatInputCommand()) {
// Handle commands

const command = interaction.client.commands.get(interaction.commandName);

if (!command) {
console.error(
`No command matching ${interaction.commandName} was found.`
);
return;
}

try {
await command.execute(interaction, client);
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({
content: "There was an error while executing this command!",
ephemeral: true,
});
} else {
await interaction.reply({
content: "There was an error while executing this command!",
ephemeral: true,
});
}
}
}
});

client.on(Discord.Events.MessageCreate, (msg) => {
let message = msg.content.trim();
if (
message.length >= 4 &&
["!tak", "!rng"].includes(message.substring(0, 4).toLowerCase())
) {
sendMessage(msg, "Please use my new slash commands!");
} else if (validPly(message)) {
return handleMove(msg, message);
}
let message = msg.content.trim();
if (
message.length >= 4 &&
["!tak", "!rng"].includes(message.substring(0, 4).toLowerCase())
) {
sendMessage(msg, "Please use my new slash commands!");
} else if (validPly(message)) {
return handleMove(msg, message);
}
});

client.on(Discord.Events.ChannelDelete, function (channel) {
return cleanupFiles(channel.id, true);
return cleanupFiles(channel.id, true);
});

client.on(Discord.Events.Error, (error) => {
console.log(`ERROR: ${error}`);
console.log(`ERROR: ${error}`);
});

client.on(Discord.Events.Warn, (warning) => {
console.log(`WARNING: ${warning}`);
console.log(`WARNING: ${warning}`);
});

client.on(Discord.Events.RateLimit, (info) => {
console.log(`RATE_LIMIT: ${info}`);
console.log(`RATE_LIMIT: ${info}`);
});

process.on("unhandledRejection", (error) => {
console.error("Unhandled promise rejection:", error);
console.error("Unhandled promise rejection:", error);
});

client.login(auth.token);
12 changes: 6 additions & 6 deletions commands/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ const { SlashCommandBuilder } = require("discord.js");
const { handleDelete } = require("../util");

module.exports = {
data: new SlashCommandBuilder()
.setName("delete")
.setDescription("Delete the current game channel."),
async execute(interaction) {
return handleDelete(interaction, interaction.member.id);
},
data: new SlashCommandBuilder()
.setName("delete")
.setDescription("Delete the current game channel."),
async execute(interaction) {
return handleDelete(interaction, interaction.member.id);
},
};
66 changes: 33 additions & 33 deletions commands/end.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
const { SlashCommandBuilder } = require("discord.js");
const {
cleanupFiles,
clearInactiveTimer,
deletePtnFile,
getGameData,
getLink,
isGameOngoing,
renameChannel,
sendMessage,
setDeleteTimer,
cleanupFiles,
clearInactiveTimer,
deletePtnFile,
getGameData,
getLink,
isGameOngoing,
renameChannel,
sendMessage,
setDeleteTimer,
} = require("../util");

module.exports = {
data: new SlashCommandBuilder()
.setName("end")
.setDescription("Cancel the current game."),
async execute(interaction) {
if (!isGameOngoing(interaction)) {
return sendMessage(
interaction,
"There is no ongoing game in this channel.",
true
);
}
data: new SlashCommandBuilder()
.setName("end")
.setDescription("Cancel the current game."),
async execute(interaction) {
if (!isGameOngoing(interaction)) {
return sendMessage(
interaction,
"There is no ongoing game in this channel.",
true
);
}

const gameData = getGameData(interaction);
let message = "The game in this channel has been removed.";
if (gameData.hl) {
message +=
"\nHere's a link to the unfinished game:\n" + getLink(gameData.gameId);
}
cleanupFiles(interaction.channel.id);
deletePtnFile(gameData);
await sendMessage(interaction, message);
clearInactiveTimer(interaction);
setDeleteTimer(interaction);
return renameChannel(interaction, false);
},
const gameData = getGameData(interaction);
let message = "The game in this channel has been removed.";
if (gameData.hl) {
message +=
"\nHere's a link to the unfinished game:\n" + getLink(gameData.gameId);
}
cleanupFiles(interaction.channel.id);
deletePtnFile(gameData);
await sendMessage(interaction, message);
clearInactiveTimer(interaction);
setDeleteTimer(interaction);
return renameChannel(interaction, false);
},
};
12 changes: 6 additions & 6 deletions commands/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ const { SlashCommandBuilder } = require("discord.js");
const { sendHelp } = require("../util");

module.exports = {
data: new SlashCommandBuilder()
.setName("help")
.setDescription("Get an overview of this bot's features."),
async execute(interaction) {
sendHelp(interaction);
},
data: new SlashCommandBuilder()
.setName("help")
.setDescription("Get an overview of this bot's features."),
async execute(interaction) {
sendHelp(interaction);
},
};
Loading

0 comments on commit b4af28b

Please sign in to comment.