Send message to channel in a function - node.js

I have an event handler for the ready event.
ready.js:
const { Events } = require('discord.js');
const db = require("../config/database");
const itemdb = require("../config/itemdb");
const items = require("../models/items");
const AHItems = require('../models/ahitems.js');
const RSS = require('../models/regionserversettings.js');
module.exports = {
name: Events.ClientReady,
once: true,
execute(client) {
console.log(`Ready! Logged in as ${client.user.tag}`);
db.authenticate()
.then(() => {
console.log('Logged in to DB!');
AHItems.init(db);
AHItems.sync();
RSS.init(db);
RSS.sync();
})
.catch(err => console.log(err));
itemdb.authenticate()
.then(() => {
console.log('Logged in to Item DB!');
items.init(itemdb);
items.sync();
})
.catch(err => console.log(err));
},
};
From inside the execute block I can use client.channels.cache.get('xxxxxx').send('Hello');
I want to use the send method in another File:
const AHItems = require("../models/ahitems");
const RSS = require("../models/regionserversettings");
const getprice = require("../api/getcurrentPrice");
const client = require("../events/ready");
const pricealarm = async function()
{
let ahitems = await AHItems.findAll({attributes: ['guildID', 'itemID']});
for (let i = 0; i < ahitems.length; i++) {
const guild = ahitems[i].dataValues.guildID;
const RSSData = await RSS.findOne({where: {guildID: guild}});
const item = ahitems[i].dataValues.itemID;
const access_token = RSSData.AccessToken;
const server = RSSData.serverID;
const price = await getprice(access_token, item, server);
const channel = client.channels.cache.get('x').send('test');
console.log(channel);
}
}
module.exports = pricealarm;
But if I try to do this, it tells me 'Unresolved function or method send()'
I think I am requiring the wrong file, but am unsure, which one I have to require

The issue with your code is that you are trying to use the send() method from an object client that has not been properly instantiated in the file where you want to use it. In your ready.js file, you correctly initialize the client object and can use its send() method inside the execute block.
However, in the other file where you want to use the send() method, you import the ready.js file, but you are only importing the module, not the instantiated client object. Therefore, the send() method is unresolved and cannot be called.
To fix this issue, you need to modify the ready.js file to export the client object in addition to the module.exports statement. For example, you can add the following line at the end of the execute block:
module.exports.client = client;
Then, in your other file, you can import the client object by requiring the ready.js file and accessing the client property of the exported module. For example:
const ready = require("../events/ready");
const client = ready.client;
// Now you can use client.channels.cache.get('xxxxxx').send('Hello');
With these modifications, you should be able to properly use the send() method from the client object in both files.

Related

How can i get a client side cookie with next.js 13 before rendering from middleware

I am trying to get a cookie from the middleware of Nextjs 13 and I can't find up to date information on this.
My question: is there a way to see the cookie before rendering the html.
window is fined or something?
page.tsx
async function getData() {
const nextCookies = cookies(); // Get cookies object
try {
let cityToken = nextCookies.get('regionCode'); // Find cookie city
return cityToken;
} catch (e) {
console.log(e);
}
}
export default async function Page() {
const nextCookies = cookies(); // Get cookies object
const token = nextCookies.get('new'); // Find cookie for is new user
// const cityToken = await getData();
const cityToken = cookies().get('regionCode')?.value;
console.log(cityToken);
}
middleware.tsx
const myFunction = async () => {
// https://geolocation.onetrust.com/cookieconsentpub/v1/geo/location
// run asynchronous tasks here - ip location
const response = await fetch('https://www.cloudflare.com/cdn-cgi/trace');
const text = await response.text();
const arr = text.split('\n');
const ip = arr.filter((v) => v.includes('ip='))[0].split('=')[1];
// https://stackoverflow.com/questions/65752416/how-can-i-most-easily-parse-this-http-response-into-json-to-then-access-response
// https://stackoverflow.com/questions/64591296/access-to-fetch-at-https-www-cloudflare-com-cdn-cgi-trace-from-origin-http
const ipCity = await fetch(`https://ipapi.co/${ip}/json/`);
const textJson = await ipCity.json();
};
const data = await myFunction();
res.cookies.set('regionCode', 'textJson.region');

Web3 smart contract instance is not able to listen Transfer events

I have a NFT deployed on the mainnet, and I want to listen any mint event by watching the Transfer event via web3js. But unfortunately I am not able to retrieve any Transfer event happening. But the thing is that, when I try to getPastEvents, I successfully retrieve the correct data so it is most likely not due to another part of the code.
Here is my relevant piece of code:
const Web3 = require('web3')
const nodeClient = require('node-rest-client-promise').Client()
const dotenv = require('dotenv')
dotenv.config()
const CONTRACT_ADDRESS = '0x8d4B648F7fAB1c72d1690b42693fb7525ce3025e'
const projectId = process.env.INFURA_KEY
const etherscanKey = process.env.ETHERSCAN_KEY
const etherscan_url = `http://api.etherscan.io/api?module=contract&action=getabi&address=${CONTRACT_ADDRESS}&apikey=${etherscanKey}`
async function getContractAbi() {
const etherscan_response = await nodeClient.getPromise(etherscan_url)
const CONTRACT_ABI = JSON.parse(etherscan_response.data.result);
return CONTRACT_ABI;
}
async function handleTransferEvent(event) {
try{
const fromAddress = event.returnValues.from
const tokenId = event.returnValues.tokenId
console.log("SOMEONE TRANSFERED NFT!")
if(fromAddress == '0x0000000000000000000000000000000000000000') {
console.log("Minted:\n", event.returnValues)
/* Do stuff */
}
}
catch(err) {
console.log(err)
console.log("ERROR WHILE HANDLING TRANSFER EVENT")
}
}
const init = async () => {
var web3 = new Web3('wss://mainnet.infura.io/ws/v3/' + projectId)
console.log("Connected to mainnet")
const CONTRACT_ABI = await getContractAbi()
console.log("Retrieved contract abi")
const contract = new web3.eth.Contract(
CONTRACT_ABI,
CONTRACT_ADDRESS
)
contract.events.Transfer({})
.on('data', handleTransferEvent)
.on('error', console.error)
console.log('Started listening minting events...')
}
init()
You can check the smart contract from https://etherscan.io/address/0x8d4b648f7fab1c72d1690b42693fb7525ce3025e#code
EDIT: I think problem might be related to calling listen event inside a function.

How to create a async function and export it?

Using node + express. I want to create a module that use several querys.
how can I export this asynchronous function to app.js?
This is the function that im trying to make it work:
app.js (where io are socketio instance)
const users = require('./sockets/users')(io)
users.js
const Users = require('../models/Users.model')
const users = async function(client){
client.on('connection', socket =>{
socket.on('userAdd',(data) =>{
console.log(data);
})
const users = await Users.find()
console.log(users[0]);
})
}
module.exports = users
Error: SyntaxError: await is only valid in async function
First create two files. you can create functions into one and can export it and in another file you can import that functions. check the code below.
> server.js
const addition = require('./addition.js') // path to your another file
const result = addition.add(5, 8) // calling function of another file
console.log(result)
another file
> addition.js
const add = (x, y) => x + y;
module.exports = { add } // export this function
output:
13
I was typing the async keyword in the incorrect function. Its in the connection function
const Users = require('../models/Users.model')
const users = function(client){
client.on('connection', async socket =>{
socket.on('userAdd',(data) =>{
console.log(data);
})
const users = await Users.find()
console.log(users[0]); //user 1
})
}
module.exports = users
You can try to use a class for that and export that class and use it

async/await not working with mongoose instance methods

I'm try to create user mongoose document which require a storage path. I want to await for directory to be created and path resolved. But it is not working and after user is saved, user.storagePath is still undefined. please figure out problem.
Following is code of createStorage()
const getMountRoot = require('../configuration/configuration').getMountRoot
const path = require('path')
const fs = require('fs')
const Logger = require('../configuration/configuration').getLogger
module.exports = (email, firstName, secondName) => {
email = String(email).toLowerCase().replace(/[#_\.\-]/g, '')
firstName = String(firstName).toLowerCase().replace(/[#_\.\-]/g, '')
secondName = String(secondName).toLowerCase().replace(/[#_\.\-]/g, '')
let storagePath = path.join(getMountRoot(), `${secondName}${email}${firstName}`)
return fs.promises.mkdir(storagePath, { recursive: true })
.then(() => { return storagePath })
.catch(() => {Logger.log(err); return storagePath})
}
And following is instance method
const createStorage = require('../extra/create-storage')
userSchema.methods.createStorage = async function() {
this.storagePath = await createStorage(this.email, this.firstName, this.secondName)
}
Kindly note that I call createStorage() on User instance before calling the save()
As #qqilihq figured, I need to await at instance method call. Doing that worked correctly.

Loopback get model from datasource with discoverSchemas

I've been testing loopback for couple hours now and everyhing is working fine when i'm creating models manually and modify the model.json created to match my oracleDb columns name.
But i'm getting stuck when i want to get a model from my oracle db to avoid to write the 50 columns manually..
I made a test with a table called "atest" and contains a column "name" and "id".
It's create the atest.json , and add this in model-config.json:
"Atest": {
"dataSource": "oracledb",
"public": true
}
But in my atest.json there is just a "undefined"..
My discover-models.js file :
'use strict';
const loopback = require('loopback');
const promisify = require('util').promisify;
const fs = require('fs');
const writeFile = promisify(fs.writeFile);
const readFile = promisify(fs.readFile);
const mkdirp = promisify(require('mkdirp'));
const DATASOURCE_NAME = 'oracledb';
const dataSourceConfig = require('./server/datasources.json');
const db = new loopback.DataSource(dataSourceConfig[DATASOURCE_NAME]);
discover().then(
success => process.exit(),
error => { console.error('UNHANDLED ERROR:\n', error); process.exit(1); },
);
async function discover() {
// It's important to pass the same "options" object to all calls
// of dataSource.discoverSchemas(), it allows the method to cache
// discovered related models
const options = { relations: false };
// Discover models and relations
const atestSchemas = await db.discoverSchemas('ATEST', options);
// Create model definition files
await mkdirp('common/models');
var response = await writeFile(
'common/models/atest.json',
JSON.stringify(atestSchemas['ATEST'], null, 2)
);
console.log(response);
// Expose models via REST API
const configJson = await readFile('server/model-config.json', 'utf-8');
console.log('MODEL CONFIG', configJson);
const config = JSON.parse(configJson);
config.Atest = { dataSource: DATASOURCE_NAME, public: true };
await writeFile(
'server/model-config.json',
JSON.stringify(config, null, 2)
);
}
My oracle connection is working fine, i don't get it, any idea?
Add a console.log after you invoke discoverSchemas:
// Discover models and relations
const atestSchemas = await db.discoverSchemas('ATEST', options);
console.log(atestSchemas);
You should see that the key is not just 'ATEST', as referenced later with atestSchemas['ATEST']. The key is 'SCHEMA_NAME.ATEST' (SCHEMA_NAME will vary as per your environment).
If you target the appropriate key, you should get what you're looking for.

Resources