1
votes

I am making a discord bot command that acts a form of queuing system.

  1. Announcement gets sent out
  2. The announcement has a field that got the list of the people who have done a command which gave them a role.
  3. That role gives them access to a voice channel and a text channel, however the role will be stripped away from them if they leave the voice channel after a specific amount of time has happened (5 minutes to be exact).

I just want to know if these are possible:

I have the bot send an embed message, that message got a field. In that field can I have it that the bot automatically updates that field with the username of the people who have a certain role?

The role will be given with a command that any user can do and the command can only be used when the embed gets send out.

I only want the MOST recent embed to be updated within a specific channel.

What can be done and what can't be done. Discord.js version 12

1
If it's possible can you please give me a quick summary of how it is done.aymhh
Your question needs to be more focused.Jakye
I updated it now, I can't think of another way to explain what I want.aymhh

1 Answers

0
votes

I believe I have done it. I will give the code, and then show you where everything is.

require('dotenv').config();

const Discord = require('discord.js');
const client = new Discord.Client();

const fs = require("fs");
client.saves = require("./minutesLogs.json");

var embed;
var role;
var member;
var myMessage;
var canBecomeHejjo = false;

function updateEmbed() {
    var channel = client.guilds.cache.get("753227876207165570").channels.cache.get("753227876207165573");
    var withRole = channel.guild.roles.cache.get("753237265454465154").members.map(m => m.user.tag);
    if (withRole[0] !== undefined) {
        embed = new Discord.MessageEmbed()
            .setColor('#8547cc')
            .setTitle("People who have role")
            .setAuthor("A bot")
            .addField('List', withRole, true)
            .setTimestamp()
            .setFooter("The members with this role");
    } else {
        embed = new Discord.MessageEmbed()
            .setColor('#8547cc')
            .setTitle("People who have role")
            .setAuthor("A bot")
            .addField('List', 'No one with role', true)
            .setTimestamp()
            .setFooter("The members with this role");
    }

    myMessage.edit(embed);
}

setInterval(updateEmbed, 4000);

client.on('ready', () => {
    console.log(`Logged in as ${client.user.tag}!`);

    var channel = client.guilds.cache.get("753227876207165570").channels.cache.get("753227876207165573");

    var withRole = channel.guild.roles.cache.get("753237265454465154").members.map(m => m.user.tag);

    console.log(withRole);

    if (withRole[0] !== undefined) {
        embed = new Discord.MessageEmbed()
            .setColor('#8547cc')
            .setTitle("People who have role")
            .setAuthor("A bot")
            .addField('List', withRole, true)
            .setTimestamp()
            .setFooter("The members with this role");
    } else {
        embed = new Discord.MessageEmbed()
            .setColor('#8547cc')
            .setTitle("People who have role")
            .setAuthor("A bot")
            .addField('List', 'No one with role', true)
            .setTimestamp()
            .setFooter("The members with this role");
    }


    channel.send(embed).then(message => {
        myMessage = message;
    });
    console.log(myMessage);
    canBecomeHejjo = true;

    setTimeout(() => {
        canBecomeHejjo = false;
    }, 10000);
});


client.on('voiceStateUpdate', (oldMember, newMember) => {
    console.log(oldMember.channelID);
    if (oldMember.channelID === undefined) {
        console.log("undefined");
        return;
    }
    const oldUserChannel = oldMember.channelID;
    var memberToChange = client.guilds.cache.get("753227876207165570").members.cache.get(oldMember.id);

    if (oldUserChannel === '760146985147433040') {
        var fiveMinutesHappened = client.saves[oldMember.id].fiveminutesPassed;
        if (fiveMinutesHappened) {
            memberToChange.roles.remove(role);
        }
    }
});

client.on('message', msg => {
    console.log(canBecomeHejjo);
    if (msg.content === '!become role' && canBecomeHejjo === true) {
        console.log("in");
        member = msg.guild.members.cache.get(msg.member.id);
        client.saves[member.id] = {
            fiveminutesPassed: false
        }

        fs.writeFile("./minutesLogs.json", JSON.stringify(client.saves, null, 4), err => {
            if (err) throw err;
        });

        channel = msg.channel;
        channel.send("ok");
        role = msg.guild.roles.cache.get("753237265454465154");
        msg.member.roles.add(role);
        setTimeout(() => {
            client.saves[member.id] = {
                fiveminutesPassed: true
            }

            fs.writeFile("./minutesLogs.json", JSON.stringify(client.saves, null, 4), err => {
                if (err) throw err;
            });

            console.log("time passed");
        }, 1000 * 10);
        channel.send("done");
    }
});

client.login(process.env.DISCORD_TOKEN);

Here's what's happening:

First, we define a function, that defines a channel and the list of people with a role. Then, we edit the embed from the message defined a bit later. We also state that we will repeat updateEmbed every 4 seconds. The

client.on('ready', () => {

});

part is the same as the updateEmbed function, with a few extra bits at the end. These extra bits do: 1. Allow people to become HeJjo, and 2. Set it up so that after 10 seconds, people cannot change their role anymore. (ps: The code in here is what will be in the command to allow people to change their role).

The next section fires when someone enters or leaves a voice channel. It checks if it is someone leaving, and if not, returns. If yes, then we check if it is the voice channel restricted for members of our role. If so, we determine if five minutes have passed. If they have, we remove the HeJjo role.

The next bit of code is when someone messages. It only runs the code inside if the message is '!become role' and it isn't too late to use this command. What the code inside does is this:

It writes in the Json that five minutes have not passed, and saves it. It then gives the member the role, and then waits five minutes before writing to the file that five minutes have passed.

Hope this helps!! 😉😉