I’m sure this is fairly simple and I’m just overthinking things as I tend to do but..
I am trying to add code to my discord bot so that when a channel is mentioned (<#channel.ID>) the bot will then post the channel name, not a link to the channel or a channel mention, just the channel name (channel-example-name). I’ve done a bit of sleuthing to see if I could find something similar but everything I find is trying to mention the channel in an embed or post.
I’ve looked through discord.js documentation and even reread through the guide to see if there would be any answers in the guide. I also haven’t seen any guidance on managing this on the discord.js discord server.
Main File:
const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, GatewayIntentBits } = require('discord.js');
const { token } = require('./config.json');
const client = new Client({ intents: [GatewayIntentBits.Guilds]});
client.commands = new Collection();
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) {
// Grab all the command files from the commands directory you created earlier
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}
const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));
for (const file of eventFiles) {
const filePath = path.join(eventsPath, file);
const event = require(filePath);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}
client.login(token);
I have a separate folder called events
that includes ready.js
(start up event), interactionCreate.js
for command handling, and messageCreate.js
for the listener and base set up from the discord.js guide. The last is below:
const { Client, Events } = require('discord.js');
const client = new Client({ intents: ['Guilds', 'GuildMessages'] });
client.once(Events.ClientReady, () => {
console.log('Ready!');
});
client.on(Events.MessageCreate, message => {
// Check if the message contains a channel mention
if (message.mentions.channels.size > 0) {
const mentionedChannel = message.mentions.channels.first();
// Respond to the message
message.reply(`Chanel mentioned above: {mentionedChannel.name}`);
}
});
client.on(Events.MessageCreate, async message => {
let args;
if (message.guild) {
let prefix;
if (message.content.startsWith(globalPrefix)) {
prefix = globalPrefix;
} else {
const guildPrefix = await prefixes.get(message.guild.id);
if (message.content.startsWith(guildPrefix)) prefix = guildPrefix;
}
}
});
Errors/Debugging:
The bot is not currently throwing any errors after tinkering around with it nor am I getting anything from debugging.