Skip to content

Commit

Permalink
compartible with discord's new username system
Browse files Browse the repository at this point in the history
  • Loading branch information
raluvy95 committed Jun 14, 2023
1 parent 68244e6 commit aa1bd38
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 21 deletions.
3 changes: 2 additions & 1 deletion src/client/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { MovCommand } from './Command';
import { MovPlugin } from './Plugin';
import { Collection } from '@discordjs/collection';
import { ISettingsDB, IUserDB } from '../interfaces/database';
import { parseName } from '../utils/get';

export interface ClientDatabase {
level: MovDB
Expand Down Expand Up @@ -229,7 +230,7 @@ class Mov extends CommandClient {
}

this.on("ready", () => {
console.log(`Logged as ${this.user.username}#${this.user.discriminator}!`);
console.log(`Logged as ${parseName(this.user)}!`);
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/commands/level/removeuserxp.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Message, TextChannel } from "eris";
import { client } from "../../client/Client";
import { MovCommand } from "../../client/Command";
import { getUser } from "../../utils/get";
import { getUser, parseName } from "../../utils/get";
import { MessageCollector } from "eris-collect";
async function generator(msg: Message, args: string[]) {
if (!isNaN(Number(args[0]))) {
Expand Down Expand Up @@ -33,7 +33,7 @@ async function generator(msg: Message, args: string[]) {
});
client.createMessage(
msg.channel.id,
`Are you sure you want to remove **${user.username}#${user.discriminator}**'s level? [Type 'yes' or 'y' to confirm]`,
`Are you sure you want to remove **${parseName(user)}**'s level? [Type 'yes' or 'y' to confirm]`,
);
collector.on("end", (c) => {
const msg = c[0];
Expand Down
11 changes: 5 additions & 6 deletions src/utils/canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Message, User } from "eris";
import { ILevelDB, IUserDB } from "../interfaces/database";
import { client } from "../client/Client";
import { getLeaderboardRank } from "./getLeaderboardRank";
import { getUserByID } from "./get";
import { getUserByID, parseName } from "./get";
import { DEFAULT_USER_SETTINGS } from "../constant/defaultConfig";

registerFont("./assets/Roboto-Bold.ttf", {
Expand Down Expand Up @@ -177,7 +177,7 @@ export async function genXPRank(user: User, level: ILevelDB): Promise<Buffer> {

ctx.globalAlpha = 1;

const name = `${user.username}#${user.discriminator}`;
const name = parseName(user);

await genAvatar(ctx, user, 40, canvas.height / 2 - 60, true);

Expand Down Expand Up @@ -254,7 +254,7 @@ async function leaderboardContent(
ctx.closePath();

const u = await getUserByID(entry.id, true);
const name = u ? `${u.username}#${u.discriminator}` : entry.id;
const name = u ? parseName(u) : entry.id;

ctx.fillStyle = labels.text.mocha.hex;
ctx.font = "16px 'RobotoB'";
Expand Down Expand Up @@ -352,7 +352,7 @@ export async function leaderboardCanvas(

ctx.globalCompositeOperation = "difference";
ctx.fillStyle = "#ffffff";
const name = `${msg.author.username}#${msg.author.discriminator}`;
const name = parseName(msg.author);
let size = 16;
let nameN = ctx.measureText(name);
while (nameN.width > 200) {
Expand All @@ -368,8 +368,7 @@ export async function leaderboardCanvas(

ctx.fillStyle = "#ffffff";
ctx.fillText(
`Level ${
yourRank.data.level
`Level ${yourRank.data.level
} | Total XP: ${yourRank.data.totalxp.toLocaleString()}`,
280,
positionI,
Expand Down
9 changes: 9 additions & 0 deletions src/utils/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ export async function getUserByID(id: string, cacheOnly?: boolean) {
}
}

// Dynamically use discriminator or Discord's new username system
export function parseName(user: User) {
if (user.discriminator.length === 1) {
return user.username
} else {
return `${user.username}#${user.discriminator}`
}
}

export async function getGuildByID(id: string) {
const cache = client.guilds.get(id);
if (!cache) {
Expand Down
21 changes: 11 additions & 10 deletions src/utils/greeting.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Guild, Member, MemberPartial } from "eris";
import { client } from "../client/Client";
import { ISettingsDB } from "../interfaces/database";
import { parseName } from "./get";

export async function greeting(
type: "w" | "g",
Expand Down Expand Up @@ -29,16 +30,16 @@ export async function greeting(
let content = !w.message
? "Hello {mention} and welcome to the server! We now have {memberCount}!"
: w.message
.replace("{server}", guild.name)
.replace("{memberCount}", guild.memberCount.toString())
.replace(
"{name}",
`${member.user.username}#${member.user.discriminator}`,
)
.replace(
"{user}",
`${member.user.username}#${member.user.discriminator}`,
);
.replace("{server}", guild.name)
.replace("{memberCount}", guild.memberCount.toString())
.replace(
"{name}",
parseName(member.user),
)
.replace(
"{user}",
parseName(member.user),
);

if (type === "w") {
content = content.replace("{mention}", `<@${member.id}>`);
Expand Down
4 changes: 2 additions & 2 deletions src/utils/levelUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Message } from "eris";
import { client } from "../client/Client";
import { ILevelDB, ISettingsDB, IUserDB } from "../interfaces/database";
import { getMemberByID } from "./get";
import { getMemberByID, parseName } from "./get";
import { probability } from "./math";

export function formulaXP(level: number) {
Expand All @@ -17,7 +17,7 @@ export async function sendLvlUP(user: string, msg: Message, level: ILevelDB) {

let target = `<@${user}>`;
if (userPref?.noMentionOnLevelUP) {
target = `**${msg.author.username}#${msg.author.discriminator}**`;
target = `**${parseName(msg.author)}**`;
}

const roleRewards = levelDB.modules.level.roleRewards;
Expand Down

0 comments on commit aa1bd38

Please sign in to comment.