Files
discordeno/src/helpers/misc/editBotProfile.ts
2021-12-06 00:39:11 +00:00

36 lines
1.4 KiB
TypeScript

import { Errors } from "../../types/discordeno/errors.ts";
import type { User } from "../../types/users/user.ts";
import type { Bot } from "../../bot.ts";
/** Modifies the bot's username or avatar.
* NOTE: username: if changed may cause the bot's discriminator to be randomized.
*/
export async function editBotProfile(bot: Bot, options: { username?: string; botAvatarURL?: string | null }) {
// Nothing was edited
if (!options.username && options.botAvatarURL === undefined) return;
// Check username requirements if username was provided
if (options.username) {
if (options.username.length > 32) {
throw new Error(Errors.USERNAME_MAX_LENGTH);
}
if (options.username.length < 2) {
throw new Error(Errors.USERNAME_MIN_LENGTH);
}
if (["@", "#", ":", "```"].some((char) => options.username!.includes(char))) {
throw new Error(Errors.USERNAME_INVALID_CHARACTER);
}
if (["discordtag", "everyone", "here"].includes(options.username)) {
throw new Error(Errors.USERNAME_INVALID_USERNAME);
}
}
const avatar = options?.botAvatarURL ? await bot.utils.urlToBase64(options?.botAvatarURL) : options?.botAvatarURL;
const result = await bot.rest.runMethod<User>(bot.rest, "patch", bot.constants.endpoints.USER_BOT, {
username: options.username?.trim(),
avatar,
});
return bot.transformers.user(bot, result);
}