Currently, I am working on developing a horse race command for my discord bot using TypeScript. The code is functioning properly; however, there is an issue with updating an embed that displays the race and the participants. To ensure the update works correctly, I have to set its description every time the collector.on("collect")
event occurs. I would like to inquire if there is a more efficient and cleaner method to handle this. Thank you!
code:
const bet = interaction.options.get("bet").value;
class Horse {
name: string;
owner: User;
speed: number;
position: Array<string>;
constructor(name: string) {
this.name = name;
this.owner = null;
this.speed = 0;
this.position = ["🐴"];
}
}
const horses = [
new Horse(aimless.pick(names, { remove: true })),
new Horse(aimless.pick(names, { remove: true })),
new Horse(aimless.pick(names, { remove: true })),
new Horse(aimless.pick(names, { remove: false })),
];
const hasJoined: Array<Horse["owner"]> = [];
const row = new MessageActionRow();
for (const horse of horses) {
row.addComponents(
new MessageButton()
.setCustomId(horse.name)
.setLabel(horse.name)
.setStyle("SECONDARY")
);
}
const embed = new MessageEmbed()
.setTitle("Place your bets!")
.setDescription(
`**${horses[0].name} - ${
horses[0].owner !== null ? horses[0].owner.username : "Nobody"
}
${horses[0].position}
${horses[1].name} - ${
horses[1].owner !== null ? horses[1].owner.username : "Nobody"
}
${horses[1].position}
${horses[2].name} - ${
horses[2].owner !== null ? horses[2].owner.username : "Nobody"
}
${horses[2].position}
${horses[3].name} - ${
horses[3].owner !== null ? horses[3].owner.username : "Nobody"
}
${horses[3].position}**`
);
await interaction.editReply({
embeds: [embed],
components: [row],
});
const filter = async (i: MessageComponentInteraction) => {
let profile: any;
try {
profile = await profileModel.findOne({ userID: i.user.id });
if (!profile) {
await profileModel.create({
userID: i.user.id,
serverID: i.guild?.id,
username: i.user.username,
bananas: 100,
deposit: 0,
});
profile.save();
}
} catch (e) {
await i.editReply("Something went wrong! :( Please retry.");
} finally {
if (hasJoined.includes(i.user)) {
return false;
}
if (profile.bananas < bet) {
interaction.editReply(`${i.user} you don't have enough bananas!`);
}
return profile.bananas >= bet;
}
};
const collector = interaction.channel.createMessageComponentCollector({
filter,
time: 60000,
});
collector.on("collect", async (int) => {
await int.deferUpdate();
for (const btn of row.components) {
if (btn.customId === (int.component as MessageButton).customId) {
(btn as MessageButton).setDisabled(true).setStyle("SUCCESS");
hasJoined.push(int.user);
horses.find((h) => h.name === btn.customId).owner = int.user;
console.log(horses);
}
}
embed.setDescription(
`**${horses[0].name} - ${
horses[0].owner !== null ? horses[0].owner.username : "Nobody"
}
${horses[0].position}
${horses[1].name} - ${
horses[1].owner !== null ? horses[1].owner.username : "Nobody"
}
${horses[1].position}
${horses[2].name} - ${
horses[2].owner !== null ? horses[2].owner.username : "Nobody"
}
${horses[2].position}
${horses[3].name} - ${
horses[3].owner !== null ? horses[3].owner.username : "Nobody"
}
${horses[3].position}**`
);
await int.editReply({
embeds: [embed],
components: [row],
});
});
},
});`