Initial commit

This commit is contained in:
Alexandertp 2024-11-17 20:59:08 +01:00
commit c312e971f5
6 changed files with 1917 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
node_modules
.env
employeescoreboard.db/
db/employeescoreboard.db

22
db/DBFirstRun.js Normal file
View File

@ -0,0 +1,22 @@
import sqlite3 from 'sqlite3';
const db = new sqlite3.Database("./db/employeescoreboard.db", sqlite3.OPEN_READWRITE,(err) => {
if (err) return console.error(err.message);
});
let query;
query = `CREATE TABLE Employees
(
id INTEGER PRIMARY KEY,
name VARCHAR NOT NULL,
rank VARCHAR DEFAULT \'Intern\',
coppergear INTEGER DEFAULT 0,
silvergear INTEGER DEFAULT 0,
goldgear INTEGER DEFAULT 0,
purplegear INTEGER DEFAULT 0,
copperswog INTEGER DEFAULT 0,
silverswog INTEGER DEFAULT 0,
goldswog INTEGER DEFAULT 0,
purpleswog INTEGER DEFAULT 0
)`;
db.run(query);

1654
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

19
package.json Normal file
View File

@ -0,0 +1,19 @@
{
"name": "swogbot",
"version": "1.0.0",
"description": "",
"main": "src/index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"discord.js": "^14.16.3",
"dotenv": "^16.4.5",
"sqlite3": "^5.1.7",
"swogbot": "file:"
}
}

118
src/index.js Normal file
View File

@ -0,0 +1,118 @@
import dotenv from 'dotenv';
import {Client, IntentsBitField} from 'discord.js';
import sqlite3 from 'sqlite3';
const db = new sqlite3.Database("./db/employeescoreboard.db", sqlite3.OPEN_READWRITE,(err) => {
if (err) return console.error(err.message);
});
//Id of the channel where the gear scoreboard will be posted to, to post in the testing channel it is 1306669146804715693
dotenv.config();
const client = new Client({
intents: [
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMembers,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.MessageContent,
]
});
client.on('ready', (c) => {
console.log('The Swog is prepared.');
})
//Chat commands are handled here
client.on('interactionCreate', (interaction) => {
if (!interaction.isChatInputCommand()) return;
if (interaction.commandName === 'croak') {
interaction.reply('Croak!');
}
if (interaction.commandName === 'gear') {
const addRemove = interaction.options.get('addremove').value;
const gearType = interaction.options.get('geartype').value;
const employee = interaction.options.get('employee').value;
if ( addRemove == 'add') {
AddGear(gearType, employee)
};
}
if (interaction.commandName === 'employee') {
const hireFirePromote = interaction.options.get('hire-fire-promote').value;
const employeeName = interaction.options.get('employee-name').value;
if (hireFirePromote == 'hire') {
CreateEmployee(employeeName);
interaction.reply(`Hired ${employeeName}!`);
}
if (hireFirePromote == 'fire') {
DeleteEmployee(employeeName);
}
}
if (interaction.commandName === 'promote') {
const employeeName = interaction.options.get('employee-name').value;
const rank = interaction.options.get('rank').value;
PromoteEmployee(employeeName, rank);
interaction.reply(`Changed ${employeeName}\'s rank to ${rank}`);
}
if (interaction.commandName === 'scoreboard') {
const updateOrDisplay = interaction.options.get('update-display').value;
if (updateOrDisplay == 'display') {
db.each("SELECT name, rank FROM Employees", (err, character) => {
if (err != null) {
console.log(err.message);
}
console.log("Character Name: " + character.name + " rank: " + character.rank);
});
const gearchannel = client.channels.cache.get('1306669146804715693');
const scoreBoardMessage = BuildScoreboard();
console.log("Scoreboard goes here: " + scoreBoardMessage);
//gearchannel.send(scoreBoardMessage);
interaction.reply("Displaying Scoreboard");
};
};
});
function CreateEmployee(Name) {
db.run(`INSERT INTO Employees (name) VALUES (\'${Name}\')`);
};
function DeleteEmployee(Name) {
db.run(`DELETE FROM Employees WHERE name = \'${Name}\'`);
};
function PromoteEmployee(Name, rank) {
db.run(`UPDATE Employees SET rank = \'${rank}\' WHERE name = \'${Name}\'`)
}
function AddGear(Type,Employee) {
};
function BuildScoreboard() {
let messageContent = ""
db.each('SELECT name, rank, coppergear, silvergear, goldgear, purplegear FROM Employees', (err, character) => {
if (err != null) {
console.log(err.message);
}
let coppergearstring = "";
for (let i = 0; i < character.coppergear; i++) {
coppergearstring += ":coppergear:"
}
let silvergearstring = "";
messageContent += `**${character.name}** \n ## ${coppergearstring} \n`;
});
return messageContent;
}
client.login(process.env.DISCORD_TOKEN);

100
src/register-commands.js Normal file
View File

@ -0,0 +1,100 @@
import {REST, Routes, ApplicationCommandOptionType} from 'discord.js';
import dotenv from 'dotenv';
dotenv.config();
// Note, command names must always be lower case
const commands = [
{
name: 'croak',
description: 'Makes Sir Hornsworth Croak!',
},
{
name: 'gear',
description: 'Add or remove a gear to an employee',
options: [
{
name: 'addremove',
description: 'Do you want to add or remove a Gear?',
type: ApplicationCommandOptionType.String,
required: true,
},
{
name: 'geartype',
description: 'What sort of gear do you want to change?',
type: ApplicationCommandOptionType.String,
required: true,
},
{
name: 'employee',
description: 'From who?',
type: ApplicationCommandOptionType.String,
required: true,
}
],
},
{
name: 'employee',
description: 'Hire or Fire an employee',
options: [
{
name: 'hire-fire-promote',
description: 'Hiring or Firing?',
type: ApplicationCommandOptionType.String,
required: true,
},
{
name: 'employee-name',
description: 'What is the character\'s name?',
type: ApplicationCommandOptionType.String,
required: true,
},
],
},
{
name: 'promote',
description: 'Changes an employee\'s rank to the specified rank',
options: [
{
name: 'employee-name',
description: 'Who\'s rank are you changing?',
type: ApplicationCommandOptionType.String,
required: true,
},
{
name: 'rank',
description: 'which rank are you changing them to?',
type:ApplicationCommandOptionType.String,
required: true,
}
]
},
{
name: 'scoreboard',
description: 'Related to the scoreboard',
options: [
{
name: 'update-display',
description: 'Displays the scoreboard or updates an already displayed one',
type: ApplicationCommandOptionType.String,
required: true,
}
]
},
];
const rest = new REST({version: 10}).setToken(process.env.DISCORD_TOKEN);
(async ()=> {
try {
console.log('Registering slash commands...')
await rest.put(
Routes.applicationGuildCommands(process.env.CLIENT_ID, process.env.GUILD_ID),
{body: commands}
)
console.log('Slash Commands Registered.')
} catch (error) {
console.log(`There was an error: ${error}`);
}
})();