how to make a menu in discord.js? - javascript

I am currently making a discord bot using discord.js and i am trying to make a menu command so when a user types [prefix]menu it sends a picture of the menu
i can do that easily but i am trying to make it multipaged, i know i could just make a menu1, menu2 and menu3 command but id like to have a command saying nextpage and previouspage this is what i have so far but it doesn't work and there is no errors
if (command === "menu") {
output()
message.channel.send({file: './images/menu1.png'});
curPage = 1;
}
if (command === "next page") {
output()
curPage++;
if(curPage >= 3) {
output()
curPage = 3; message.channel.send("You're on the last page!");
}
} else if (command === "previous page") {
output()
curPage--;
if(curPage <= 0) {
output()
curPage = 1; message.channel.send("You're on the first page!");
}
message.channel.send({file: `./images/menu${curPage}.png`});
}

Use a ReactionCollector instead.
A collect event would be emitted when a user reacts to the targeted message.
Example:
const collector = message.createReactionCollector((reaction, user) =>
user.id === message.author.id &&
reaction.emoji.name === "◀" ||
reaction.emoji.name === "▶" ||
reaction.emoji.name === "❌"
).once("collect", reaction => {
const chosen = reaction.emoji.name;
if(chosen === "◀"){
// Prev page
}else if(chosen === "▶"){
// Next page
}else{
// Stop navigating pages
}
collector.stop();
});
Docs: Message, ReactionCollector, Reaction, User

I think the best way to achieve this, would be with .awaitMessages?
https://discord.js.org/#/docs/main/stable/class/TextChannel?scrollTo=awaitMessages
It might be worth trying something along these lines, however I'm not 100% sure about how to await multiple times for paging back and forth... I'd be interested in seeing someone elses solution for this.
For example:
if (command === "menu") {
message.channel.send({file: './images/menu1.png'});
.then(() => {
message.channel.awaitMessages(response => response.content === 'next', {
max: 1,
time: 30000,
errors: ['time'],
})
.then((collected) => {
message.channel.send({file: './images/menu2.png'});
})
.catch(() => {
// Do something with error
});
});
}

Related

I want bot to send first message only

I'm making a spam command. I want users to enter a message to spam but only the first message should be spam. No other message should be registered if 1 spam is already running.
My code -
else if (command === prefix + "spam" && args[0] !== undefined) {
message.reply("Quick tip: Use `stopspam` command to stop this")
spamInterval = setInterval(() => {
message.channel.send(`${args.slice(0).join(" ")}`);
}, 1500);
}
Define outside an status variable
let spamrunning=0
Then add the condition of that status variable, and the change of it.
else if (command === prefix + "spam" && args[0] !== undefined && spamrunning==0) {
message.reply("Quick tip: Use `stopspam` command to stop this")
spamrunning=1
spamInterval = setInterval(() => {
message.channel.send(`${args.slice(0).join(" ")}`);
}, 1500);
}
and change status again when you clear the interval
else if (command === prefix + "stopspam") {
...
spamrunning=0
}

waiting for reply gives error (time out) after reply. Discord.js

I'm creating a discord bot and I'm trying to create a bot in which you can adopt children (in my code you can get up to 5 children).
Now I have no idea if I'm even doing it the correct way but it looks like it should be working. I'm a kinda beginner in JS.
I found the code I'm using as example here.
I then edited it to work with my code. The error I get is if the mentioned user answers with yes, my bot responds with "They didn't respond in time. try again later" while it should be "you're now adopted".
Here is my code:
else if (message.content.startsWith(`${prefix}adopt`)) {
let filter = m => m.author.id === message.mentions.users.first().id
let marryEmbed = new discord.MessageEmbed();
marryEmbed.setDescription(mentioned.username + ', ' + randomadopt + `\`YES\` / \`NO\``)
marryEmbed.setColor()
marryEmbed.setTimestamp()
message.channel.send(marryEmbed).then(() => {
//console.log(rUser, rMentioned)
message.channel.awaitMessages(filter, {
max: 1,
time: 30000,
errors: ['time']
})
.then(message => {
message = message.first()
if (message.content.toUpperCase() == 'YES' || message.content.toUpperCase() == 'Y') {
let replyembed = new discord.MessageEmbed();
replyembed.setColor()
replyembed.setDescription(randomAdopted)
replyembed.setTimestamp()
if (!rUser.child1) {
if(!rUser.partner){
{code here}
else{
{code here}
}
}
else if (!rUser.child2) {
if(!rUser.partner){
{code here}
}
else{
{code here}
}
}
} else if (message.content.toUpperCase() == 'NO' || message.content.toUpperCase() == 'N') {
let replyembed = new discord.MessageEmbed();
replyembed.setColor()
replyembed.setDescription('guess they\'ll stay homeless for a bit longer.')
replyembed.setTimestamp()
message.channel.send(replyembed)
//message.reply('keep your head up, you\'re too good for them')
} else {
message.channel.send(`Terminated: Invalid Response`)
}
})
.catch(collected => {
let noreplyembed = new discord.MessageEmbed();
noreplyembed.setColor()
noreplyembed.setDescription('They didn\'t respond in time. try again later')
noreplyembed.setTimestamp()
message.channel.send(noreplyembed)
console.log()
//message.reply('No answer after 30 seconds, request canceled.');
});
})
}
}

Giving multiple prompts over DM's

So I need a command to send 4 different messages to the user, each message with a new prompt, so for example "Prompt 1" and what ever the user responds will be pushed into an array called "config". I thought about using message collectors, but couldn't set it up to collect multiple answers.
Pseudo code:
let config = new Array();
message.author.send("Prompt 1");
config.push(collected.answer).then(
message.author.send("Prompt 2");
config.push(collected.answer).then(
ect...
)
You CAN use message collectors. However, you need to have it in a variable. Here is an example:
msg.author.send('some prompt').then(m => {
let i = 0;
var collector = m.channel.createMessageCollector(me =>
me.author.id === msg.author.id && me.channel === m.channel, {max: /*some number*/})
collector.on('collect', collected => {
if(collected.content === 'end') return collector.stop();
//basically if you want to stop all the prompts and do nothing
i +=1
if(i === 1) return collected.channel.send(/*something*/); //next prompt
if(i === 2) return collected.channel.send(/*something*/); //and so on until you get to the last prompt
})
collector.on('end', collectedMsgs => {
if(collectedMsgs.size < /*amount of prompts*/) {
return collectedMsgs.first().channel.send('Ended early, nothing was done.');
}
//some action you would do after all are finished
})
})
There may be some missing parentheses, you will have to add them.

I made this and my bot loops the "else" function. I'm confused cuz I don't think I have any intentions to make a looping function

I made a bot with purpose to manage discord roles and here's my code briefly
bot.on('message', (message) => {
const parts = message.content.split(' ');
if (parts[0] == 'Platform' || 'platform') {
if (parts[1] == 'iOS') {
message.channel.send(`thx, ${message.author}`)
message.member.roles.add(iOS);
}
else {
message.channel.send(`nooo`)
}
}
}
Why my else command keeps going over and over? How am I supposed to do in order to stop it?
Your code should read:
if (parts[0] == 'Platform' || parts[0] == 'platform') {

Leave Command Discord JS

Leave command not working on my Discord bot which is written in JS.
This is the snippet of code I'm using.
if(command == 'leave') {
client.leaveVoiceChannel;
However the command which should make the bot leave the voice channel does not seem to work. Here is how I'm using the code.
const Eris = require('eris');
const client = new Eris(require('./config.json').token, { maxShards: 1 });
fs = require('fs')
var stations = fs.readFileSync("./stations.txt", {"encoding": "utf-8"}).toString()
client.connect();
client.on('ready', () => {
console.log('Ready to go!')
})
client.on('messageCreate', message => {
if(message.author.bot) return;
if(message.content.startsWith('!')) {
let command = message.content.substring(1).split(" ")[0];
let args = message.content.substring(2 + command.length);
if(command == 'leave') {
client.leaveVoiceChannel;
} else if(command == 'streams') {
message.channel.createMessage(stations);
} else if(command == 'radio') {
if(args == '') return message.channel.createMessage(`:exclamation: Please specify the radio stream example: **!radio <stream> or use command **!streams** to see list.`);
if(require('./stations.json')[args]) {
if(!message.member.voiceState.channelID) return message.channel.createMessage(`:exclamation: You need to be in a voice channel to play that stream.`);
client.joinVoiceChannel(message.member.voiceState.channelID).then(vc => {
if(vc.playing) vc.stopPlaying();
message.channel.createMessage(`:radio: You are listening to Streaming station **${args}**. To change the stream use **!radio <stream>**`);
vc.play(require('./stations.json')[args]);
})
} else {
return message.channel.createMessage(`:frowning2: I cannot find a radio stream with that name. Make sure it has capitals and the correct spelling. Type **!streams** to see stream list.`);
}
}
}
})
I'm not sure where I'm going wrong here.
if(command == 'stopstream') {
client.leaveVoiceChannel(message.member.voiceState.channelID);
message.channel.createMessage(`Thanks for tuning in!`); }

Categories

Resources