Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Username Format configurable #144

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/data/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { GuildChannel, GuildMember, Role } from "discord.js";
export default class Database {
private db: Sequelize;

private Username = class extends Model {};
private Mod = class extends Model {};
private Channel = class extends Model {};
private Language = class extends Model {};
Expand Down Expand Up @@ -38,6 +39,29 @@ export default class Database {
},
);
}
this.Username.init(
{
/*id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},*/
serverId: {
type: DataTypes.STRING,
allowNull: false,
},
mode: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 4,
},
},
{
sequelize: this.db,
modelName: "username",
timestamps: false,
},
);
this.Mod.init(
{
/*id: {
Expand Down Expand Up @@ -223,6 +247,31 @@ export default class Database {
return await this.db.sync({ alter: true });
}

async getUsernameMode(serverId: string) {
return await this.Username.findOne({
where: {
serverId,
},
});
}

/**
* 0 = Nickname if exists -> Display Name if exists -> Username in any other case
* 1 = Nickname if exists -> Username in any other case
* 2 = Display Name if exists -> Username in any other case
* 3 = Display Name if exists -> Nickname if exists -> Username in any other case
* 4 = Always Username
*/
async setUsernameMode(serverId: string, mode: number) {
if (mode < 0 || mode > 4) throw new Error("mode must be between 0 and 4");
let oldMode = await this.getUsernameMode(serverId);
if (oldMode == null || oldMode == undefined) {
return await this.Username.create({ serverId, mode });
} else {
return await this.Username.update({ mode }, { where: { serverId } });
}
}

async addMod(mentionable: GuildMember | Role) {
const lang = await LanguageApi.getLanguage(mentionable.guild.id, this);
if (await this.isMod(mentionable)) {
Expand Down
20 changes: 19 additions & 1 deletion src/languages/de_DE.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@
"settings_language_unset_description_success": "Bevorzugte Sprache erfolgreich entfernt!",
"settings_language_unset_description_failed": "Entfernen der bevorzugten Sprache aufgrund von folgendem fehlgeschlagen: ",
"settings_language_show_title": "Aktuell bevorzugte Sprache",
"settings_username_set_title_success": "Nutzernamenformat gesetzt",
"settings_username_set_title_failed": "Nutzernamenformat setzen fehlgeschlagen",
"settings_username_set_description_success": "Nutzernamenformat erfolgreich gesetzt!",
"settings_username_set_description_failed": "Nutzernamenformat setzen aufgrund von folgendem fehlgeschlagen: ",
"settings_username_unset_title_success": "Nutzernamenformat zurückgesetzt",
"settings_username_unset_title_failed": "Zurücksetzen des Nutzernamenformats fehlgeschlagen",
"settings_username_unset_description_success": "Nutzernamenformat erfolgreich zurückgesetzt!",
"settings_username_unset_description_failed": "Zurücksetzen des Nutzernamenformats aufgrund von folgendem fehlgeschlagen: ",
"settings_username_show_title": "Current username format",
"settings_reset_title_success": "Einstellungen zurückgesetzt",
"settings_reset_description_success": "Einstellungen erfolgreich zurückgesetzt!",
"settings_reset_title_failed": "Zurücksetzen der Einstellungen fehlgeschlagen",
Expand All @@ -85,6 +94,10 @@
"settings_language_set": "Setzt die bevorzugte Sprache für den aktuellen Server",
"settings_language_unset": "Entfernt die bevorzugte Sprache für den aktuellen Server",
"settings_language_show": "Zeigt die aktuell bevorzugte Sprache für den aktuellen Server an",
"settings_username_help": "Hilfe zur Einstellung des Nutzernamenformats",
"settings_username_set": "Das Format für Nutzernamen außerhalb von @Erwähnungen setzen",
"settings_username_unset": "Das Format, in dem Nutzernamen außerhalb von @Erwähnungen angezeigt werden sollen, zurücksetzen",
"settings_username_show": "Zeigt das Format, in dem Nutzernamen außerhalb von @Erwähnungen angezeigt werden, an",
"settings_help": "Einstellungshilfe",
"settings_reset": "Alle Einstellungen zurücksetzen",
"settings_show": "Zeigt alle gesetzten Werte an Einstellungen an",
Expand Down Expand Up @@ -177,5 +190,10 @@
"position": "Platz",
"score": "Punktzahl",
"not_available": "N/A",
"championship": "Meisterschaft"
"championship": "Meisterschaft",
"username_mode_0": "Spitzname wenn existent -> Anzeigename wenn existent -> Nutzername",
"username_mode_1": "Spitzname wenn existent -> Nutzername",
"username_mode_2": "Anzeigename wenn existent -> Nutzername",
"username_mode_3": "Anzeigename wenn existent -> Spitzname wenn existent -> Nutzername",
"username_mode_4": "Immer Nutzername"
}
20 changes: 19 additions & 1 deletion src/languages/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@
"settings_language_unset_description_success": "Preferred Language successfully unset!",
"settings_language_unset_description_failed": "Unsetting Preferred Language failed due to: ",
"settings_language_show_title": "Currently preferred Language",
"settings_username_set_title_success": "Username format set",
"settings_username_set_title_failed": "Setting Username format failed",
"settings_username_set_description_success": "Username format successfully set!",
"settings_username_set_description_failed": "Setting Username format failed due to: ",
"settings_username_unset_title_success": "Username format unset",
"settings_username_unset_title_failed": "Unsetting username format failed",
"settings_username_unset_description_success": "Username format successfully unset!",
"settings_username_unset_description_failed": "Unsetting username format failed due to: ",
"settings_username_show_title": "Current username format",
"settings_reset_title_success": "Settings Reset",
"settings_reset_description_success": "Settings reset successfully!",
"settings_reset_title_failed": "Resetting Settings failed",
Expand All @@ -85,6 +94,10 @@
"settings_language_set": "Sets the preferred language for the current server",
"settings_language_unset": "Sets the preferred language for the current server",
"settings_language_show": "Shows the preferred language for the current server",
"settings_username_help": "Help for the settings regarding the username format",
"settings_username_set": "Sets the username format for strings outside of @mentions",
"settings_username_unset": "Reset how usernames outside of @mentions are shown to it's default format",
"settings_username_show": "Shows the current format for usernames outside of @mentions",
"settings_help": "Settings Help",
"settings_reset": "Reset all Settings",
"settings_show": "Shows all currently set values",
Expand Down Expand Up @@ -177,5 +190,10 @@
"position": "Position",
"score": "Score",
"not_available": "N/A",
"championship": "Championship"
"championship": "Championship",
"username_mode_0": "Nickname if exists -> Display Name if exists -> Username",
"username_mode_1": "Nickname if exists -> Username",
"username_mode_2": "Display Name if exists -> Username",
"username_mode_3": "Display Name if exists -> Nickname if exists -> Username",
"username_mode_4": "Always Username"
}
19 changes: 18 additions & 1 deletion src/languages/slash-commands/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@
"settings_language_show_description": "Zeigt die aktuell bevorzugte Serversprache an",
"settings_language_help_name": "hilfe",
"settings_language_help_description": "Zeigt Hilfe zu den Spracheinstellungen an",
"settings_username_name": "nutzername",
"settings_username_description": "Das Format für Nutzernamen außerhalb von @Erwähnungen verwalten",
"settings_username_set_name": "setzen",
"settings_username_set_description": "Das Format für Nutzernamen außerhalb von @Erwähnungen setzen ",
"settings_username_set_mode_name": "modus",
"settings_username_set_mode_description": "Das Format, in dem Nutzernamen außerhalb von @Erwähnungen angezeigt werden sollen",
"settings_username_unset_name": "zuruecksetzen",
"settings_username_unset_description": "Das Format, in dem Nutzernamen außerhalb von @Erwähnungen angezeigt werden, zurücksetzen",
"settings_username_show_name": "zeigen",
"settings_username_show_description": "Zeigt das Format, in dem Nutzernamen außerhalb von @Erwähnungen angezeigt werden, an",
"settings_username_help_name": "hilfe",
"settings_username_help_description": "Zeigt Hilfe für die Unterbefehle von /einstellungen nutzername an",
"settings_reset_name": "zurücksetzen",
"settings_reset_description": "Setzt alle aktuellen Einstellungen zurück",
"settings_help_name": "hilfe",
Expand All @@ -130,5 +142,10 @@
"timeout_show_name": "anzeigen",
"timeout_show_description": "Zeigt die Dauer des Auszeit eines Benutzers an",
"timeout_show_user_name": "benutzer",
"timeout_show_user_description": "Der Benutzer dessen Auszeit du sehen willst"
"timeout_show_user_description": "Der Benutzer dessen Auszeit du sehen willst",
"username_mode_0": "Spitzname wenn existent -> Anzeigename wenn existent -> Nutzername",
"username_mode_1": "Spitzname wenn existent -> Nutzername",
"username_mode_2": "Anzeigename wenn existent -> Nutzername",
"username_mode_3": "Anzeigename wenn existent -> Spitzname wenn existent -> Nutzername",
"username_mode_4": "Immer Nutzername"
}
19 changes: 18 additions & 1 deletion src/languages/slash-commands/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@
"settings_language_show_description": "Shows your currently set preferred language for this server",
"settings_language_help_name": "help",
"settings_language_help_description": "Shows help for language settings",
"settings_username_name": "username",
"settings_username_description": "Manage the format to use when refering to users without @mentions",
"settings_username_set_name": "set",
"settings_username_set_description": "Set the username format to use when refering to users without @mentions",
"settings_username_set_mode_name": "mode",
"settings_username_set_mode_description": "The format you want to set the username to be rendered",
"settings_username_unset_name": "unset",
"settings_username_unset_description": "Resets the format for usernames to be rendered to its default value",
"settings_username_show_name": "show",
"settings_username_show_description": "Shows the current username format",
"settings_username_help_name": "help",
"settings_username_help_description": "Shows help for the /settings unsername subcommands",
"settings_reset_name": "reset",
"settings_reset_description": "Resets all current settings",
"settings_help_name": "help",
Expand All @@ -130,5 +142,10 @@
"timeout_show_name": "show",
"timeout_show_description": "Shows a users timeout",
"timeout_show_user_name": "user",
"timeout_show_user_description": "The user whose timeout you want to see"
"timeout_show_user_description": "The user whose timeout you want to see",
"username_mode_0": "Nickname if exists -> Display Name if exists -> Username",
"username_mode_1": "Nickname if exists -> Username",
"username_mode_2": "Display Name if exists -> Username",
"username_mode_3": "Display Name if exists -> Nickname if exists -> Username",
"username_mode_4": "Always Username"
}
29 changes: 19 additions & 10 deletions src/leaderboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
User,
BaseInteraction,
SlashCommandBuilder,
GuildMember,
} from "discord.js";
import { Model } from "sequelize";
import Database from "./data/postgres";
Expand All @@ -24,6 +25,9 @@ export default class Leaderboard {
let userName = "";
let score = "";
let scores = await db.getScores(interaction.guildId!);
let usernameMode = (
await db.getUsernameMode(interaction.guildId!)
)?.getDataValue("mode");
const leaderboardEmbed: EmbedBuilder = Util.returnEmbed(
lang.obj["leaderboard_title"],
lang.obj["leaderboard_description"],
Expand All @@ -32,12 +36,14 @@ export default class Leaderboard {
// Add fields to Embed
for (let i: number = 0; i < Math.max(5, scores.length); i++) {
let userId = scores[i]?.getDataValue("userId");
let userObj = userId ? await Util.findUser(interaction, userId) : null;
if (userObj) {
if (Array.isArray(userObj)) {
userName = userObj[0].username;
let memberObj = userId
? await Util.findMember(interaction, userId)
: null;
if (memberObj) {
if (Array.isArray(memberObj)) {
userName = Util.getCorrectUsernameFormat(usernameMode, memberObj[0]);
} else {
userName = userObj.username;
userName = Util.getCorrectUsernameFormat(usernameMode, memberObj);
}
} else {
userName = userId;
Expand Down Expand Up @@ -107,6 +113,7 @@ export default class Leaderboard {
longestUserLength = await this.getLongestUsername(
interaction,
scores.slice(5),
usernameMode,
);
}
// Adds additional users into overflow leaderboard up until 20
Expand Down Expand Up @@ -135,16 +142,18 @@ export default class Leaderboard {
private static async getLongestUsername(
interaction: BaseInteraction,
scores: Model[],
usernameModeId: number,
) {
let longestUsernameLength: number = 0;
for (let i: number = 0; i < scores.length; i++) {
let u: User | undefined = (await Util.findUser(
let m: GuildMember | undefined = (await Util.findMember(
interaction,
scores[i].getDataValue("userId"),
)) as User | undefined;
if (u) {
if (u.username.length > longestUsernameLength) {
longestUsernameLength = u.username.length;
)) as GuildMember | undefined;
if (m) {
let memberUsername = Util.getCorrectUsernameFormat(usernameModeId, m);
if (memberUsername.length > longestUsernameLength) {
longestUsernameLength = memberUsername.length;
}
}
}
Expand Down
Loading