node js discord bot is not works - javascript

/home/discord/project/bloodmoon/node_modules/discord.js/src/client/Client.js:42
} catch {
^
SyntaxError: Unexpected token {
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:616:28)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/home/discord/project/bloodmoon/node_modules/discord.js/src/index.js:8:11)
i'm interested in developing discord bot. so i tried it. first, it works.
but, i moved the server and i setted it. and it not works! please give advise to me.
(please understand i can't write a English essay well. sorry)

If you check the discord.js source code, you can see that they're trying to silently ignore if there is an error when their code requires the worker_threads module. They simply omit the usual (error) part after catch:
try {
// Test if worker threads module is present and used
data = require('worker_threads').workerData || data;
} catch {
// Do nothing
}
However, it only works in newer versions of JavaScript and older ones will throw a SyntaxError. So, you must be using an older version of Node, because optional catch binding is only available in Node.js v10+.
The following is an example in Node v8:
// using Node v8
try { error } catch { console.log('oops') }
⬇⬇ result ⬇⬇
try { error } catch { console.log('oops') }
^
SyntaxError: Unexpected token {
While it just works fine in Node v10+:
// using Node v16
try { error } catch { console.log('oops') }
⬇⬇ result ⬇⬇
oops
To solve this, you need to update your Node version to at least v12, as mentioned in the discord.js docs:
v12 requires Node 12.x or higher, so make sure you're up-to-date. To
check your Node version, use node -v in your terminal or command
prompt, and if it's not high enough, update it! There are many
resources online to help you with this step based on your host system.

Related

node.js SyntaxError: Unexpected token { in Version v0.10.43

I habe the following line of code in my js file:
const { connectDb } = require("./db");
When I run nodejs locally everthing is fine and I dont get an error.
Im using v10.16.0 locally. When I put that on a server, I got the follwoing error:
2019-07-23 15:49:36.925754500 const { connectDb } = require("./db");
2019-07-23 15:49:36.925779500 ^
2019-07-23 15:49:36.930604500 SyntaxError: Unexpected token {
2019-07-23 15:49:36.930606500 at Module._compile (module.js:439:25)
2019-07-23 15:49:36.930607500 at Object.Module._extensions..js (module.js:474:10)
2019-07-23 15:49:36.930608500 at Module.load (module.js:356:32)
2019-07-23 15:49:36.930609500 at Function.Module._load (module.js:312:12)
2019-07-23 15:49:36.930610500 at Function.Module.runMain (module.js:497:10)
2019-07-23 15:49:36.930611500 at startup (node.js:119:16)
2019-07-23 15:49:36.930612500 at node.js:945:3
I'm running Version v0.10.43 on that server. What am I doing wrong?
I'm running Version v0.10.43 on that server. What am I doing wrong?
You're running v0.10.43.
Is it too old to support destructuring (which was added in 6.0.0 … which is also past end of life!).
You could replace it with:
const connectDb = require("./db").connectDb
… but are likely to run into other problems down the line.
v0.10.43 is from 2013. It is over half a decade old. It is past the end of life. It is not supported. It does not get security releases. Don't use it.
Use a supported version instead.

Plain text SASL authentication in gremlin-javascript

I'm trying to connect to a Gremlin server with the JavaScript driver variant.
Up to package version 2.7.0, this is done easily by passing options to Gremlin.createClient() as in this example for Azure Cosmos DB:
const client = Gremlin.createClient(
config.port,
config.endpoint,
{
"session": false,
"ssl": true,
"user": `/dbs/${config.database}/colls/${config.collection}`,
"password": config.primaryKey
}
);
In newer versions of the package I can't get it done. The official docs suggest using gremlin.driver.auth.PlainTextSaslAuthenticator. However, that method seems to be not implemented in the package and returns TypeError: Cannot read property 'PlainTextSaslAuthenticator' of undefined
My test code (same config.js as in the working example):
const gremlin = require("gremlin");
const config = require("./config");
const Graph = gremlin.structure.Graph;
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
const graph = new Graph();
const authenticator = new gremlin.driver.auth.PlainTextSaslAuthenticator(
`/dbs/${config.database}/colls/${config.collection}`,
config.primaryKey
);
const g = graph.traversal().withRemote(new DriverRemoteConnection(`ws://${config.endpoint}:${config.port}`, { authenticator });
Return:
C:\repos\gremlin-test\index.js:9
const authenticator = new gremlin.driver.auth.PlainTextSaslAuthenticator(
^
TypeError: Cannot read property 'PlainTextSaslAuthenticator' of undefined
at Object.<anonymous> (C:\repos\gremlin-test\index.js:9:47)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:191:16)
at bootstrap_node.js:612:3
Anyone know a solution to this?
I've not got too much experience with Gremlin.js, but I just downloaded it and have manually searched through all of its files - I can't find any trace of a PlainTextSaslAuthenticator function or its declaration.
This could mean one of two three -
Its function has been (accidentally) removed
It uses a third party package that has been (accidentally) removed
It has not been added to the package yet
Upon a swift Google search, I found this link which seems to show it being added to /lib/driver/auth, but that directory doesn't seem to exist in the package I got through npm install gremlin. Perhaps it is yet to be released?
I would therefore suggest you raise an issue on Github, but it seems that repository you linked doesn't allow for issues to be raised. So perhaps email/contact the author?
EDIT:
Thanks to Stephen for the link - the code hasn't been merged to the package yet. Keep track of it here.

Invoke cucumber cli programatically through index.js of node

I have been given an automation framework in CucumberJS and node with selenium. But it has an older version of cucumber which relies on promises. In an attempt to avail latest synchronous step feature, I decided to upgrade the cucumberJS version (1.3.3 to 4.2.1)
Now the problem is the code that was used to invoke cucumber cli programmatically in index.js isnt working anymore. I made all the other changes in step definitions and world.js, but I am not able to figure out how do I run this thing via node, something like
node index.js --tags #SampleFeature
This used to work before with the older version but not anymore.
Code that was working before -
// execute cucumber
let cucumberCli = Cucumber.Cli(process.argv);
cucumberCli.run(succeeded => {
var code = succeeded ? 0 : 1;
function exitNow() {
process.exit(code);
}
if (process.stdout.write('')) {
exitNow();
} else {
process.stdout.on('drain', exitNow);
}
});
Now it throws the error like this after version update
/Users/../node_modules/babel-runtime/helpers/classCallCheck.js:7
throw new TypeError("Cannot call a class as a function");
^
TypeError: Cannot call a class as a function
at exports.default (/Users/../node_modules/babel-runtime/helpers/classCallCheck.js:7:11)
at Object.Cli (/Users/../node_modules/cucumber/lib/cli/index.js:78:34)
at Object.<anonymous> (/Users/../index.js:90:10)
at Module._compile (internal/modules/cjs/loader.js:678:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:689:10)
at Module.load (internal/modules/cjs/loader.js:589:32)
at tryModuleLoad (internal/modules/cjs/loader.js:528:12)
at Function.Module._load (internal/modules/cjs/loader.js:520:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:719:10)
at startup (internal/bootstrap/node.js:228:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:576:3)
I googled a lot but did not find an alternative. Tried multiple things as calling Cli as class with new keyword, didn't work. Tried removing it and running via plain cucumber cli runner, didn't work.
PS. I come from a Cucumber with Java background where things were simpler :)
You need to create a new CLI object, then use it's .run method:
let runArgs = ['The cucumber args array here'];
let cliArgs = {argv : runArgs, cwd: process.cwd(), stdout: process.stdout};
let cli = (new require('cucumber').Cli)(cliArgs);
cli.run(); //Returns a promise
For reference I've employed the same approach with Typescript and cucumber 7.3.2
import { Cli } from '#cucumber/cucumber';
const runArgs = ['-p', argv['profile'], '--tags', argv['tags'], '--fail-fast'];
const cliArgs = { argv: runArgs, cwd: process.cwd(), stdout: process.stdout };
const cli = new Cli(cliArgs);
await cli.run();

remote function returns undefined or null in electron renderer process

I'm currently very interested in the comprehensive opportunites granted by electron.js and its modules. Unfortunately, I keep getting the same error in my renderer process (named 'connector.js') when trying to start my application.
Here is the error:
App threw an error during load
TypeError: Cannot match against 'undefined' or 'null'.
at Object.<anonymous> (D:\Eigene Dateien\Desktop\Coding\DesktopApps\EVT\extFunctions\connector\connector.js:2:44)
at Object.<anonymous> (D:\Eigene Dateien\Desktop\Coding\DesktopApps\EVT\extFunctions\connector\connector.js:22:3)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (D:\Eigene Dateien\Desktop\Coding\DesktopApps\EVT\main.js:9:1)
And here is my connector.js:
const $ = require('jquery');
const {BrowserWindow} = require('electron').remote;
let Remotewin = remote.getFocusedWindow();
$("#minimize").click(function(){
Remotewin.minimize();
});
$("#maximize").click(function(){
if(!Remotewin.isMaximized()){
Remotewin.maximize();
}else{
Remotewin.unmaximize();
}
});
$("#close").click(function(){
Remotewin.close();
});
As you can see clearly, I wanted to create my own menubar at the top frame of the window, but the functionality is getting seemingly demolished by this error. I already searched half of the internet and stackoverflow, but the every answer I found refered to a webpack and/or electron bug they couldn't directly influence.
That's why I want to clearly point out, that I am NOT using webpack in this project. Only external module I added is jquery, as you can see in the code.
So my question; Have you experiended this error in this context and do you maybe even know a solution? Or can you refer to someone with similiar problems?
Thank you in advance, J0nny
Since getFocusedWindow() is a a static method of BrowserWindow,
let Remotewin = remote.getFocusedWindow();
should be:
let Remotewin = BrowserWindow.getFocusedWindow();

Unable to run appjs sample app using node.js

I am trying to run one of the sample projects using appjs which is present over here https://github.com/appjs/appjs/tree/master/examples. I am using the latest version of node.js (v4.1.0
) on Windows (64 bit machine)
When I try and run the example using the below command on Command Prompt
node --harmony index.js
I get an error as follows,
Error: AppJS requires Node is run with the --harmony command line flag
at Object.<anonymous> (F:\programs\appjs_examples\node_modules\appjs\lib\ind
ex.js:2:9)
at Module._compile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (F:\programs\appjs_examples\octosocial\index.js:1:73)
at Module._compile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)
I tried searching for this issue but I couldn't find a solution. Can anyone tell me how to use node.js with the harmony flag?
UPDATE
My index.js looks like this
var app = require('appjs'),
github = new (require('github'))({ version: '3.0.0' }),
KEY_F12 = process.platform === 'darwin' ? 63247 : 123;
app.serveFilesFrom(__dirname + '/assets');
var window = app.createWindow({
width: 460,
height: 640,
resizable: false,
disableSecurity: true,
icons: __dirname + '/assets/icons'
});
window.on('create', function(){
window.frame.show();
window.frame.center();
});
window.on('ready', function(){
var $ = window.$,
$username = $('input[name=username]'),
$password = $('input[name=password]'),
$info = $('#info-login'),
$label = $info.find('span'),
$buttons = $('input, button');
$(window).on('keydown', function(e){
if (e.keyCode === KEY_F12) {
window.frame.openDevTools();
}
});
$username.focus();
$('#login-form').submit(function(e){
e.preventDefault();
$info.removeClass('error').addClass('success');
$label.text('Logging in...');
$buttons.attr('disabled', true);
github.authenticate({
type: 'basic',
username: $username.val(),
password: $password.val()
});
github.user.get({}, function(err, result) {
if (err) {
$info.removeClass('success').addClass('error');
$label.text('Login Failed. Try Again.');
$buttons.removeAttr('disabled');
} else {
loggedIn(result);
}
});
});
function loggedIn(result){
$label.text('Logged in!');
$('#user-avatar').append('<img src="'+result.avatar_url+'" width="64" height="64">');
$('#user-name').text(result.name);
$('#login-section').hide();
$('#profile-section').show();
['Followers', 'Following'].forEach(function(type){
github.user['get'+type]({ user: result.login }, populate.bind(null, type.toLowerCase()));
});
}
Now with v0.12 of Node.js I get below error
F:\softwares\Node.js_v0.12\node_modules\appjs\lib\index.js:2
throw new Error ('AppJS requires Node is run with the --harmony command line
Error: AppJS requires Node is run with the --harmony command line flag
at Object.<anonymous> (F:\softwares\Node.js_v0.12\node_modules\appjs\lib\ind
ex.js:2:9)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (F:\softwares\Node.js_v0.12\index.js:1:73)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
Just tested your code out locally with node v0.12.7 and v4.0.0. Looks like the node_modules/appjs/lib/index.js check makes sure that proxy is enabled no matter what.
By default the --harmony flag does not enable proxies. However you can use --harmony_proxies.
To help you understand what is happening:
Open node in your terminal, Then type Proxy. You will get 'Proxy is not defined'.
Now, open node --harmony in your terminal and do the same. You will get the same output.
Now, with node --harmony-proxies. Bam, you get an empty object.
You should be able to run this with v4.x.x however, you will still need the proxies flag for harmony.
When the merge happened with node.js and io.js for v4 they released a page of ES6 features that are shipped if you are using 4.x.x. https://nodejs.org/en/docs/es6/
https://github.com/appjs/appjs is deprecated btw, but once you pass the module's test of features, it will require 32bit ;)
Edit:
To properly run your app use the following:
node --harmony-proxies index.js
Here is a screenshot to show the expected output from step 3 above.

Categories