• Home
  • Jobs
  • Courses
  • Questions
  • Teachers
  • For business
  • ES/EN

0

25
Views
How to create a channel and send a message to it when user logined to server?

How add channel and send message to it by discord bot when user login? Message should include hyper link button.

const Discord = require("discord.js");
const config = require("./config.json");
const { MessageActionRow, MessageButton } = require("discord.js");
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] });
const prefix = "!";
client.on("messageCreate", function (message) {
    let guild = message.guild;
    if (message.author.bot) return;
    if (!message.content.startsWith(prefix)) return;

    const commandBody = message.content.slice(prefix.length);
    const args = commandBody.split(" ");
    const command = args.shift().toLowerCase();

    if (command === "ping") {
        const timeTaken = Date.now() - message.createdTimestamp;
        message.reply(`Pong! This message had a latency of ${timeTaken}ms.`);
    } else if (command === "sum") {
        const numArgs = args.map((x) => parseFloat(x));
        const sum = numArgs.reduce((counter, x) => (counter += x));
        message.reply(`The sum of all the arguments you provided is ${sum}!`);
    } else if (command === "channel") {
        // Create a new text channel
        guild.channels
            .create("nft-checking", { reason: "Needed a cool new channel" })
            .then(console.log)
            .catch(console.error);
    }
});

// client.on("ready", (client) => {
//     client.channels.get("938800178314485823").send("Hello here!");
// });
client.login(config.BOT_TOKEN);
console.log("Discord server is running.");

Please check this and let me know correct code.

about 1 month ago ·

Juan Pablo Isaza

1 answers
Answer question

0

You would send a message to the channel as you do with any other channel. To get your new channel you would access it in the .then() after you created it. Here's an example:

guild.channels
    .create("nft-checking", { reason: "Needed a cool new channel" })
    .then(channel => {
         channel.send('I am a new channel');
    }).catch(console.error);

In order to run this code when a user joins, you would use the guildMemberAdd event.

For example:

client.on('guildMemberAdd', (member) => {
    const guild = member.guild;
    
    guild.channels
        .create("nft-checking", { reason: "Needed a cool new channel" })
        .then(channel => {
             channel.send('I am a new channel');
        }).catch(console.error);
});

If you would like to make the channel restricted to the new user with permissions you can check the documentation here and look at the options available.

about 1 month ago · Juan Pablo Isaza Report
Answer question
Find remote jobs
Loading

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2022 PeakU Inc. All Rights Reserved.