guildCreate channe.send is not a function discordJS v13 - discord

im trying to make an event that everytime when someone add my bot to their it will send message, but when i did this happened...
Cannot read properties of undefined (reading 'send')
const client = require("../index");
client.on("guildCreate", async (guild, client) => {
let channel = guild.channels.cache.find(
channel =>
channel.type === "text" &&
channel.permissionsFor(guild.me).has("SEND_MESSAGES")
);
channel.send({ content: "test" })
})
im using discordjs v13.2.0

Related

Discord.js struggling to add a role to a discord member through id's

const { SlashCommandBuilder } = require("discord.js");
module.exports = {
data: new SlashCommandBuilder()
.setName("mute")
.setDescription("Mute a Member in secret")
.addUserOption((option) =>
option.setName("target").setDescription("Select a user").setRequired(true)
),
async execute(interaction, client) {
const message = await interaction.deferReply({
fetchReply: true,
});
const guild = client.guilds.cache.get("872567903839453215");
const userId = interaction.options.getUser("target").id;
console.log(userId);
const user = interaction.guild.roles.cache.get(userId);
console.log(user);
const newMessage = `Muted : ${user}`;
let fRole = message.guild.roles.cache.get("1005988662472888370");
let rRole = message.guild.roles.cache.get("1004543262884888636");
user.roles.add(fRole);
await interaction.editReply({
content: newMessage,
});
},
};
in the code i grab the user mentioned in the message and try to convert to something that would be recognised in user.roles.add(frole);
does anyone know a way to do this?
TypeError: Cannot read properties of undefined (reading 'add')
at Object.execute (DiscordBot\src\commands\tools\MuteMember.js:25:16)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async Object.execute (DiscordBot\src\events\client\interactionCreate.js:11:9)
node:events:505
throw er; // Unhandled 'error' event
^
Error [InteractionAlreadyReplied]: The reply to this interaction has already been sent or deferred.
at ChatInputCommandInteraction.reply (discord.js\src\structures\interfaces\InteractionResponses.js:101:46)
at Object.execute (DiscordBot\src\events\client\interactionCreate.js:14:27)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
Emitted 'error' event on Client instance at:
at emitUnhandledRejectionOrErr (node:events:384:10)
at processTicksAndRejections (node:internal/process/task_queues:85:21) {
code: 'InteractionAlreadyReplied'
}
this is the error message i get when i do /mute #user
i have no idea what it means please help :)
You're searching for the user ID using interaction.guild.roles.cache instead of searching for members.
Change that to:
const user = interaction.guild.members.cache.get(userId);
A better way of mute can be through the usage of the new timeout feature.
Read more here

discord.js V13 Invite Tracker TypeError: client.guilds.cache.get(...).fetchInvites is not a function

I am trying to code a invite tracker this is the code :
let invites;
client.on('ready', async() => {
await wait(2000);
client.guilds.cache.get("926674245357039657").fetchInvites().then(inv => {
invites = inv;
})
});
client.on('guildMemberAdd', async(member) => {
if(member.guild.id !== "926674245357039657") return;
member.guild.fetchInvites().then(gInvites => {
const invite = gInvites.find((inv) => invites.get(inv.code).uses < inv.uses);
const channel = member.guild.channels.cache.get('940362032253923348');
channel.send(`${member} was invited by ${invite.inviter} and the code was ${invite.code}`);
});
});
when i run the bot i get this error
client.guilds.cache.get("926674245357039657").fetchInvites().then(inv => {
^
TypeError: client.guilds.cache.get(...).fetchInvites is not a function
can anyone help and thx
fetchInvites does not work in Discord v13, use invites.fetch() instead.
https://discordjs.guide/whats-new.html#guild

DiscordJS TypeError: Cannot read properties of undefined (reading 'roles')

I'm trying to set up a command that when mentioning a member gives them a specific role. However every time I get the same roles undefined error. Can anyone help me out?
My code:
client.on('messageCreate', (message) => {
if (message.content == '!brig'){
const role = message.guild.roles.cache.find(role => role.name === 'BRIG')
const target = message.mentions.members.first();
target.roles.add('757816527478325391')
}
})
I made a small modification to your code changing message.content === '!brig' to message.content.includes("!brig") and it seems like it works fine for me. You can try my code and let me know if anything changes:
const { Client } = require("discord.js");
const client = new Client({
intents: ["GUILDS", "GUILD_MESSAGES"],
});
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}`);
});
client.on("messageCreate", (message) => {
if (message.content.includes("!brig")) {
const role = message.guild.roles.cache.find(
(role) => role.name === "BRIG"
);
const target = message.mentions.members.first();
target.roles.add(role.id);
}
});
client.login("YOURTOKEN");
Edit: This code is working with discord.js#13.5.0 but I can't see why it wouldn't work with the latest version (13.6.0 at this moment).

discord.js get message id from interaction message

I want to get message id from interaction message, but i can't get it :|
discord.js verson : ^13.1.0
client.on('interactionCreate',async interaction => {
if(interaction.commandName==='test') {
let message = await interaction.reply({content:'testing...',ephemeral:true});
console.log(message); //undefined
}
});
You can use the CommandInteraction#fetchReply() method to fetch the Message instance of an initial response.
Example:
client.on('interactionCreate', async (interaction) => {
if (interaction.commandName === 'test') {
interaction.reply({
content: 'testing...',
ephemeral: true,
})
const message = await interaction.fetchReply()
console.log(message)
}
})
For latest version of Discord.js:
You can use the fetchReply property of InteractionReplyOptions to fetch the Message object after send it.
let message = await interaction.reply({content:'testing...',ephemeral:true, fetchReply: true});
console.log(message); //Message Object

TypeError: Cannot read properties of null (reading 'id')

So I was updating my bot to discord.js v13 and apparently my logging system has now broke, for some reason it can't read the ID of the guild where this log is occurring.
banAdd.js
const { MessageEmbed} = require("discord.js");
const {red_light} = require("../../other/colors.json");
const Channel = require('../../models/ModerationModel.js');
module.exports = async (bot, guild, user) => {
const guildDB = await Channel.findOne({
guildId: guild.id
}, async (err, guild) => {
if(err) console.error(err)
if (!guild) {
const newGuild = new Channel({
guildId: guild.id,
modChannel: null,
msgChannel: null
});
await newGuild.save().then(result => console.log(result)).catch(err => console.error(err));
}
});
const modChannel = guild.channels.cache.get(guildDB.modChannel);
if (!modChannel) {
return console.log(`No message channel found`);
}
let mEmbed = new MessageEmbed()
.setAuthor(`Member Unbanned`, user.displayAvatarURL({dynamic : true}))
.setColor(red_light)
.setDescription(`${user} ${user.tag}`)
.setThumbnail(`${user.displayAvatarURL({dynamic : true})}`)
.setFooter(`ID: ${user.id}`)
.setTimestamp()
modChannel.send({embeds:[mEmbed]});
}
Error
/home/runner/switch-beta-test/events/guild/banRemove.js:13
guildId: guild.id,
^
TypeError: Cannot read properties of null (reading 'id')
at /home/runner/switch-beta-test/events/guild/banRemove.js:13:27
at /home/runner/switch-beta-test/node_modules/mongoose/lib/model.js:5074:18
at processTicksAndRejections (node:internal/process/task_queues:78:11)
I have no idea why this is not working as it works in previous versions but updating to discord.js V13 completely broke this system. I tried looking at any possible solution but I can't find a single solution.
The cause of this error was because guild can no longer be defined during a users ban or unban, guild and user should be replaced with ban in both the unban and ban logs.
CODE
const { MessageEmbed} = require("discord.js");
const {red_light} = require("../../other/colors.json");
const Channel = require('../../models/ModerationModel.js');
module.exports = async (bot, ban) => {
const guildDB = await Channel.findOne({
guildId: ban.guild.id
}, async (err, guild) => {
if(err) console.error(err)
if (!guild) {
const newGuild = new Channel({
guildId: ban.guild.id,
modChannel: null,
msgChannel: null
});
await newGuild.save().then(result => console.log(result)).catch(err => console.error(err));
}
});
const modChannel = ban.guild.channels.cache.get(guildDB.modChannel);
if (!modChannel) {
return console.log(`No message channel found`);
}
let mEmbed = new MessageEmbed()
.setAuthor(`Member Unbanned`, ban.user.displayAvatarURL({dynamic : true}))
.setColor(red_light)
.setDescription(`${ban.user} ${ban.user.tag}`)
.setThumbnail(`${ban.user.displayAvatarURL({dynamic : true})}`)
.setFooter(`ID: ${ban.user.id}`)
.setTimestamp()
modChannel.send({embeds:[mEmbed]});
}
After this the error should no longer show.
The error says that the guild variable is empty doesn't have a value null and when you do guild.id you're trying to access a property that don't exist
Make sure that the guild variable is assigned to a value
Probably they add some changes to the new version of npm package go check the docs

Resources