gpt4 book ai didi

node.js - Discord 按钮只工作一次 discord.js

转载 作者:行者123 更新时间:2023-12-05 05:55:37 25 4
gpt4 key购买 nike

我已经发布了一个按钮,但它只能使用一次 enter image description here但在那之后当我再次使用它时

enter image description here

还有

如果我使用它显示的deferUpdate 如何回复消息

代码

client.on('ready', () => {
client.user.setActivity('people and managing the server', {
type: 'WATCHING',
});

const channel = client.channels.cache.get('894171605608042496');

const row = new MessageActionRow().addComponents(
new MessageButton()
.setCustomId('openTicket')
.setLabel('Create ticket')
.setEmoji('📩')
.setStyle('SECONDARY')
);

channel
.send({
embeds: [
{
title: 'SGAS Tickets',
color: '#388e3c',
description: 'To create a ticket react with 📩',
},
],
components: [row],
})
.then(() => {
const filter = () => {
return true;
};

const collector = channel.createMessageComponentCollector({
filter,
time: 15 * 1000,
});
collector.on('collect', (i) => {
i.deferUpdate().then(() => {
Ticket.count({}, (err, result) => {
if (err) {
console.log(err);
} else {
const ticketNumber = result + 1;
const ticketString = convertNumber(ticketNumber);
const ticket = new Ticket({
tickedId: ticketString,
});
ticket.save((err) => {
if (err) {
console.log(err);
} else {
const myguild = client.guilds.cache.get('887277806386565150');
if (!myguild) {
console.log('guild not found');
return;
}
const category = myguild.channels.cache.find(
(c) =>
c.id === '887277807279947826' &&
c.type == 'GUILD_CATEGORY'
);
if (!category) {
console.log('category not found');
return;
}
myguild.channels
.create(`Ticket#${ticketString}`, {
type: 'GUILD_TEXT',
})
.then(async (myc) => {
myc.setParent(category).then(() => {
myc.send(
`Hello <@${i.user.id}>, your question will be solved here shortly`
);
i.reply({
content: `Go to <#${myc.id}>, for your question`,
ephemeral: true,
});
});
});
}
});
}
});
});
});
});
});

最佳答案

您正在使用一个组件收集器 - 如您的代码所示,它会在 15 秒后过期。这意味着 15 秒后,您的机器人将停止监听按钮点击。我的建议是使用 interactionCreate 事件来监听该按钮:参见文档 https://discord.js.org/#/docs/main/stable/class/ButtonInteraction

例子:

client.on("interactionCreate", (interaction) => {
if(!interaction.isButton()) return;
if(interaction.customId === "openTicket") {
// your ticket code here
}
});

关于node.js - Discord 按钮只工作一次 discord.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69426687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com