From 469a2c5379628d65a099aa0a9b6efb2df67745dc Mon Sep 17 00:00:00 2001 From: Robin Bourachot Date: Sun, 21 Aug 2022 14:03:00 +0200 Subject: [PATCH] add: &mydata command with some adjustments --- package.json | 2 +- src/commands/commands.ts | 2 + src/commands/settings/mydata.ts | 56 ++++++++++++++++++++++++++++ src/interfaces/ContextUser.ts | 2 + src/structures/models/DiscordUser.ts | 9 +++++ src/structures/models/RevoltUser.ts | 11 ++++++ src/structures/models/User.ts | 1 + 7 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 src/commands/settings/mydata.ts diff --git a/package.json b/package.json index ccc60327..b9c82c37 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "typescript": "^4.7.4" }, "scripts": { - "build": "npx tsc --build", + "build": "npx tsc", "dev": "node --require dotenv/config dist/src/app.js dev !", "start": "node --require dotenv/config dist/src/app.js" }, diff --git a/src/commands/commands.ts b/src/commands/commands.ts index 602281a5..a730b54e 100644 --- a/src/commands/commands.ts +++ b/src/commands/commands.ts @@ -70,6 +70,7 @@ import OsurecentCommand from "./osu!/osurecent.js"; // Settings import EnabledrpgCommand from "./settings/enabledrpg.js"; import GconfigCommand from "./settings/gconfig.js"; +import MydataCommand from "./settings/mydata.js"; import SetprofileCommand from "./settings/setprofile.js"; import UconfigCommand from "./settings/uconfig.js"; @@ -151,6 +152,7 @@ export default () => { LewdCommand, EnabledrpgCommand, GconfigCommand, + MydataCommand, SetprofileCommand, UconfigCommand, PokeCommand, diff --git a/src/commands/settings/mydata.ts b/src/commands/settings/mydata.ts new file mode 100644 index 00000000..717b3bc0 --- /dev/null +++ b/src/commands/settings/mydata.ts @@ -0,0 +1,56 @@ +import SettingsCommand from "../../groups/SettingsCommand.js"; +import MessageContext from "../../structures/contexts/MessageContext.js"; +import Embed from "../../structures/Embed.js"; +import DiscordUser from "../../structures/models/DiscordUser.js"; +import RevoltUser from "../../structures/models/RevoltUser.js"; +import User from "../../structures/models/User.js"; + +function associatedAccount(u: DiscordUser | RevoltUser) { + const id = u instanceof DiscordUser ? u.snowflake : u.id; + return { + id, + createdAt: u.getDataValue("createdAt"), + updatedAt: u.getDataValue("updatedAt") + }; +} + +export default class MydataCommand extends SettingsCommand { + constructor() { + super({ + description: "Displays your Aldebaran user metadata and more" + }); + } + + async run(ctx: MessageContext) { + const userId = ctx.author.base.id; + const discordAccs = await DiscordUser.findAll({ where: { userId } }); + const revoltAccs = await RevoltUser.findAll({ where: { userId } }); + + let associated = ""; + discordAccs.forEach(user => { + associated += `**Discord** (\`${user.snowflake}\`)\n`; + }); + revoltAccs.forEach(user => { + associated += `**Revolt** (\`${user.id}\`)\n`; + }); + + const user = await User.findByPk(userId, { include: { all: true } }) as User; + const json = JSON.stringify({ + id: userId, + permissions: user.permissions, + createdAt: user.getDataValue("createdAt"), + updatedAt: user.getDataValue("updatedAt"), + profile: user.profile, + settings: user.settings, + associatedDiscordAccounts: discordAccs.map(associatedAccount), + associatedRevoltAccounts: revoltAccs.map(associatedAccount) + }, null, 4); + ctx.author.send(`Here is your data.\n\`\`\`json\n${json}\n\`\`\``); + + const embed = new Embed() + .setTitle("Aldebaran User Metadata") + .setDescription(`*Your data has been sent into your DMs for privacy reasons.*\n**User ID**: \`${userId}\``) + .addField("Associated accounts", associated); + ctx.reply(embed); + } +} diff --git a/src/interfaces/ContextUser.ts b/src/interfaces/ContextUser.ts index cdcfbb51..236a53a2 100644 --- a/src/interfaces/ContextUser.ts +++ b/src/interfaces/ContextUser.ts @@ -1,4 +1,5 @@ import { ImageSize } from "@discordjs/rest"; +import Embed from "../structures/Embed.js"; import User from "../structures/models/User.js"; export default interface ContextUser { @@ -11,5 +12,6 @@ export default interface ContextUser { get username(): string; getAvatarURL(size?: ImageSize): string; + send(content: string | Embed): Promise; toString(): string; } diff --git a/src/structures/models/DiscordUser.ts b/src/structures/models/DiscordUser.ts index ec9329ae..80193c5e 100644 --- a/src/structures/models/DiscordUser.ts +++ b/src/structures/models/DiscordUser.ts @@ -3,6 +3,7 @@ import { User as DjsUser } from "discord.js"; import { DataTypes, Model } from "sequelize"; import ContextUser from "../../interfaces/ContextUser.js"; import { tableConf } from "../../utils/Methods.js"; +import Embed from "../Embed.js"; import User from "./User.js"; export default class DiscordUser extends Model implements ContextUser { @@ -43,6 +44,14 @@ export default class DiscordUser extends Model implements ContextUser { return this.user.displayAvatarURL({ size }); } + public async send(content: string | Embed) { + if (content instanceof Embed) { + return this.user.send({ embeds: [content.toDiscordEmbed()] }); + } else { + return this.user.send(content); + } + } + public toString() { return this.user.toString(); } diff --git a/src/structures/models/RevoltUser.ts b/src/structures/models/RevoltUser.ts index 375bc860..97d75fa8 100644 --- a/src/structures/models/RevoltUser.ts +++ b/src/structures/models/RevoltUser.ts @@ -2,6 +2,7 @@ import { User as RjsUser } from "revolt.js"; import { DataTypes, Model } from "sequelize"; import ContextUser from "../../interfaces/ContextUser.js"; import { tableConf } from "../../utils/Methods.js"; +import Embed from "../Embed.js"; import User from "./User.js"; export default class RevoltUser extends Model implements ContextUser { @@ -32,6 +33,16 @@ export default class RevoltUser extends Model implements ContextUser { ? `https://autumn.revolt.chat/avatars/${this.user.avatar._id}` : this.user.defaultAvatarURL; } + + public async send(content: string | Embed) { + const channel = await this.user.openDM(); + if (content instanceof Embed) { + const embed = await content.toRevoltEmbed(); + return channel.sendMessage({ embeds: [embed] }); + } else { + return channel.sendMessage(content); + } + } public toString() { return `<@${this.user._id}>`; diff --git a/src/structures/models/User.ts b/src/structures/models/User.ts index 35b7eef4..564c8a01 100644 --- a/src/structures/models/User.ts +++ b/src/structures/models/User.ts @@ -6,6 +6,7 @@ import Profile from "./Profile.js"; import UserSetting from "./UserSetting.js"; class User extends Model { + declare public profile?: Profile; // Sequelize inclusion declare public settings: UserSetting[]; // Sequelize inclusion declare public id: number; declare public permissions: AldebaranPermissions;