I'm wondering if it's even possible to get user's input through DMs after calling a slash command. I've created a little mini-game that would ideally be played in the user's DMs to avoid spamming the channel it was called in. An example of my code structure is as followed:
module.exports = {
data: new SlashCommandBuilder()
.setName('game')
.setDescription('Play game'),
async execute(interaction) {
const score = await gameLogic(interaction.user);
await interaction.reply(score);
},
};
Then I send the user a DM letting them know that the game has started and call a function that will wait for the user input.
async function gameLogic(user) {
user.send("Game time");
let userInput = getUserInput();
.... game logic ...
}
I've tried using message collectors and awaitMessage but can't seem to get it working. Really trying to exhaust all options until moving on to a different solution.
I appreciate any advice!
EDIT Unfortunately I don't remember exactly what I've tried but I can give some examples.
My message collector was something along the lines of this:
async function getUserInput() {
console.log("Awaiting user input");
const filter = message => message.author.id === m_user.id;
const collector = m_interaction.channel.createMessageCollector({ filter, max: 1, time: 15000, errors: ['time'] })
collector.on('collect', message => console.log(`You wrote ${message.content}`));
collector.on('error', collected => console.log("Time out"));
}
And my awaitMessage was something like this:
async function getUserInput() {
const filter = message => message.author.id === m_user.id;
const message = await m_interaction.channel.awaitMessages({ filter, max: 1, time: 15000, errors: ['time'] })
.then(message => m_interaction.channel.send(`You wrote ${message.content}`))
.catch(collected => m_interaction.channel.send("Time out"));
console.log(message);
}
I'm sure these will work for collecting input from the channel the command was called in, but I'm not sure how I could alter them to collect input through the DMs. I did read a little into dmChannel but couldn't seem to get it working the way I wanted it to.
EDIT 2 + SOLUTION I've managed to store the DMChannel with the user by using.
const channel = user.createDM();
And I can now use the MessageCollector there. Hopefully this helps.
I've managed to store the DMChannel with the user by using.
const channel = user.createDM();
And I can now use the MessageCollector there. Hopefully this helps.