lots of changes/new features. added some currency commands and a database system

This commit is contained in:
staticfanfare 2023-12-18 09:56:55 -05:00
parent 420fd837f6
commit 256f318713
16 changed files with 1409 additions and 60 deletions

View File

@ -6,4 +6,11 @@ general purpose discord bot
install dependencies: `npm install`
initialize database: `node dbInit.js`
deploy commands: `node delpoy-commands.js`
start the bot: `node index.js`
## TODO
- Finish currency system

View File

@ -0,0 +1,19 @@
//Boring init stuff
const { SlashCommandBuilder } = require('discord.js');
const helpers = require('../../helpers.js');
module.exports = {
//Creates the slash command
data: new SlashCommandBuilder()
.setName('balance')
.setDescription('Shows your (or a specified user\'s) ospCoin balance.')
.addUserOption(option =>
option
.setName('user')
.setDescription('User\'s balance to display')),
//Executes said slash command
async execute(interaction) {
const target = interaction.options.getUser('user') ?? interaction.user;
return interaction.reply(`${target.tag} has ${helpers.getBalance(target.id)} ospCoin`);
},
};

View File

@ -0,0 +1,24 @@
//Boring init stuff
const { SlashCommandBuilder } = require('discord.js');
const helpers = require('../../helpers.js');
const { Users } = require('../../dbObjects.js');
module.exports = {
//Creates the slash command
data: new SlashCommandBuilder()
.setName('inventory')
.setDescription('Shows your (or a specified user\'s) inventory.')
.addUserOption(option =>
option
.setName('user')
.setDescription('User\'s inventory to display')),
//Executes said slash command
async execute(interaction) {
const target = interaction.options.getUser('user') ?? interaction.user;
const user = await Users.findOne({ where: { user_id: target.id } });
const items = await user.getItems();
if (!items.length) return interaction.reply(`${target.tag} has no items ;v;`);
return interaction.reply(`${target.tag} currently has ${items.map(i => `${i.amount} ${i.item.name}`).join(', ')}`);
},
};

34
commands/fun/gamble.js Normal file
View File

@ -0,0 +1,34 @@
//Boring init stuff
const { SlashCommandBuilder } = require('discord.js');
const { Users } = require('../../dbObjects.js');
const helpers = require('../../helpers.js');
module.exports = {
//Creates the slash command
cooldown: 1800,
data: new SlashCommandBuilder()
.setName('gamble')
.setDescription('bet a certain amount of ospCoin, and either win or lose that amount.')
.addIntegerOption(option =>
option
.setName('amount')
.setDescription('Amount of ospCoin to bet')
.setRequired(true)),
//Executes said slash command
async execute(interaction) {
const amount = interaction.options.getInteger('amount') ?? 0;
const target = interaction.user;
const id = target.id;
if(amount <= helpers.getBalance(id) && amount > 0) {
if(Boolean(Math.round(Math.random()))) {
helpers.addBalance(id, amount);
await interaction.reply(`You won **${amount}** ospCoin!`);
} else {
helpers.addBalance(id, -amount);
await interaction.reply(`You lost **${amount}** ospCoin :<`);
}
} else {
await interaction.reply(`Invalid amount!`);
}
},
};

27
dbInit.js Normal file
View File

@ -0,0 +1,27 @@
const Sequelize = require('sequelize');
const sequelize = new Sequelize('database', 'username', 'password', {
host: 'localhost',
dialect: 'sqlite',
logging: false,
storage: 'database.sqlite',
});
const ItemShop = require('./models/ItemShop.js')(sequelize, Sequelize.DataTypes);
require('./models/Users.js')(sequelize, Sequelize.DataTypes);
require('./models/UserItems.js')(sequelize, Sequelize.DataTypes);
const force = process.argv.includes('--force') || process.argv.includes('-f');
sequelize.sync({ force }).then(async () => {
const shop = [
ItemShop.upsert({ name: 'Canary', desc: 'A little guy! :>', cost: 100 }),
ItemShop.upsert({ name: 'Brick', desc: 'For throwing at friends :3', cost: 3 }),
ItemShop.upsert({ name: 'Book', desc: 'Reading is good for the mind.', cost: 10 }),
];
await Promise.all(shop);
console.log('Database synced');
sequelize.close();
}).catch(console.error);

40
dbObjects.js Normal file
View File

@ -0,0 +1,40 @@
const Sequelize = require('sequelize');
const sequelize = new Sequelize('database', 'username', 'password', {
host: 'localhost',
dialect: 'sqlite',
logging: false,
storage: 'database.sqlite',
});
const Users = require('./models/Users.js')(sequelize, Sequelize.DataTypes);
const ItemShop = require('./models/ItemShop.js')(sequelize, Sequelize.DataTypes);
const UserItems = require('./models/UserItems.js')(sequelize, Sequelize.DataTypes);
UserItems.belongsTo(ItemShop, { foreignKey: 'item_id', as: 'item' });
Reflect.defineProperty(Users.prototype, 'addItem', {
value: async item => {
const userItem = await UserItems.findOne({
where: { user_id: this.user_id, item_id: item.id },
});
if (userItem) {
userItem.amount += 1;
return userItem.save();
}
return UserItems.create({ user_id: this.user_id, item_id: item.id, amount: 1 });
},
});
Reflect.defineProperty(Users.prototype, 'getItems', {
value: () => {
return UserItems.findAll({
where: { user_id: this.user_id },
include: ['item'],
});
},
});
module.exports = { Users, ItemShop, UserItems };

View File

@ -0,0 +1,46 @@
const { Events, Collection } = require('discord.js');
module.exports = {
name: Events.InteractionCreate,
async execute(interaction) {
if (!interaction.isChatInputCommand()) return;
const { cooldowns } = interaction.client;
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
console.error(`ERR: Command "${interaction.commandName}" not found`);
return;
}
if (!cooldowns.has(command.data.name)) {
cooldowns.set(command.data.name, new Collection());
}
const now = Date.now();
const timestamps = cooldowns.get(command.data.name);
const defaultCooldownDuration = 5;
const cooldownAmount = (command.cooldown ?? defaultCooldownDuration) * 1000;
if (timestamps.has(interaction.user.id)) {
const expirationTime = timestamps.get(interaction.user.id) + cooldownAmount;
if (now < expirationTime) {
const expiredTimestamp = Math.round(expirationTime / 1000);
return interaction.reply({ content: `Please wait for <t:${expiredTimestamp}:R> before using \`${command.data.name}\` again.`, ephemeral: true });
}
}
timestamps.set(interaction.user.id, now);
setTimeout(() => timestamps.delete(interaction.user.id), cooldownAmount);
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: `ERROR!`, ephemeral: true});
} else {
await interaction.reply({ content: `ERROR!`, ephemeral: true});
}
}
},
}

12
events/messageCreate.js Normal file
View File

@ -0,0 +1,12 @@
const { Events } = require('discord.js');
const helpers = require('../helpers.js');
module.exports = {
name: Events.MessageCreate,
async execute(message) {
if (message.author.bot) return;
if (Math.floor(Math.random() * 11) == 10) {
helpers.addBalance(message.author.id, 1);
}
},
}

14
events/ready.js Normal file
View File

@ -0,0 +1,14 @@
const { Events } = require('discord.js');
const { Users } = require('../dbObjects.js');
const helpers = require('../helpers.js');
module.exports = {
name: Events.ClientReady,
once: true,
async execute(client) {
const storedBalances = await Users.findAll();
storedBalances.forEach(b => helpers.currency.set(b.user_id, b));
console.log(`Ready! Logged in as ${client.user.tag}`);
},
};

25
helpers.js Normal file
View File

@ -0,0 +1,25 @@
const { Collection } = require('discord.js');
const { Users } = require('./dbObjects.js')
const currency = new Collection();
//Helper methods for currency stuff
module.exports = {
currency: currency,
addBalance: async function (id, amount) {
const user = currency.get(id);
if (user) {
user.balance += Number(amount);
return user.save();
}
const newUser = await Users.create({ user_id: id, balance: amount });
currency.set(id, newUser);
return newUser;
},
getBalance: function (id) {
const user = currency.get(id);
return user ? user.balance : 0;
},
}

View File

@ -1,13 +1,19 @@
//Boring boring initialization stuff
const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, Events, GatewayIntentBits } = require('discord.js');
const { Op } = require('sequelize');
const { Users, ItemShop } = require('./dbObjects.js');
const helpers = require('./helpers.js');
const { Client, codeBlock, Collection, GatewayIntentBits } = require('discord.js');
const { token } = require('./config.json');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages] });
//Handle commands
client.commands = new Collection();
client.cooldowns = new Collection();
//Command Loading
const foldersPath = path.join(__dirname, 'commands');
@ -17,7 +23,7 @@ for(const folder of commandFolders) {
const commandPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandPath, file);
const filePath = path.join(commandPath, file);
const command = require(filePath);
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
@ -27,34 +33,18 @@ for(const folder of commandFolders) {
}
}
//Command Interaction Processing
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) return;
//Event Loading
const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
console.error(`ERR: Command "${interaction.commandName}" not found`);
return;
for (const file of eventFiles) {
const filePath = path.join(eventsPath, file);
const event = require(filePath);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: `ERROR!`, ephermal: true});
} else {
await interaction.reply({ content: `ERROR!`, ephermal: true});
}
}
});
//Log the bot in with the token supplied in config.json
client.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});
}
client.login(token);

17
models/ItemShop.js Normal file
View File

@ -0,0 +1,17 @@
module.exports = (sequelize, DataTypes) => {
return sequelize.define('item_shop', {
name: {
type: DataTypes.STRING,
unique: true,
},
desc: {
type: DataTypes.STRING,
},
cost: {
type: DataTypes.INTEGER,
allowNull: false,
},
}, {
timestamps: false,
});
};

14
models/UserItems.js Normal file
View File

@ -0,0 +1,14 @@
module.exports = (sequelize, DataTypes) => {
return sequelize.define('user_item', {
user_id: DataTypes.STRING,
item_id: DataTypes.INTEGER,
amount: {
type: DataTypes.INTEGER,
allowNull: false,
'default': 0,
},
}, {
timestamps: false,
});
};

15
models/Users.js Normal file
View File

@ -0,0 +1,15 @@
module.exports = (sequelize, DataTypes) => {
return sequelize.define('users', {
user_id: {
type: DataTypes.STRING,
primaryKey: true,
},
balance: {
type: DataTypes.INTEGER,
defaultValue: 0,
allowNull: false,
},
}, {
timestamps: false,
});
};

1119
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{
"name": "skopa",
"version": "0.1.1",
"version": "0.2.0",
"description": "a general-purpose discord bot",
"main": "index.js",
"scripts": {
@ -13,7 +13,9 @@
"author": "staticfanfare",
"license": "AGPL-3.0-or-later",
"dependencies": {
"discord.js": "^14.14.1"
"discord.js": "^14.14.1",
"sequelize": "^6.35.2",
"sqlite3": "^5.1.6"
},
"devDependencies": {
"eslint": "^8.56.0"