await messages, get a channel by the given ID

Viewed 1575

So i want that my poll command asks for the channel and the question in a conversation, but i haven't figured out how to get the channel when the user only gives the ID, i have figured out that i have to use .content but i still don't know how to implement it.

My code:

run: async(message, client, args) => {

  // Channel where the poll should take palce
  await message.channel.send(`Please provide a channel where the poll should take place or cancel this command with "cancel"!`)
  const response1 = await message.channel.awaitMessages(m => m.author.id === message.author.id, {max: 1});
  const channel = response1.first().mentions.channels.first() || response1.content.guild.channels.cache.get()

  if (!channel) {
    return message.channel.send(`You did not mention or provide the ID of a channel where the poll should take place!`)
  }

  // Channel where the poll should take palce
  await message.channel.send(`Please provide a question for the poll!`)
  const response2 = await message.channel.awaitMessages(m => m.author.id === message.author.id, {max: 1});
  let question = response2.first();

  if (!question) {
    return message.channel.send(`You did not specify your question!`)
  }

  const Embed = new Discord.MessageEmbed()
    .setTitle(`New poll!`)
    .setDescription(`${question}`)
    .setFooter(`${message.author.username} created this poll.`)
    .setColor(`0x0099ff`)
  let msg = await client.channels.cache.get(channel.id).send(Embed)
  await msg.react("")
  await msg.react("")
}

And it is this line: response1.content.guild.channels.cache.get() that is writte wrong by me but idk what i have to change/where to add the .content so that it works.

Would be nice if someone can help me.

My message event for the args:

module.exports = async (client, message) => {
    if (!message.content.startsWith(prefix) || message.author.bot) return;
    if (!message.guild) return;
    if (!message.member) message.member = await message.guild.fetchMember(message);
    const args = message.content.slice(prefix.length).split(/ +/g);
    const cmd = args.shift().toLowerCase();
    if (cmd.length == 0) return;
    let command = client.commands.get(cmd)
    if (!command) command = client.commands.get(client.aliases.get(cmd));
    if (command) {
        try {
            command.run(message, client, args)
        } catch (error) {
            console.error(error);
            message.reply('There was an error trying to execute that command!');
        } 
    }
}
2 Answers

You need the content to grab the id, but I assume that's already handled by the code that generates the args parameter.
To get the guild you can use Message.guild, and then just Guild.channels.cache.get()

That means that your code would look like this:

const channel = response1.first().mentions.channels.first() 
  || response1.first().guild.channels.cache.get(args[0]) // Assuming args[0] is your id

So i went around it with making another constructor means:

const ID = client.channels.cache.get(response1.first().content) const channel = response1.first().mentions.channels.first() || ID

It works fine now

Related