database

I have a file called database.js const client = requre('Client.js'); const Sequelize = require('sequelize');
const sequelize = new Sequelize('database', 'user', 'password', {
host: 'localhost',
dialect: 'sqlite',
logging: false,
storage: 'database.sqlite',
});

const blocked = sequelize.define('blocked', {
user: {
type: Sequelize.STRING,
unique: true,
},
});
const sequelize = new Sequelize('database', 'user', 'password', {
host: 'localhost',
dialect: 'sqlite',
logging: false,
storage: 'database.sqlite',
});

const blocked = sequelize.define('blocked', {
user: {
type: Sequelize.STRING,
unique: true,
},
});
and here is my command file:
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const Client = require('../Constants/Client.js');
const Sequelize = require('sequelize');



module.exports = {
data: new SlashCommandBuilder()
.setName('test')
.setDescription('test cmd')
.addUserOption(option =>
option.setName('user')
.setDescription('user to be banned from using bot')
.setRequired(true)),

async execute(interaction) {
const person = interaction.options.getUser('user');
try {
const user = await blocked.create({
user: person.id,
});

return interaction.reply(`added ${person.tag}`);
}
catch (error) {
if (error.name === 'SequelizeUniqueConstraintError') {
return interaction.reply('That tag already exists.');
}

return interaction.reply('Something went wrong with adding a tag.');
}
}
};
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const Client = require('../Constants/Client.js');
const Sequelize = require('sequelize');



module.exports = {
data: new SlashCommandBuilder()
.setName('test')
.setDescription('test cmd')
.addUserOption(option =>
option.setName('user')
.setDescription('user to be banned from using bot')
.setRequired(true)),

async execute(interaction) {
const person = interaction.options.getUser('user');
try {
const user = await blocked.create({
user: person.id,
});

return interaction.reply(`added ${person.tag}`);
}
catch (error) {
if (error.name === 'SequelizeUniqueConstraintError') {
return interaction.reply('That tag already exists.');
}

return interaction.reply('Something went wrong with adding a tag.');
}
}
};
23 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
JollyJohn
JollyJohnOP3y ago
ran the command and it gave the error that something went wrong ill do dat rn
'ReferenceError: blocked is not defined
: at Object.execute (/home/node/commands/test.js:19:17)
6 at Client.<anonymous> (/home/node/index.js:65:19)
( at Client.emit (node:events:525:35)
y at InteractionCreateAction.handle (/home/node/node_modules/discord.js/src/client/actions/InteractionCreate.js:97:12)
� at module.exports [as INTERACTION_CREATE] (/home/node/node_modules/discord.js/src/client/websocket/handlers/INTERACTION_CREATE.js:4:36)
z at WebSocketManager.handlePacket (/home/node/node_modules/discord.js/src/client/websocket/WebSocketManager.js:352:31)
r at WebSocketShard.onPacket (/home/node/node_modules/discord.js/src/client/websocket/WebSocketShard.js:489:22)
s at WebSocketShard.onMessage (/home/node/node_modules/discord.js/src/client/websocket/WebSocketShard.js:328:10)
L at callListener (/home/node/node_modules/ws/lib/event-target.js:290:14)
R at WebSocket.onMessage (/home/node/node_modules/ws/lib/event-target.js:209:9)
'ReferenceError: blocked is not defined
: at Object.execute (/home/node/commands/test.js:19:17)
6 at Client.<anonymous> (/home/node/index.js:65:19)
( at Client.emit (node:events:525:35)
y at InteractionCreateAction.handle (/home/node/node_modules/discord.js/src/client/actions/InteractionCreate.js:97:12)
� at module.exports [as INTERACTION_CREATE] (/home/node/node_modules/discord.js/src/client/websocket/handlers/INTERACTION_CREATE.js:4:36)
z at WebSocketManager.handlePacket (/home/node/node_modules/discord.js/src/client/websocket/WebSocketManager.js:352:31)
r at WebSocketShard.onPacket (/home/node/node_modules/discord.js/src/client/websocket/WebSocketShard.js:489:22)
s at WebSocketShard.onMessage (/home/node/node_modules/discord.js/src/client/websocket/WebSocketShard.js:328:10)
L at callListener (/home/node/node_modules/ws/lib/event-target.js:290:14)
R at WebSocket.onMessage (/home/node/node_modules/ws/lib/event-target.js:209:9)
CanineData
CanineData3y ago
you don't have blocked defined in the command file
JollyJohn
JollyJohnOP3y ago
so would i have to require it from my database file?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
JollyJohn
JollyJohnOP3y ago
yes, i am
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
JollyJohn
JollyJohnOP3y ago
Ok do i need to export it @lonedefense
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
JollyJohn
JollyJohnOP3y ago
https://discloud.app/logs/1df64729a80dfd375e5790fdcc258109d3fe i keep getting this ik what it means but idk what file its comign from
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
JollyJohn
JollyJohnOP3y ago
oh ty
/home/node/database.js:1
/const client = require('/Constants/Client.js);
/ ^^^^^^^^^^^^^^^^^^^^^^^

)SyntaxError: Invalid or unexpected token
/ at Object.compileFunction (node:vm:360:18)
; at wrapSafe (node:internal/modules/cjs/loader:1088:15)
B at Module._compile (node:internal/modules/cjs/loader:1123:27)
I at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
> at Module.load (node:internal/modules/cjs/loader:1037:32)
> at Module._load (node:internal/modules/cjs/loader:878:12)
A at Module.require (node:internal/modules/cjs/loader:1061:19)
: at require (node:internal/modules/cjs/helpers:103:18)
6 at Object.<anonymous> (/home/node/index.js:11:17)
B at Module._compile (node:internal/modules/cjs/loader:1159:14)

Node.js v18.12.1
/home/node/database.js:1
/const client = require('/Constants/Client.js);
/ ^^^^^^^^^^^^^^^^^^^^^^^

)SyntaxError: Invalid or unexpected token
/ at Object.compileFunction (node:vm:360:18)
; at wrapSafe (node:internal/modules/cjs/loader:1088:15)
B at Module._compile (node:internal/modules/cjs/loader:1123:27)
I at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
> at Module.load (node:internal/modules/cjs/loader:1037:32)
> at Module._load (node:internal/modules/cjs/loader:878:12)
A at Module.require (node:internal/modules/cjs/loader:1061:19)
: at require (node:internal/modules/cjs/helpers:103:18)
6 at Object.<anonymous> (/home/node/index.js:11:17)
B at Module._compile (node:internal/modules/cjs/loader:1159:14)

Node.js v18.12.1
now im getting this nvm
throw err;
^

1Error: Cannot find module '/Constants/Client.js'
Require stack:
- /home/node/database.js
- /home/node/index.js
I at Module._resolveFilename (node:internal/modules/cjs/loader:995:15)
> at Module._load (node:internal/modules/cjs/loader:841:27)
A at Module.require (node:internal/modules/cjs/loader:1061:19)
: at require (node:internal/modules/cjs/helpers:103:18)
8 at Object.<anonymous> (/home/node/database.js:1:16)
B at Module._compile (node:internal/modules/cjs/loader:1159:14)
I at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
> at Module.load (node:internal/modules/cjs/loader:1037:32)
> at Module._load (node:internal/modules/cjs/loader:878:12)
C at Module.require (node:internal/modules/cjs/loader:1061:19) {
code: 'MODULE_NOT_FOUND',
D requireStack: [ '/home/node/database.js', '/home/node/index.js' ]
}
throw err;
^

1Error: Cannot find module '/Constants/Client.js'
Require stack:
- /home/node/database.js
- /home/node/index.js
I at Module._resolveFilename (node:internal/modules/cjs/loader:995:15)
> at Module._load (node:internal/modules/cjs/loader:841:27)
A at Module.require (node:internal/modules/cjs/loader:1061:19)
: at require (node:internal/modules/cjs/helpers:103:18)
8 at Object.<anonymous> (/home/node/database.js:1:16)
B at Module._compile (node:internal/modules/cjs/loader:1159:14)
I at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
> at Module.load (node:internal/modules/cjs/loader:1037:32)
> at Module._load (node:internal/modules/cjs/loader:878:12)
C at Module.require (node:internal/modules/cjs/loader:1061:19) {
code: 'MODULE_NOT_FOUND',
D requireStack: [ '/home/node/database.js', '/home/node/index.js' ]
}
Error
d at Database.<anonymous> (/home/node/node_modules/sequelize/lib/dialects/sqlite/query.js:179:27)
M at /home/node/node_modules/sequelize/lib/dialects/sqlite/query.js:177:50
! at new Promise (<anonymous>)
Y at Query.run (/home/node/node_modules/sequelize/lib/dialects/sqlite/query.js:177:12)
A at /home/node/node_modules/sequelize/lib/sequelize.js:314:28
} at async SQLiteQueryInterface.insert (/home/node/node_modules/sequelize/lib/dialects/abstract/query-interface.js:308:21)
Q at async model.save (/home/node/node_modules/sequelize/lib/model.js:2432:35)
U at async blocked.create (/home/node/node_modules/sequelize/lib/model.js:1344:12)
@ at async Object.execute (/home/node/commands/test.js:20:20)
= at async Client.<anonymous> (/home/node/index.js:67:5) {
" name: 'SequelizeDatabaseError',
; parent: [Error: SQLITE_ERROR: no such table: blockeds] {
errno: 1,
code: 'SQLITE_ERROR',
` sql: 'INSERT INTO `blockeds` (`id`,`user`,`createdAt`,`updatedAt`) VALUES (NULL,$1,$2,$3);'
},
= original: [Error: SQLITE_ERROR: no such table: blockeds] {
errno: 1,
code: 'SQLITE_ERROR',
` sql: 'INSERT INTO `blockeds` (`id`,`user`,`createdAt`,`updatedAt`) VALUES (NULL,$1,$2,$3);'
},
_ sql: 'INSERT INTO `blockeds` (`id`,`user`,`createdAt`,`updatedAt`) VALUES (NULL,$1,$2,$3);',
parameters: {}
}
Error
d at Database.<anonymous> (/home/node/node_modules/sequelize/lib/dialects/sqlite/query.js:179:27)
M at /home/node/node_modules/sequelize/lib/dialects/sqlite/query.js:177:50
! at new Promise (<anonymous>)
Y at Query.run (/home/node/node_modules/sequelize/lib/dialects/sqlite/query.js:177:12)
A at /home/node/node_modules/sequelize/lib/sequelize.js:314:28
} at async SQLiteQueryInterface.insert (/home/node/node_modules/sequelize/lib/dialects/abstract/query-interface.js:308:21)
Q at async model.save (/home/node/node_modules/sequelize/lib/model.js:2432:35)
U at async blocked.create (/home/node/node_modules/sequelize/lib/model.js:1344:12)
@ at async Object.execute (/home/node/commands/test.js:20:20)
= at async Client.<anonymous> (/home/node/index.js:67:5) {
" name: 'SequelizeDatabaseError',
; parent: [Error: SQLITE_ERROR: no such table: blockeds] {
errno: 1,
code: 'SQLITE_ERROR',
` sql: 'INSERT INTO `blockeds` (`id`,`user`,`createdAt`,`updatedAt`) VALUES (NULL,$1,$2,$3);'
},
= original: [Error: SQLITE_ERROR: no such table: blockeds] {
errno: 1,
code: 'SQLITE_ERROR',
` sql: 'INSERT INTO `blockeds` (`id`,`user`,`createdAt`,`updatedAt`) VALUES (NULL,$1,$2,$3);'
},
_ sql: 'INSERT INTO `blockeds` (`id`,`user`,`createdAt`,`updatedAt`) VALUES (NULL,$1,$2,$3);',
parameters: {}
}
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
JollyJohn
JollyJohnOP3y ago
ye im so confused abt this err
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
JollyJohn
JollyJohnOP3y ago
everything is called blocked so idk where its getting blockeds from
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
JollyJohn
JollyJohnOP3y ago
const blocked = sequelize.define('blocked', { user: { type: Sequelize.STRING, unique: true, }, }); module.exports = blocked; and then i have this in my index.js client.once(Events.clientReady, () => { blocked.sync(); console.log('synced blocked users') }); anyone please
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
JollyJohn
JollyJohnOP3y ago
ok, but wdym didnt define the schema anywhere
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
JollyJohn
JollyJohnOP3y ago
how do i override the collection name? isnt that right here const blocked = sequelize.define('no', { isnt no what the table will be called? const blocked = sequelize.define('no', { TableName: 'Blocked'
user: { type: Sequelize.STRING, unique: true, }, }); module.exports = blocked; like that? i still am stuck what if i dont need the 2nd parameter sequelize.define(modelName, attributes, options) i see that const Restricted = sequelize.define('Restricted', 'Restricted Users' { user: { type: Sequelize.STRING, unique: true, }, reason: Sequelize.TEXT, staff: Sequelize.STRING }); module.exports = Restricted; would it be like that or something then
const client = require('./Constants/Client.js');
const Sequelize = require('sequelize');

const sequelize = new Sequelize('database', 'user', 'password', {
host: 'localhost',
dialect: 'sqlite',
logging: false,
storage: 'database.sqlite',
});

const Restricted = sequelize.define( ModalName, 'Restricted', schema, 'Restricted Users', {
user: {
type: Sequelize.STRING,
unique: true,
},
reason: Sequelize.TEXT,
staff: Sequelize.STRING
});
module.exports = Restricted;
const client = require('./Constants/Client.js');
const Sequelize = require('sequelize');

const sequelize = new Sequelize('database', 'user', 'password', {
host: 'localhost',
dialect: 'sqlite',
logging: false,
storage: 'database.sqlite',
});

const Restricted = sequelize.define( ModalName, 'Restricted', schema, 'Restricted Users', {
user: {
type: Sequelize.STRING,
unique: true,
},
reason: Sequelize.TEXT,
staff: Sequelize.STRING
});
module.exports = Restricted;
like that then? const Restricted = sequelize.define( 'Restricted',{ user: { type: Sequelize.STRING, unique: true, }, reason: Sequelize.TEXT, staff: Sequelize.STRING, define: { freezeTableName: true } }); module.exports = Restricted; would that work? sequelize.define('User', { user: { type: Sequelize.STRING, unique: true, }, reason: Sequelize.TEXT, staff: Sequelize.STRING }, { tableName: 'Restricted Users' }); sorry it took me a second to understnad what u meant
Error
d at Database.<anonymous> (/home/node/node_modules/sequelize/lib/dialects/sqlite/query.js:179:27)
M at /home/node/node_modules/sequelize/lib/dialects/sqlite/query.js:177:50
! at new Promise (<anonymous>)
Y at Query.run (/home/node/node_modules/sequelize/lib/dialects/sqlite/query.js:177:12)
A at /home/node/node_modules/sequelize/lib/sequelize.js:314:28
} at async SQLiteQueryInterface.insert (/home/node/node_modules/sequelize/lib/dialects/abstract/query-interface.js:308:21)
Q at async model.save (/home/node/node_modules/sequelize/lib/model.js:2432:35)
X at async Restricted.create (/home/node/node_modules/sequelize/lib/model.js:1344:12)
@ at async Object.execute (/home/node/commands/test.js:25:21)
= at async Client.<anonymous> (/home/node/index.js:67:5) {
" name: 'SequelizeDatabaseError',
C parent: [Error: SQLITE_ERROR: no such table: Restricted Users] {
errno: 1,
code: 'SQLITE_ERROR',
sql: 'INSERT INTO `Restricted Users` (`id`,`user`,`reason`,`staff`,`createdAt`,`updatedAt`) VALUES (NULL,$1,$2,$3,$4,$5);'
},
E original: [Error: SQLITE_ERROR: no such table: Restricted Users] {
errno: 1,
code: 'SQLITE_ERROR',
sql: 'INSERT INTO `Restricted Users` (`id`,`user`,`reason`,`staff`,`createdAt`,`updatedAt`) VALUES (NULL,$1,$2,$3,$4,$5);'
},
~ sql: 'INSERT INTO `Restricted Users` (`id`,`user`,`reason`,`staff`,`createdAt`,`updatedAt`) VALUES (NULL,$1,$2,$3,$4,$5);',
parameters: {}
}
Error
d at Database.<anonymous> (/home/node/node_modules/sequelize/lib/dialects/sqlite/query.js:179:27)
M at /home/node/node_modules/sequelize/lib/dialects/sqlite/query.js:177:50
! at new Promise (<anonymous>)
Y at Query.run (/home/node/node_modules/sequelize/lib/dialects/sqlite/query.js:177:12)
A at /home/node/node_modules/sequelize/lib/sequelize.js:314:28
} at async SQLiteQueryInterface.insert (/home/node/node_modules/sequelize/lib/dialects/abstract/query-interface.js:308:21)
Q at async model.save (/home/node/node_modules/sequelize/lib/model.js:2432:35)
X at async Restricted.create (/home/node/node_modules/sequelize/lib/model.js:1344:12)
@ at async Object.execute (/home/node/commands/test.js:25:21)
= at async Client.<anonymous> (/home/node/index.js:67:5) {
" name: 'SequelizeDatabaseError',
C parent: [Error: SQLITE_ERROR: no such table: Restricted Users] {
errno: 1,
code: 'SQLITE_ERROR',
sql: 'INSERT INTO `Restricted Users` (`id`,`user`,`reason`,`staff`,`createdAt`,`updatedAt`) VALUES (NULL,$1,$2,$3,$4,$5);'
},
E original: [Error: SQLITE_ERROR: no such table: Restricted Users] {
errno: 1,
code: 'SQLITE_ERROR',
sql: 'INSERT INTO `Restricted Users` (`id`,`user`,`reason`,`staff`,`createdAt`,`updatedAt`) VALUES (NULL,$1,$2,$3,$4,$5);'
},
~ sql: 'INSERT INTO `Restricted Users` (`id`,`user`,`reason`,`staff`,`createdAt`,`updatedAt`) VALUES (NULL,$1,$2,$3,$4,$5);',
parameters: {}
}
my .sync code is in index.js client.once(Events.clientReady, () => { Restricted.sync(); console.log('synced blocked users') }); No, only on when the client is ready im guessing where would i put .sync then?
const Restricted = sequelize.define( 'Restricted',{
user: {
type: Sequelize.STRING,
unique: true,
},
reason: Sequelize.STRING,
staff: Sequelize.STRING
}, {
tableName: 'Restricted Users'
});
await Restricted.sync()
module.exports = Restricted;
const Restricted = sequelize.define( 'Restricted',{
user: {
type: Sequelize.STRING,
unique: true,
},
reason: Sequelize.STRING,
staff: Sequelize.STRING
}, {
tableName: 'Restricted Users'
});
await Restricted.sync()
module.exports = Restricted;
? someone please help
MrMythical
MrMythical3y ago
This isn't even discord.js related Thonk #useful-servers
Want results from more Discord servers?
Add your server