Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
725 views
in Technique[技术] by (71.8m points)

node.js - Discord.js sending a message to a specific channel

I'm failing to achieve something very simple. I can't send a message to a specific channel. I've browsed trough the documentation and similar threads on stack overflow.

client.channels.get().send()

does NOT work. It is not a function. I also do not see it as a method on the Channel class on the official documentation yet every thread I've found so far has told me to use that.

I managed to have the bot reply to a message by listening for a message and then using message.reply() but I don't want that. I want my bot to say something in a specific channel in client.on('ready')

What am I missing?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You didn't provide any code that you already tested so I will give you the code for your ready event that will work!

    client.on('ready', client => {
        client.channels.get('CHANNEL ID').send('Hello here!');
    })

Be careful that your channel id a string.

Let me know if it worked, thank you!

2020 Jun 13 Edit:

A Discord.js update requires the cache member of channels before the get method.

If your modules are legacy the above would still work. My recent solution works changing the send line as follows.

        client.channels.cache.get('CHANNEL ID').send('Hello here!')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...