message.guild.createChannel is not a function - javascript

Hello I'm making a ticket bot for my server.
But I'm getting error like message.guild.createChannel is not a function
The code
if (message.content.toLowerCase().startsWith(prefix + `openticket`)) {
const reason = message.content.split(" ").slice(1).join(" ");
if (message.guild.channels.cache.find(c => c === `ticket-${message.author.id}`)) return message.channel.send(`You already opened a ticket.`);
message.guild.createChannel(`ticket-${message.author.id}`, "text").then(c => {
let role2 = message.guild.roles.find("name", "#everyone");
c.overwritePermissions(role, {
SEND_MESSAGES: true,
READ_MESSAGES: true
});
c.overwritePermissions(role2, {
SEND_MESSAGES: false,
READ_MESSAGES: false
});
c.overwritePermissions(message.author, {
SEND_MESSAGES: true,
READ_MESSAGES: true
});
message.channel.send(`:white_check_mark: Your ticket is opened, #${c.name}.`);
const embed = new Discord.RichEmbed()
.setColor(0xCF40FA)
.addField(`Hey ${message.author.username}!`, `Your Ticket is opened.`)
.setTimestamp();
c.send({ embed: embed });
message.delete();
}).catch(console.error);
}
Ä° think the code is too old. I got this code from my friends.

Your code seems to be a mix between discord.js v11 and discord.js v12 version.
v12 introduced Managers.
To create a channel on a guild, you have to use the GuildChannelManager, this is the v12 way to do it (see GuildChannelManager.create method documentation) :
message.guild.channels.create(`ticket-${message.author.id}`, { type: 'text' }).then(c => {
...
});
Useful guide that shows changes between v11 and v12.

Related

Why am I getting: TypeError: channel.updateOverwrite is not a function

I'm following a tutorial on discord.js, making a ticket bot. I have double-checked and I am still getting the same error:
TypeError: channel.updateOverwrite is not a function
I've looked over all the StackOverflow questions that I could find, but none has worked for me. I have also explored a little deeper outside of SO, still no help. Here is my code:
module.exports = {
name: 'ticket',
description: 'Open a ticket!',
async execute(client, message, args, cmd, Discord) {
// creates tickets
let channel = await message.guild.channels.create(
`ticket: ${message.author.tag}`,
{ type: 'text' }
);
await channel.setParent('912495738947260446');
// updates channel perms
channel.updateOverwrite(message.guild.id, {
SEND_MESSAGE: false,
VIEW_CHANNEL: false
});
channel.updateOverwrite(message.author, {
SEND_MESSAGE: true,
VIEW_CHANNEL: true
});
const reactionMessage = await channel.send('Thanks for opening a ticket! A staff member will be with you shortly. While you are here, please tell us why you opened this ticket.');
try {
await reactionMessage.react("šŸ”’");
await reactionMessage.react("šŸ—‘ļø");
} catch(err) {
channel.send('Error sending emojis! Please tell a developer to check the console!');
throw err;
}
const collector = reactionMessage.createReactionCollector((reaction, user) => message.guild.members.cache.find((member) => member.id === user.id).hasPermission('ADMINISTRATOR'), {dispose: true});
collector.on('collect', (reaction, user) => {
switch (reaction.emoji.name) {
case "šŸ”’":
channel.updateOverwrite(message.author, { SEND_MESSAGE: false, VIEW_CHANNEL: false});
channel.setname(`šŸ”’ ${channel.name}`)
break;
case "šŸ—‘ļø":
channel.send('Deleting Channel in 10 seconds!');
setTimeout(() => channel.delete(), 10000);
break;
}
});
}
}
It seems you're using discord.js v13 and trying some old code. In v13 the channel#updateOverwrite() method is removed and while in previous version channel#permissionOverwrites was a collection of overwrites, in v13 it's a PermissionOverwriteManager. It means, you should use its .edit() method to update overwrites:
channel.permissionOverwrites.edit(
message.author,
{ SEND_MESSAGES: false, VIEW_CHANNEL: false },
)

Close ticket system not working... Discord.js

Hello I am having a bit of an issue with my Discord ticketing system and it is making me really confused... It does not work when it creates the channel of the ticketing system and it is really confusing me...
code:
Open ticket command:
const Discord = require('discord.js')
module.exports = {
name: 'ticket',
description: "opens a ticket",
execute(message, args){
const user = message.author.id;
const name = "ticket-" + user;
if(message.guild.channels.cache.find(ch => ch.name == name)){
message.channel.send("You have already opened a ticket!")
}else{
message.guild.channels.create(name).then((chan)=>{
chan.updateOverwrite(message.guild.roles.everyone, {
SEND_MESSAGES: false,
VIEW_CHANNEL: false
})
chan.updateOverwrite(user,{
SEND_MESSAGES: true,
VIEW_CHANNEL: true
})
message.channel.send("I have created a ticket for you! Please go to the channel at the top of the server, Only you will have access to it and it will be called ticket(id)");
chan.send("Support will be here shortly").then((m)=>{
m.pin()
})
})
}
}
}
close ticket command (this is were the error is):
const Discord = require('discord.js');
module.exports = {
name: 'endticket',
description: "ends the ticket",
execute(client, message, args){
if(!message.member.hasPermission("ADMINISTRATOR")) return message.channel.send("Only a moderator can end a ticket!")
if(message.member.hasPermission("ADMINISTRATOR")) message.channnel.delete()
}
}
console error:
TypeError: Cannot read property 'delete' of undefined
I dont understand what is going wrong...
thank you.

How to add permissions to user to channel by command? Discord.js

How to give permissions to a specific channel by command? Sorry, Iā€™m new at discord.js so any help would be appreciated.
const Discord = require('discord.js');
module.exports = {
name: 'addrole',
run: async (bot, message, args) => {
//!addrole #user RoleName
let rMember =
message.guild.member(message.mentions.users.first()) ||
message.guild.members.cache.get(args[0]);
if (!rMember) return message.reply("Couldn't find that user, yo.");
let role = args.join(' ').slice(22);
if (!role) return message.reply('Specify a role!');
let gRole = message.guild.roles.cache.find((r) => r.name === role);
if (!gRole) return message.reply("Couldn't find that role.");
if (rMember.roles.has(gRole.id));
await rMember.addRole(gRole.id);
try {
const oofas = new Discord.MessageEmbed()
.setTitle('something')
.setColor(`#000000`)
.setDescription(`Congrats, you have been given the role ${gRole.name}`);
await rMember.send(oofas);
} catch (e) {
message.channel.send(
`Congrats, you have been given the role ${gRole.name}. We tried to DM `
);
}
},
};
You can use GuildChannel.updateOverwrites() to update the permissions on a channel.
// Update or Create permission overwrites for a message author
message.channel.updateOverwrite(message.author, {
SEND_MESSAGES: false
})
.then(channel => console.log(channel.permissionOverwrites.get(message.author.id)))
.catch(console.error);
(From example in the discord.js docs)
Using this function, you can provide a User or Role Object or ID of which to update permissions (in your case, you can use gRole).
Then, you can list the permissions to update followed by true, to allow, or false, to reject.
Here is a full list of permission flags you can use
This method is outdated and doesn't work on V13+ the new way is doing this:
channel.permissionOverwrites.edit(role, {SEND_MESSAGES: true }
channel.permissionOverwrites.edit(member, {SEND_MESSAGES: true }

Discord.js - How do you log role changes?

I'm setting up server logs on my Discord bot on Discord.js v12.2.0 and I'm currently trying to set up role logging. I've rummaged around on the internet a bit and I can only find solutions for this on older versions of Discord.js, which obviously don't work on v12.2.0. I've set up the guildMemberUpdate event to log nickname changes, but I simply don't know how to do it for roles. It might just be super simple but I'm not sure how I would go about it.
Here's my code so far:
client.on('guildMemberUpdate', (oldMember, newMember) => {
if (!oldMember.nickname && newMember.nickname) {
const membernewnicklog = new Discord.MessageEmbed()
.setAuthor(`${newMember.user.tag}`, `${newMember.user.displayAvatarURL({ format: "png", dynamic: true })}`)
.setDescription(`**${newMember} nickname added**`)
.setFooter(`${newMember.user.username}'s ID: ${newMember.id}`)
.setTimestamp()
.setColor('#ffff00')
.addField("New nickname", newMember.nickname)
client.channels.cache.get('736996028787589201').send(membernewnicklog);
return;
}
if (oldMember.nickname && !newMember.nickname) {
const memberremovenicklog = new Discord.MessageEmbed()
.setAuthor(`${oldMember.user.tag}`, `${oldMember.user.displayAvatarURL({ format: "png", dynamic: true })}`)
.setDescription(`**${oldMember} nickname removed**`)
.setFooter(`${oldMember.user.username}'s ID: ${oldMember.id}`)
.setTimestamp()
.setColor('#f04747')
.addField("Old nickname", oldMember.nickname)
client.channels.cache.get('736996028787589201').send(memberremovenicklog);
return;
}
if (oldMember.nickname && newMember.nickname) {
const memberchangednicklog = new Discord.MessageEmbed()
.setAuthor(`${newMember.user.tag}`, `${newMember.user.displayAvatarURL({ format: "png", dynamic: true })}`)
.setDescription(`**${newMember} nickname changed**`)
.setFooter(`${newMember.user.username}'s ID: ${newMember.id}`)
.setTimestamp()
.setColor('#ff4500')
.addField("Before", oldMember.nickname)
.addField("After", newMember.nickname);
client.channels.cache.get('736996028787589201').send(memberchangednicklog);
return;
}
});
And here's what I'm going for: https://imgur.com/a/FRbTpGQ (an example from another bot)
Any help would be super appreciated. Thanks!
client.on("guildMemberUpdate", (oldMember, newMember) => {
// Old roles Collection is higher in size than the new one. A role has been removed.
if (oldMember.roles.cache.size > newMember.roles.cache.size) {
// Creating an embed message.
const Embed = new discord.MessageEmbed();
Embed.setColor("RED");
Embed.setAuthor(newMember.user.tag, newMember.user.avatarURL());
// Looping through the role and checking which role was removed.
oldMember.roles.cache.forEach(role => {
if (!newMember.roles.cache.has(role.id)) {
Embed.addField("Role Removed", role);
}
});
client.channels.cache.get("ChannelID").send(Embed);
} else if (oldMember.roles.cache.size < newMember.roles.cache.size) {
const Embed = new discord.MessageEmbed();
Embed.setColor("GREEN");
Embed.setAuthor(newMember.user.tag, newMember.user.avatarURL());
// Looping through the role and checking which role was added.
newMember.roles.cache.forEach(role => {
if (!oldMember.roles.cache.has(role.id)) {
Embed.addField("Role Added", role);
}
});
client.channels.cache.get("ChannelID").send(Embed);
}
});

Discord.js: message.guild.channels.forEach is not a function

I'm creating a Discord Bot using Discord.js
I'm creating a mute command but when I want to disable speaking permission for the Mute role for each channel, I get this error:
TypeError: message.guild.channels.forEach is not a function
I have V12.
And I looked at some other options but I couldn't find any working options.
if(!toMute) return message.reply('It looks like you didnt specify the user!');
if(toMute.hasPermission('MANAGE_MESSAGES')) return message.reply("can't mute them");
let muterole = message.guild.roles.cache.find(r => r.name === 'muted');
if(!muterole){
try{
muterole = await message.guild.roles.create({
name: "muted",
color: "#000000",
permissions: []
})
message.guild.channels.forEach(async (channel, id) => {
await channel.overwritePermission(muterole, {
SEND_MESSAGES: false,
ADD_REACTIONS: false
});
});
}catch(e){
console.log(e.stack);
}
} return message.channel.send('Cant')
let mutetime = args[1];
if(!mutetime) return message.reply('You didnt specify the time');
await(toMute.addRole(muterole.id));
message.reply(`Successfully muted <#${toMute.id}> for ${ms(mutetime)}`);
setTimeout(function(){
toMute.removeRole(muterole.id);
message.channel.send(`<#${toMute.id}> has been unmuted!`);
}, ms(mutetime));
}
Please try
message.guild.channels.cache.forEach((channel)=>{
...
})
Reference: https://discord.js.org/#/docs/main/stable/class/GuildChannelManager?scrollTo=cache
It's like the error says. message.guild.channels.forEach is not a function!
You're probably using discord.js v12.
In this version, message.guild.channels doesn't return a collection, it returns the ChannelManager. To get a collection of all channels, you use message.guild.channels.cache.
And now you can use .forEach():
message.guild.channels.cache.forEach((channel) => {
// your code here
});

Categories