Compare commits

...

4 Commits

Author SHA1 Message Date
baz 1f504ae0c4 Create register command 2023-12-18 21:57:06 +00:00
baz 590ca18f18 Create associations 2023-12-18 21:56:59 +00:00
baz 2e4e68a82b Fix requires 2023-12-18 21:55:20 +00:00
baz 7c27191202 Refactor models 2023-12-18 21:55:04 +00:00
7 changed files with 55 additions and 40 deletions

View File

@ -0,0 +1,25 @@
const { SlashCommandBuilder } = require('discord.js');
const { Users } = require ('../../dbObjects.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('register')
.setDescription('Manually registers the user into the user database.'),
async execute(interaction) {
// interaction.user is the object representing the user who ran the command
// interaction.member is the GuildMember object, which represents the user in the specific guild
const user = await Users.findOne({ where: { discord_id: interaction.user.id } })
.catch((err) => {
console.log(err);
});
if (user) return interaction.reply(`User "${interaction.user.username}" is already registered`);
await Users.create({ discord_id: interaction.user.id, username: interaction.user.username })
.then(await interaction.reply(`${interaction.user.username} was manually registered.`))
.catch((err) => {
console.log(err);
});
},
};

View File

@ -9,7 +9,7 @@ const sequelize = new Sequelize('database', 'username', 'password', {
const db = require('./models/games.js')(sequelize, Sequelize.DataTypes);
require('./models/users.js')(sequelize, Sequelize.DataTypes);
require('models/beatenGames.js')(sequelize, Sequelize.DataTypes);
require('./models/beatenGames.js')(sequelize, Sequelize.DataTypes);
const force = process.argv.includes('--force') || process.argv.includes('-f');

22
dbObjects.js Normal file
View File

@ -0,0 +1,22 @@
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 Games = require('./models/games.js')(sequelize, Sequelize.DataTypes);
const BeatenGames = require('./models/beatenGames.js')(sequelize, Sequelize.DataTypes);
// Create userId foreignKey
Users.hasMany(BeatenGames);
BeatenGames.belongsTo(Users);
// Create gameId foreignKey
Games.hasMany(BeatenGames);
BeatenGames.belongsTo(Games);
module.exports = { Users, Games, BeatenGames };

View File

@ -50,30 +50,9 @@ for (const file of eventFiles) {
client.login(process.env.token);
const { Sequelize } = require('sequelize');
const sequelize = new Sequelize('database', 'user', 'password', {
host: 'localhost',
dialect: 'sqlite',
logging: false,
storage: 'database.sqlite',
});
const Tags = sequelize.define('tags', {
name: {
type: Sequelize.STRING,
unique: true,
},
description: Sequelize.TEXT,
username: Sequelize.STRING,
usage_count: {
type: Sequelize.INTEGER,
defaultValue: 0,
allowNull: false,
},
});
client.once(Events.ClientReady, () => {
Tags.sync({ force: true });
console.log(`Logged in as ${client.user.tag}!`);
});
});
require('sequelize');
require('./dbObjects.js');

View File

@ -1,16 +1,7 @@
module.exports = (sequelize, DataTypes) => {
return sequelize.define('beatenGames', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
user_id: DataTypes.INTEGER,
game_id: DataTypes.STRING,
date_created: DataTypes.DATE,
date_last_modified: DataTypes.DATE,
}, {
timestamps: false,
timestamps: true,
});
};

View File

@ -1,8 +1,7 @@
module.exports = (sequelize, DataTypes) => {
return sequelize.define('games', {
id: {
igdb_id: {
type: DataTypes.INTEGER,
primaryKey: true,
},
name: {
type: DataTypes.STRING,

View File

@ -1,8 +1,7 @@
module.exports = (sequelize, DataTypes) => {
return sequelize.define('users', {
user_id: {
discord_id: {
type: DataTypes.STRING,
primaryKey: true,
},
username: {
type: DataTypes.STRING,