Skip to content

Commit

Permalink
add: &mydata command with some adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
Ciborn committed Aug 21, 2022
1 parent 662b77d commit 469a2c5
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
2 changes: 2 additions & 0 deletions src/commands/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -151,6 +152,7 @@ export default () => {
LewdCommand,
EnabledrpgCommand,
GconfigCommand,
MydataCommand,
SetprofileCommand,
UconfigCommand,
PokeCommand,
Expand Down
56 changes: 56 additions & 0 deletions src/commands/settings/mydata.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 2 additions & 0 deletions src/interfaces/ContextUser.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -11,5 +12,6 @@ export default interface ContextUser {
get username(): string;

getAvatarURL(size?: ImageSize): string;
send(content: string | Embed): Promise<unknown>;
toString(): string;
}
9 changes: 9 additions & 0 deletions src/structures/models/DiscordUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
}
Expand Down
11 changes: 11 additions & 0 deletions src/structures/models/RevoltUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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}>`;
Expand Down
1 change: 1 addition & 0 deletions src/structures/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 469a2c5

Please sign in to comment.