send a message to random channel on join

if my bot has permissions, how can i make it so that it sends a message to a random channel when joining a server?
16 Replies
d.js toolkit
d.js toolkit16mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
d.js docs
d.js docs16mo ago
event (event) Client#guildCreate Emitted whenever the client joins a guild.
Youssef
Youssef16mo ago
Listen to the guildCreate event, get the channels collection, use random function and send a message to it. You can also add a filter to only do this with textchannels
d.js docs
d.js docs16mo ago
method Collection#random() Obtains unique random value(s) from this method Collection#map() Maps each item to another value into an array. Identical in behavior to Array.map().
Youssef
Youssef16mo ago
Skip the map one
keekztah
keekztahOP16mo ago
thank you how can i get the channel?
Cae
Cae16mo ago
You define it then send to it?
Youssef
Youssef16mo ago
const { channels } = <Guild>;
let textChannels = channels.filter((c) => c.type === 0);
let randomChannel = textChannels.random();

...code
const { channels } = <Guild>;
let textChannels = channels.filter((c) => c.type === 0);
let randomChannel = textChannels.random();

...code
keekztah
keekztahOP16mo ago
thank you there was no error but nothing happened
Youssef
Youssef16mo ago
Well that's how you get the channel no just add a check that it exist, and send to it a message
keekztah
keekztahOP16mo ago
module.exports = {
name: 'guildCreate',
async execute(guild, client) {
const channels = guild;
let textChannels = channels.filter((c) => c.type === 0);
let randomChannel = textChannels.random();


randomChannel.send({ content: `This is a test`})
}
}
module.exports = {
name: 'guildCreate',
async execute(guild, client) {
const channels = guild;
let textChannels = channels.filter((c) => c.type === 0);
let randomChannel = textChannels.random();


randomChannel.send({ content: `This is a test`})
}
}
Youssef
Youssef16mo ago
module.exports = {
name: 'guildCreate',
async execute(guild, client) {
const { channels } = guild;
let textChannels = channels.cache.filter((c) => c.type === 0);
let randomChannel = textChannels.random();

if(randomChannel) randomChannel.send({ content: `This is a test`})
}
}
module.exports = {
name: 'guildCreate',
async execute(guild, client) {
const { channels } = guild;
let textChannels = channels.cache.filter((c) => c.type === 0);
let randomChannel = textChannels.random();

if(randomChannel) randomChannel.send({ content: `This is a test`})
}
}
try this it should work
Youssef
Youssef16mo ago
also you can use this instead of 0 in the type
treble/luna
treble/luna16mo ago
the bot needs to have access to that channel and iirc cache contains all channels even if the bot does not have access
Youssef
Youssef16mo ago
^ yes also check for permissions when filtering
d.js docs
d.js docs16mo ago
method TextChannel#permissionsFor() Gets the overall set of permissions for a member or role in this channel, taking into account channel overwrites.
Want results from more Discord servers?
Add your server