How do you mass create channels on Discord.js - javascript

Hello I am making a bot that will basically make a whole discord server and I just can't figure out how to mass create channels so could someone tell me, please

const Channels = ["Example Channel", "Example Channel 2"]; // Example of channel names.
const Guild = await client.guilds.create("My Guild"); // Creating the guild.
Channels.forEach(channelName => { // Looping through Channels array to get the channels names.
Guild.channels.create(channelName); // Creating the channels.
});
const GuildChannel = Guild.channels.cache.filter(c => c.type === "text").find(x => x.position == 0); // Getting the first channel in the guild.
const Invite = await GuildChannel.createInvite({maxAge: 0, maxUses: 1}); // Creating and invite.
message.channel.send(`I created the guild ${Guild.name}. Invite Link: ${Invite.url}`); // Sending the invite link to the current channel.
// Warning: client.guilds.create() is only available to bots in fewer than 10 guilds.guilds!

Related

Create invite from server 1 and send to user DMS in server 2

So I am trying to create a type of command that will generate 1 single use in server 1. Now if I were to input this command into a general text in server 2, !dminvite #user, it would send the mentioned user the single created invite from server 1 and send it to the mentioned user in server 2, via DMs.
Some Notes:
1. I have the 2 server id's saved in my config.json file and this invite command requires config.json. In this commands file base
2. This command is running over discord.js v12, but if anyone wants to help out in discord.js v13, It would be amazing as well.
3. I am no way in trying to advertise anything, I own both servers
I have a code written down but it's not working, just a view point:
message.delete();
message.mentions.members.first() ||
message.guild.members.cache.get(args[0]);
const Options = {
temporary: false,
maxAge: 0,
maxUses: 1,
unique: true
};
const channel = message.guild.channels.cache.get('(Channel ID)');
let invite = message.channels.createInvite(Options).then(function(Invite) {
message.mentions.members.first().send({embed: {
title: `**INVITE**`,
description: `Invite:\nhttps://discord.gg/` + Invite.code
}});
message.channel.send(new Discord.MessageEmbed()
.setAuthor(message.author.tag, message.author.displayAvatarURL({ dynamic: true }))
.setDescription(`${message.author.tag}, invite was sent to DMs`)
.setColor('BLUE')
);
});
To accomplish your goal we do this:
Get the mentioned user from the message.
Fetch the other guild we want to invite the user to.
Create an invite to the other guild's system channel (it doesn't have to be systemChannel).
Send the invite to the mentioned user's DMs.
// Id of the other guild
const otherGuildId = "guild id";
client.on("message", async (message) => {
// Don't reply to itself
if (message.author.id == client.user.id) return;
// Check if the message starts with our command
if (message.content.startsWith("!dminvite")) {
// Get the mentioned user from the message
const user = message.mentions.users.first();
if (!user) {
message.channel.send("You need to mention the user to send the invitation to.");
return;
}
// Fetch the other guild we want to invite the user to
const otherGuild = await client.guilds.fetch(otherGuildId);
// Create invite to the other guild's system channel
const invite = await otherGuild.systemChannel.createInvite({
maxAge: 0,
maxUses: 1,
unique: true
});
// Send the invite to the mentioned user's DMs
user.send({
embed: {
title: "**INVITE**",
description: `${invite}`
}
});
}
});
Note that if you don't want to use the systemChannel, you can simply get any channel you want from the otherGuild by its id for example.
// Resolve channel id into a Channel object
const channel = otherGuild.channels.resolve("channel id");
Using discord.js ^12.5.3.

Get count of member's messages in channel in discord.js

Is there any way how to count messages of specified user in specified Discord channel in discord.js? When I use:
const countMyMessages = async (channel, member) => {
const messages = await channel.messages.fetch()
const myMessages = message.filter(m => m.author.id === member.id)
console.log(myMessages.size)
}
Only 50 messages are fetched, so I can't count all messages of user. And option limit can have max value 100. /guilds/guild_id/messages/search API on the other hand is not available for bots.
You will need to use a storage system to keep this kind of statistics on Discord.
I recommend you to use SQLite at first (like Enmap npm package).
I can quickly draw a structure for you based on this one.
const Enmap = require("enmap");
client.messages = new Enmap("messages");
client.on("message", message => {
if (message.author.bot) return;
if (message.guild) {
const key = `${message.guild.id}-${message.author.id}`;
client.messages.ensure(key, {
user: message.author.id,
guild: message.guild.id,
messages: 0
});
client.messages.inc(key, "messages");
// Do your stuff here.
console.log(client.messages.get(key, "messages"))
}
});

Show users connected to voice channel?

I want to know is it possible to know if any member is connected to a any voice channel in discord.js v12.2.0. Please let me know if you have any clues on it.
Use VoiceChannel.members
const vc = <message>.guild.channels.cache.get('VC Id')
const members = vc.members //COLLECTION
To check if a member is in vc, use GuildMember.voice
const vc = <member>.voice.channel //VOICE CHANNEL
//if you want, you can check the vc name, id, etc with vc.name, vc.id, etc
EDIT
Here is an example for what you said in the comments
//MAKE SURE IT IS ASYNC CALLBACK
await client.guilds.fetch();
const VCs = [];
client.guilds.cache.forEach(async guild => {
await guild.channels.fetch();
let VCs = guild.channels.cache.filter(c => c.type === 'voice');
VCs.forEach(vc => {
if(vc.members) {
VCs.push(vc)
}
})
})
I hope this is what you wanted (VCs is an array with all VCs with members)

I am trying to make a guild add message

I am trying to make it so when my bot is added to another server, it will send an embed saying how many servers it's in now and the guild name and also the guild owner. I am also trying to make another embed so it tells me when it leaves a server and tells me when it joined the server first and then when it was removed and the guild name and guild owner. I use discord.js. Could someone help, please? This is my current script:
bot.on("guildCreate", guild => {
const joinserverembed = new Discord.MessageEmbed()
.setTitle("Joined a server!")
.addField("Guild name:", `${guild.name}`)
.addField("Time of join:", `${Discord.Guild.createdTimestamp()}`)
.setColor("GREEN")
.setThumbnail(guild.displayAvatarURL())
if (guilds.channel.id = 740121026683207760) {
channel.send(joinserverembed)
}
guild.channel.send("Thank you for inviting Ultra Bot Premium! Please use up!introduction and up!help for the new perks and more!")
})
bot.on("guildDelete", guild => {
const leftserverembed = new Discord.MessageEmbed()
.setTitle("Left a server!")
.addField("Guild name:", `${guild.name}`)
.addField("Time of removal:", `${createdTimestamp()}`)
.setColor("RED")
.setThumbnail(guild.displayAvatarURL())
if (guilds.channel.id = 740121026683207760) {
channel.send(leftserverembed)
}
})
I have resolved your first issue for you in the code below.
You were doing guild.channel.send(), in this case, guild represents a Discord.Guild however you're using it like it represents an instance of Message, which it does not.
You can use guild.channels.cache.find(x => x.name == 'general').send("Thanks for inviting me to this serverĀ¬!") will send a message to a channel named general in that server.
bot.on("guildCreate", (guild) => {
const joinserverembed = new Discord.MessageEmbed()
.setTitle("Joined a server!")
.addField("Guild name:", guild.name)
.addField("Time of join:", Date.now())
.setColor("GREEN")
.setThumbnail(guild.iconURL({ dynamic: true }));
bot.channels.cache.get("740121026683207760").send(joinserverembed);
guild.channels.cache
.filter((c) => c.type === "text")
.random()
.send(
"Thank you for inviting Ultra Bot Premium! Please use up!introduction and up!help for the new perks and more!"
);
});
I filter the channels in the guild, ensuring that they are not categories or voice channels, then send the welcome message to a random one.
As for your second query, you need to use a database, store the Date.now timestamp of when it was added, then once the bot has left the guild it must get the value and display its time. I haven't done this for you, but I have fixed your code:
bot.on("guildDelete", (guild) => {
const leftserverembed = new Discord.MessageEmbed()
.setTitle("Left a server!")
.addField("Guild name:", guild.name)
.addField("Time of removal:", Date.now())
.setColor("RED")
.setThumbnail(guild.iconURL({ dynamic: true }));
bot.channels.cache.get("740121026683207760").send(leftserverembed);
});

How do I fetch the creator of a channel in discord?

bot.on('channelCreate', async channel => {
if (!channel.guild) return;
const fetchedLogs = await channel.guild.fetchAuditLogs({
limit: 1,
type: 'CHANNEL_CREATE',
});
const logbook = channel.guild.channels.cache.get("ChannelID")
const deletionLog = fetchedLogs.entries.first();
if (!deletionLog) return logbook.send(`A channel was updated but no relevant autid logs were found`);
const { executor, user } = deletionLog;
if (user.id) {
logbook.send(`${executor.tag} created a channel`);
} else {
logbook.send(`A channel was created but idk who did.`);
}
});
I am a newbie when it comes to fetching actions through Discord Audit Logs; so I am experimenting and somehow came up with this code. However, when I create a channel, it does not send any messages saying that a channel has been created by #user. I have no idea what my next step will be. All I wanted to do was to know who created the channel.
Discord.JS: v12.2.0
client.on("channelCreate", async channel => {
if (!channel.guild) return false; // This is a DM channel.
const AuditLogFetch = await channel.guild.fetchAuditLogs({limit: 1, type: "CHANNEL_CREATE"}); // Fetching the audot logs.
const LogChannel = client.channels.cache.get("722052103060848664"); // Getting the loggin channel. (Make sure its a TextChannel)
if (!LogChannel) return console.error(`Invalid channel.`); // Checking if the channel exists.
if (!AuditLogFetch.entries.first()) return console.error(`No entries found.`);
const Entry = AuditLogFetch.entries.first(); // Getting the first entry of AuditLogs that was found.
LogChannel.send(`${Entry.executor.tag || "Someone"} created a new channel. | ${channel}`) // Sending the message to the logging channel.
});
If the code I provided is not working, please make sure the bot has access to view AuditLogs.

Categories

Resources