Skip to content

Commit

Permalink
Improve show info command
Browse files Browse the repository at this point in the history
  • Loading branch information
89Q12 committed Feb 6, 2024
1 parent 308219c commit e5a2a57
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions backend/src/bot/commands/user-info-ui.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Client,
ContextMenuCommandInteraction,
EmbedBuilder,
Message,
userMention,
} from 'discord.js';
import { PrismaService } from 'src/prisma.service';
Expand Down Expand Up @@ -40,7 +41,8 @@ export class UserInfoUiCommand {
});
}
const firstMessageId = guildUser.firstMessageId;
if (!firstMessageId) {
const message = await this._getMessage(firstMessageId, interaction.guildId);
if (!message) {
return interaction.followUp({
content:
'Cannot find the first message for user in the database. \n Please add it manually, using /set-first-message command.',
Expand All @@ -64,7 +66,7 @@ export class UserInfoUiCommand {
.addFields([
{
name: 'Link to introduction message',
value: `[Click here](https://discord.com/channels/${interaction.guildId}/1121822614374060175/${firstMessageId})`,
value: `[Click here](${message.url})`,
},
{
name: 'Number of messages sent',
Expand All @@ -80,4 +82,20 @@ export class UserInfoUiCommand {
ephemeral: true,
});
}
async _getMessage(msgId: string, guildId: string) {
if (!msgId) return null;
let message: Message<true> = null;
await this.client.guilds.fetch(guildId).then(async (guild) => {
(await guild.channels.fetch()).forEach(async (channel) => {
if (channel.isTextBased()) {
try {
message = await channel.messages.fetch(msgId);
} catch {
return;
}
}
});
});
return message;
}
}

0 comments on commit e5a2a57

Please sign in to comment.