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 toolkit11mo 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 docs11mo ago
event (event) Client#guildCreate Emitted whenever the client joins a guild.
youssef
youssef11mo 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 docs11mo 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
youssef11mo ago
Skip the map one
keekztah
keekztah11mo ago
thank you how can i get the channel?
Cae
Cae11mo ago
You define it then send to it?
youssef
youssef10mo 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
keekztah10mo ago
thank you there was no error but nothing happened
youssef
youssef10mo ago
Well that's how you get the channel no just add a check that it exist, and send to it a message
keekztah
keekztah10mo 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
youssef10mo 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
youssef10mo ago
also you can use this instead of 0 in the type
treble/luna
treble/luna10mo 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
youssef10mo ago
^ yes also check for permissions when filtering
d.js docs
d.js docs10mo ago
method TextChannel#permissionsFor() Gets the overall set of permissions for a member or role in this channel, taking into account channel overwrites.