需要帮助使用自定义编码的不和谐考勤机器人修改嵌入字段

问题描述 投票:0回答:1

我正在编写一个自定义考勤机器人,该机器人可以组织那些勾选“是”的人将他们的名字添加到各自的团队中。我现在有两个问题:

目前,它已按预期将我自己添加到我的团队中。但是,它只是将当前团队的值设置为我的名字,这意味着如果来自同一团队的任何人签入,它会将我的名字替换为他们的名字。我想让它在我的名字下添加他们的名字,这样来自同一团队的多个人就可以签入。

我遇到的第二个问题是按钮系统仅适用于特定的通道 ID(请参阅下面的行) const Channel = client.channels.cache.get('channelID');

我想更改此设置,以便它只需获取嵌入/按钮当前所在频道的 ID,这样就可以同时针对不同的事件进行多个签入。

嵌入/当前按钮系统的所有代码如下。

如果这些只是简单的修复,我深表歉意,我对 JS 的了解不是最好的,而且我无法在线/使用 ChatGPT 找到这些问题的明确答案。

const customEmojiTeam1 = '<:Team1:team1ID>';
const customEmojiTeam2 = '<:Team2:team2ID>';
const customEmojiTeam3 = '<:Team3:team3ID>';
const customEmojiAvailable = '<:yes:availableID>';
const customEmojiUnavailable = '<:no:unavailableID>';
const customEmojiReserve = '<:Reserve:reserveID>';

client.on('interactionCreate', (interaction) => {
    if (!interaction.isChatInputCommand()) return;

    if (interaction.commandName === 'embed') {

        const embed = new EmbedBuilder()

            .setTitle("Generic Title")
            .setDescription('Event 1 - timestamp)
            .setColor(0xffffff)
            .addFields({ 
                name: `${customEmojiTeam1} Team1`,
                value: '-',
            }, {
                name: `${customEmojiTeam2} Team2`,
                value: '-',
            }, {
                name: `${customEmojiTeam3} Team3`,
                value: '-',
            }, {
                name: `${customEmojiUnavailable} Unavailable`,
                value: '-',
            }, {
                name: `${customEmojiReserve} Reserves`,
                value: '-',
        });

        const yesButton = new ButtonBuilder()
            .setStyle(ButtonStyle.Secondary)
            .setEmoji(customEmojiAvailable)
            .setCustomId('yes-button');

        const noButton = new ButtonBuilder()
            .setEmoji(customEmojiUnavailable)
            .setStyle(ButtonStyle.Secondary)
            .setCustomId('no-button');

        const buttonRow = new ActionRowBuilder().addComponents(yesButton, noButton);

        interaction.reply({ content: 'Your check-in has been created. If you encounter any issues, please message @user.', ephemeral: true});
        interaction.channel.send({ content: `<@role1ID> <@role2ID>`, embeds: [embed], components: [buttonRow] });
    }
});

client.on('interactionCreate', async (interaction) => {
    if (!interaction.isButton()) return;

    const member = interaction.guild.members.cache.get(interaction.user.id);
    const team1Role = interaction.guild.roles.cache.find(role => role.name === 'Team 1');
    const team2Role = interaction.guild.roles.cache.find(role => role.name === 'Team 2');
    const team3Role = interaction.guild.roles.cache.find(role => role.name === 'Team 3');
    const reserveRole = interaction.guild.roles.cache.find(role => role.name === 'Reserve');

    if (interaction.customId === 'yes-button') {
        if (member.roles.cache.has(team1Role.id)) {
            const team1Value = `- ${member.displayName}`;
            // Update the Team 2 field in the embed
            await updateEmbedField('Team 1', team1Value);
        } else if (member.roles.cache.has(team2Role.id)) {
            const team2Value = `- ${member.displayName}`;
            // Update the Team 2 field in the embed
            await updateEmbedField('Team 2', team2Value);
        }
        else if (member.roles.cache.has(team3Role.id)) {
            const team3Value = `- ${member.displayName}`;
            // Update the Team 3 field in the embed
            await updateEmbedField('Team 3', team3Value);
        }
        else if (member.roles.cache.has(reserveRole.id)) {
            const reserveValue = `- ${member.displayName}`;
            // Update the Reserves field in the embed
            await updateEmbedField('Reserves', reserveValue);
        }
});

async function updateEmbedField(roleName, value) {
    const channel = client.channels.cache.get('channelID');
    const messages = await channel.messages.fetch({ limit: 1 });
    const embedMessage = messages.first();

    // Find the field with the specified role name and update its value
    const field = embedMessage.embeds[0].fields.find(f => f.name.includes(roleName));
    if (field) {
        field.value = value;
    }

    // Update the embed message
    await embedMessage.edit({ embeds: [embedMessage.embeds[0]] });
}

如果这些只是简单的修复,我深表歉意,我对 JS 的了解不是最好的,而且我无法在线/使用 ChatGPT 找到这些问题的明确答案。

discord discord.js
1个回答
0
投票

首先:

您在创建嵌入时出现语法错误。您忘记了结束引号

.setDescription('Event 1 - timestamp)

应该是

.setDescription('Event 1 - timestamp')


问题1:

要添加新参与者而不是将值重置为单击它的人,您需要通过添加一些字符串连接来更改分配给

field.value
的值,如下所示:

async function updateEmbedField(channel, roleName, value) {
    const messages = await channel.messages.fetch({ limit: 1 });
    const embedMessage = messages.first();
                
    // Find the field with the specified role name and update its value
    const field = embedMessage.embeds[0].fields.find(f => f.name.includes(roleName));

    if (field) {
        field.value = field.value === "-" ? `${value}` : `${field.value}\n${value}`;
    }
                
    // Update the embed message
    await embedMessage.edit({ embeds: [embedMessage.embeds[0]] });
}

我们还修改了

updateEmbedField()
,添加了通道参数,以便您可以获取嵌入按钮的通道。


问题2:

您可以使用在

interactionCreate
事件处理程序上传递的交互数据来获取单击按钮的通道。

提示:

  • 您可以将它们合并为一个,以获得更高效、更简洁的代码,而不是为同一个
    interactionCreate
    事件使用两个事件侦听器。
client.on("interactionCreate", async (interaction) => {
    if (interaction.isChatInputCommand()) {
        // Do your chat input command logic here
    }
    else if (interaction.isButton()) {
        // For your second question, you can grab the channel/channel Id of the channel like this
        let channel = interaction.channel;     // The channel itself
        let channelId = interaction.channelId; // The channel Id
        let member = interaction.member;

        // When you call your updateEmbedValue(), pass the channel as the first argument
        // updateEmbedValue(channel, "Team1", member.displayName);
        // Do your on button interaction logic here
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.