Ad
Fetch/Listen For A New Message In The Channel
Hello so I'm making a bot that will fetch a message in a specific channel, copies it and then sent it to another channel. I tried using messages.fetch()
but it doesn't work and always returns with DiscordAPIError: Cannot send an empty message
. This is what my code is currently like :
if (message.content === 'Log'){
message.channel.messages.fetch({limit: 10})
.then(messages => {
message.channel.send(messages)}).catch(console.error)}
I hope anyone can help me with this, thanks in advance.
Ad
Answer
Assuming you want the bot to output the content of the last 10 messages in the channel, this should work:
let msgs = [];
message.channel.messages.fetch({limit: 10})
.then(messages => {
return messages.each(msg => msgs.push(msg.content));
})
.then(messages => {
message.channel.send(msgs.reverse().join("\n")); // outputs the messages separated by a newline
});
If you only want to get one message you can just do:
message.channel.messages.fetch("MESSAGE_ID")
.then(msg => {
message.channel.send(msg.content);
});
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad