How execute other prompt when prompt previous is true on Yeoman? - javascript

How execute prompt2 when prompt1 is true on Yeoman as shown below?
var prompts = [
{name: 'prompt1', message: 'Ask 1?'},
{name: 'prompt2', message: 'Ask 2?'}
];

Yeoman uses a thing called Inquirer.js for the prompt system. Here's an example of how you can ask Question 2 if Question 1 was true:
inquirer.prompt([{
name: 'movie',
type: 'confirm',
message: 'Have you seen a movie lately?'
}, {
when: function (response) {
return response.movie;
},
name: 'good-or-not',
message: 'Sweet! Was it any good?'
}], function (response) {});
From the Inquirer.js documentation:
when: (Function) Receive the current user answers hash and should return true or false depending on wheter or not this question should be asked.

Related

How do I create a text channel discord.js 14

I wanted to ask how to create channels in discord.js 14 (i tried to researched but found nothing) I want to make a simple text channel! :)) Also, with the stuff i found, it worked ( no errors) it just didnt create anything / post errors in debug log
It's literally under Discord.js documentation. Please, next time before asking a question read the docs.
https://discord.js.org/#/docs/discord.js/main/general/welcome
guild.channels.create({
name: "hello",
type: ChannelType.GuildText,
parent: cat[0].ID,
// your permission overwrites or other options here
});
//If the command is slash type
const { Permissions } = require("discord.js")
interaction.guild.channels.create({
name: "new-channel",
type: 'GUILD_TEXT',
permissionOverwrites: [
{
id: interaction.guild.id,
accept: [Permissions.FLAGS.VIEW_CHANNEL],
},
],
});
//If the command is message type
const { Permissions } = require("discord.js")
message.guild.channels.create({
name: "new-channel",
type: 'GUILD_TEXT',
permissionOverwrites: [
{
id: message.guild.id,
accept: [Permissions.FLAGS.VIEW_CHANNEL],
},
],
});

i can't assign a role to an user, via my code [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 months ago.
Improve this question
const Discord = require("discord.js");
require('dotenv').config();
module.exports = {
name: "manuallyverify",
description: "Manually verifies an User.",
limits: {
owner: false,
permission: ["MANAGE_ROLES"],
cooldown: "10s",
},
options: [
{
name: "user",
description: "The User that will be affected by this action.",
type: 6,
required: true,
}
],
run: (client, interaction) => {
let member = interaction.options.getUser("user").toString()
let role = interaction.guild.roles.cache.find(role => role.id == process.env.verified_role_id)
let embed = new Discord.MessageEmbed()
.setTitle(":white_check_mark: ・ Manually Verified the User.")
.addFields(
{
name: "User affected",
value: interaction.options.getUser("user").toString(),
inline: true,
},
{
name: "Role given",
value: `${role}`,
inline: true,
}
)
.setColor("#95eda4");
try {
interaction.guild.member.roles.cache.add(role)
interaction.reply({ embeds: [embed] });
} catch (e) {
interaction.reply({content:`I encountered an error: ${e}`, ephemeral: true});
}
},
};
so, this is my code, i'm making a simple verification bot that will be a template on github , but when i run the command, it gives me this error:
TypeError: Cannot read properties of undefined (reading 'roles')
so, i guess the error is because something is undefinited between interaction.guild, or guild.member. i didnt code for a while so i'm probably missing / forgetting something
also, sorry if my framework is bad
edit: i got this code to work thanks to a guy in the comments, so, thanks for coming here and have a good day
There isn't any property called interaction.guild.member and I assume what you are trying to do is to add the role to the user provided. Therefore, what you could do is use interaction.options.getMember("user") to get the user in the option and then add the role to that GuildMember. A small example:
const Discord = require("discord.js");
require('dotenv').config();
module.exports = {
name: "manuallyverify",
description: "Manually verifies an User.",
limits: {
owner: false,
permission: ["MANAGE_ROLES"],
cooldown: "10s",
},
options: [
{
name: "user",
description: "The User that will be affected by this action.",
type: 6,
required: true,
}
],
run: (client, interaction) => {
let memberToAddRole = interaction.options.getMember("user")
let role = interaction.guild.roles.cache.find(role => role.id == process.env.verified_role_id)
let embed = new Discord.MessageEmbed()
.setTitle(":white_check_mark: ・ Manually Verified the User.")
.addFields(
{
name: "User affected",
value: memberToAddRole.user.toString(),
inline: true,
},
{
name: "Role given",
value: `${role}`,
inline: true,
}
)
.setColor("#95eda4");
try {
memberToAddRole.roles.cache.add(role)
interaction.reply({ embeds: [embed] });
} catch (e) {
interaction.reply({content:`I encountered an error: ${e}`, ephemeral: true});
}
},
};
Note: the code you provided was a bit inefficient and redundant in some parts, so I have changed that accordingly
The error message tells you exactly where and what your issues is. The error message indicates that roles is undefined, so either interaction.guild.roles or interaction.guild.member.roles is undefined.

Get message_id from Telegram message - node.js

I have a problem concerning a Telegram bot I am currently working on. I get messages from users in the following format:
update { update_id: 82618016,
message:
{ message_id: 363,
from: { id: 22303518, first_name: 'Steve', language_code: 'de-DE' },
chat: { id: 22303518, first_name: 'Steve', type: 'private' },
date: 1501501753,
text: 'j' } }
When I want to access the id of the chat I can do this without any problems by using
$.message.chat.id
As soon as a want to get the message_id or first_name I only get "undefined".
$.message.chat.first_name
$.message.message_id
Can anyone help me here? As far as I see it I understood the structure of the message correctly so I don't really know what's the problem here.
Thank you very much in advance
EDIT: I am adding a bit more of my code here:
The main code for the bot (including the webhook) is this:
initializeBot();
function initializeBot(){
const Telegram = require('telegram-node-bot');
const PingController = require('./controllers/ping');
const OtherwiseController = require('./controllers/otherwise');
const tg = new Telegram.Telegram('MY_TOKEN_IS_HERE', {
webhook: {
url: 'https://xxx.herokuapp.com',
port: process.env.PORT || 443,
host: '0.0.0.0'
}
})
tg.router.when(new Telegram.TextCommand('/ping', 'pingCommand'), new PingController())
.otherwise (new OtherwiseController());
}
When the OtherwiseController gets called the following code is called (I reduced it to the essentials to clarify the problem.
class OtherwiseController extends Telegram.TelegramBaseController {
handle($){
console.log($.message.chat.first_name);
console.log($.message.text);
console.log($.message.chat.id);
console.log($.message.message_id);
}
}
The console output for this message
update { update_id: 82618020,
message:
{ message_id: 371,
from: { id: 22303518, first_name: 'Steve', language_code: 'de-DE' },
chat: { id: 22303518, first_name: 'Steve', type: 'private' },
date: 1501509762,
text: 'hello' } }
would be:
undefined
hello
22303518
undefined
Use the below method to extract the keys of your json object, then you can access with an appropriate key:
Object.keys($.message.chat);

how to get stored options from `this.prompt` inside yeoman context?

Basically, yeoman force you to ask everything you need from developer. Although, it’s a good thing, that you can store something and in future runs these things will be autocompleted for developer. The point is that I want to not ask developer if he already answered on that questions.
here is example of basic yeoman generator (name will be saved and autocompleted later):
var yeoman = require('yeoman-generator');
module.exports = yeoman.generators.Base.extend({
init: function () {
var cb = this.async();
this.prompt([{
name: 'name',
message: 'your name:',
store: true,
}, {
name: 'moduleName',
message: 'module name:'
}], function (props) {
console.log(
props.name, // developer’s name
props.moduleName // module’s name
)
}.bind(this));
},
}
});
The question is how to get stored options from this.prompt inside yeoman context to do smth like this:
this.prompt([!this.name.stored && {
name: 'name', // so after first run this will never be asked again
message: 'your name:',
store: true,
}, {
name: 'moduleName',
message: 'module name:'
}], function (props) {
console.log(
props.name, // developer’s name
props.moduleName // module’s name
)
}.bind(this));
There's no public way to access the stored previous prompt answers.
If you want to cache some data and access it later, then use the storage functionality (this.config)
FWIW, the prompt cache is stored into the private this._globalConfig. I'm adding this detail for completeness, you probably shouldn't use it.
You can user default's value as function, where first argument is prev stored answers:
const answers = await this.prompt([
{
type: 'input',
name: 'projectName',
message: 'Your project id',
default: this.appname,
store: true
},
{
type: 'input',
name: 'projectTitle',
message: 'Your project title',
default: ({ projectName }) => projectName
},
])
You can add a config.js that will store and read the information in the users home directory as a config.json file and later the app can read this file that can be used as a default.
{
name: 'authorName',
message: 'What\'s your name?',
'default': self.defaultAuthorName
}
Check out https://www.npmjs.com/package/generator-yo-wordpress where the developer makes use of a config.js file.

How to check whether a value already exists using AJAX in jQuery validator?

$.validator.addMethod("noanon", function(value) {
return ajaxFunction();
}, 'Username already exists.');
The addMethod function takes 3 arguments. A name, the logic to actually run, and lastly, the default message to use for failures.
name: {
required: true,
minlength: 2,
noanon: true
},
Now if I use the ajaxFunction, there sems to be some error which is appearing.
You can use remote method:
rules: {
name: {
remote : "script.php"
}
},
messages: {
name: {
remote : "Page with this name is exists"
}
}
In PHP you take $_GET['name'].
Additional information here

Categories

Resources