Skip to content

Commit

Permalink
feat: handle discord username changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mmkiir committed May 10, 2023
1 parent 33b9aa7 commit 6af8c2e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion bot/classes/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 17,7 @@ type BasicUser = {
id: string;
avatar: string | null;
username: string;
discriminator: string;
discriminator: string | null;
};

type AuthorizedUserData = BasicUser & { token: string };
Expand Down
6 changes: 4 additions & 2 deletions bot/classes/sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 62,12 @@ export async function authenticateUser(
member.isAuthenticated = true;
member.id = user.id;
member.color = color(user.id) || swatches[Math.floor(Math.random() * swatches.length)];
member.name = user.username "#" user.discriminator;
member.name = `${user.username}${
user.discriminator ? (user.discriminator === "0" ? "" : `#${user.discriminator}`) : ""
}`;
member.avatarUrl = user.avatar
? `https://cdn.discordapp.com/avatars/${user.id}/${user.avatar}.png`
: `https://cdn.discordapp.com/embed/avatars/${ user.discriminator % 5}.png`;
: `https://cdn.discordapp.com/embed/avatars/${parseInt(user.id) % 5}.png`;
}
if (!member) throw new ServerError(401, "Could not authenticate user");
ctx.client.userData = member;
Expand Down
7 changes: 6 additions & 1 deletion bot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 50,12 @@ creator.on("error", (error) => console.error(error));
creator.on("synced", () => console.info("Commands synced!"));
creator.on("commandRun", (command, _, ctx) => {
updateUser(ctx.user);
console.info(`${ctx.user.username}#${ctx.user.discriminator} (${ctx.user.id}) ran command ${command.commandName}`);
const { id, username, discriminator } = ctx.user;
console.info(
`${username}${discriminator ? (discriminator === "0" ? "" : `#${discriminator}`) : ""} (${id}) ran command ${
command.commandName
}`,
);
});
creator.on("commandRegister", (command) => console.info(`Registered command ${command.commandName}`));
creator.on("commandError", (command, error) => console.error(`Command ${command.commandName}:`, error));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 1,21 @@
-- RedefineTables
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_User" (
"id" TEXT NOT NULL PRIMARY KEY,
"hash" TEXT,
"email" TEXT,
"avatar" TEXT,
"username" TEXT NOT NULL,
"discriminator" TEXT,
"accessToken" TEXT,
"refreshToken" TEXT,
"updatedAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO "new_User" ("accessToken", "avatar", "discriminator", "email", "hash", "id", "refreshToken", "updatedAt", "username") SELECT "accessToken", "avatar", "discriminator", "email", "hash", "id", "refreshToken", "updatedAt", "username" FROM "User";
DROP TABLE "User";
ALTER TABLE "new_User" RENAME TO "User";
CREATE UNIQUE INDEX "User_id_key" ON "User"("id");
CREATE UNIQUE INDEX "User_hash_key" ON "User"("hash");
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
PRAGMA foreign_key_check;
PRAGMA foreign_keys=ON;
2 changes: 1 addition & 1 deletion bot/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 13,7 @@ model User {
email String? @unique
avatar String?
username String
discriminator String
discriminator String?
accessToken String? // keep serverside only
refreshToken String? // keep serverside only
ownedSessions Session[] @relation("owner")
Expand Down

0 comments on commit 6af8c2e

Please sign in to comment.