mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-03-16 22:21:51 +00:00
resolveUser: return null if the passed value is not a valid id
This commit is contained in:
parent
5215dd0738
commit
a641312853
6 changed files with 21 additions and 4 deletions
|
@ -6,6 +6,7 @@ export enum ERRORS {
|
||||||
INVALID_EMOJI,
|
INVALID_EMOJI,
|
||||||
NO_USER_NOTIFICATION_CHANNEL,
|
NO_USER_NOTIFICATION_CHANNEL,
|
||||||
INVALID_USER_NOTIFICATION_CHANNEL,
|
INVALID_USER_NOTIFICATION_CHANNEL,
|
||||||
|
INVALID_USER,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const RECOVERABLE_PLUGIN_ERROR_MESSAGES = {
|
export const RECOVERABLE_PLUGIN_ERROR_MESSAGES = {
|
||||||
|
@ -14,6 +15,7 @@ export const RECOVERABLE_PLUGIN_ERROR_MESSAGES = {
|
||||||
[ERRORS.INVALID_EMOJI]: "Invalid emoji",
|
[ERRORS.INVALID_EMOJI]: "Invalid emoji",
|
||||||
[ERRORS.NO_USER_NOTIFICATION_CHANNEL]: "No user notify channel specified",
|
[ERRORS.NO_USER_NOTIFICATION_CHANNEL]: "No user notify channel specified",
|
||||||
[ERRORS.INVALID_USER_NOTIFICATION_CHANNEL]: "Invalid user notify channel specified",
|
[ERRORS.INVALID_USER_NOTIFICATION_CHANNEL]: "Invalid user notify channel specified",
|
||||||
|
[ERRORS.INVALID_USER]: "Invalid user",
|
||||||
};
|
};
|
||||||
|
|
||||||
export class RecoverablePluginError extends Error {
|
export class RecoverablePluginError extends Error {
|
||||||
|
|
|
@ -12,6 +12,10 @@ export async function createCaseNote(pluginData: PluginData<CasesPluginType>, ar
|
||||||
}
|
}
|
||||||
|
|
||||||
const mod = await resolveUser(pluginData.client, args.modId);
|
const mod = await resolveUser(pluginData.client, args.modId);
|
||||||
|
if (!mod) {
|
||||||
|
throw new RecoverablePluginError(ERRORS.INVALID_USER);
|
||||||
|
}
|
||||||
|
|
||||||
const modName = `${mod.username}#${mod.discriminator}`;
|
const modName = `${mod.username}#${mod.discriminator}`;
|
||||||
|
|
||||||
let body = args.body;
|
let body = args.body;
|
||||||
|
|
|
@ -35,6 +35,7 @@ export const UnmuteCmd = modActionsCommand({
|
||||||
async run({ pluginData, message: msg, args }) {
|
async run({ pluginData, message: msg, args }) {
|
||||||
const user = await resolveUser(pluginData.client, args.user);
|
const user = await resolveUser(pluginData.client, args.user);
|
||||||
if (!user) return sendErrorMessage(pluginData, msg.channel, `User not found`);
|
if (!user) return sendErrorMessage(pluginData, msg.channel, `User not found`);
|
||||||
|
|
||||||
const memberToUnmute = await resolveMember(pluginData.client, pluginData.guild, user.id);
|
const memberToUnmute = await resolveMember(pluginData.client, pluginData.guild, user.id);
|
||||||
const mutesPlugin = pluginData.getPlugin(MutesPlugin);
|
const mutesPlugin = pluginData.getPlugin(MutesPlugin);
|
||||||
const hasMuteRole = memberToUnmute && mutesPlugin.hasMutedRole(memberToUnmute);
|
const hasMuteRole = memberToUnmute && mutesPlugin.hasMutedRole(memberToUnmute);
|
||||||
|
|
|
@ -20,6 +20,12 @@ export async function banUserId(
|
||||||
): Promise<BanResult> {
|
): Promise<BanResult> {
|
||||||
const config = pluginData.config.get();
|
const config = pluginData.config.get();
|
||||||
const user = await resolveUser(pluginData.client, userId);
|
const user = await resolveUser(pluginData.client, userId);
|
||||||
|
if (!user) {
|
||||||
|
return {
|
||||||
|
status: "failed",
|
||||||
|
error: "Invalid user",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// Attempt to message the user *before* banning them, as doing it after may not be possible
|
// Attempt to message the user *before* banning them, as doing it after may not be possible
|
||||||
let notifyResult: UserNotificationResult = { method: null, success: true };
|
let notifyResult: UserNotificationResult = { method: null, success: true };
|
||||||
|
|
|
@ -41,6 +41,11 @@ export async function muteUser(
|
||||||
}
|
}
|
||||||
|
|
||||||
const user = await resolveUser(pluginData.client, userId);
|
const user = await resolveUser(pluginData.client, userId);
|
||||||
|
if (!user) {
|
||||||
|
lock.unlock();
|
||||||
|
throw new RecoverablePluginError(ERRORS.INVALID_USER);
|
||||||
|
}
|
||||||
|
|
||||||
const member = await resolveMember(pluginData.client, pluginData.guild, user.id); // Grab the fresh member so we don't have stale role info
|
const member = await resolveMember(pluginData.client, pluginData.guild, user.id); // Grab the fresh member so we don't have stale role info
|
||||||
const config = pluginData.config.getMatchingConfig({ member, userId });
|
const config = pluginData.config.getMatchingConfig({ member, userId });
|
||||||
|
|
||||||
|
|
|
@ -972,8 +972,7 @@ export function resolveUserId(bot: Client, value: string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Just a user ID?
|
// Just a user ID?
|
||||||
const idMatch = value.match(/^\d+$/);
|
if (isValidSnowflake(value)) {
|
||||||
if (idMatch) {
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1000,12 +999,12 @@ export async function resolveUser<T>(bot, value) {
|
||||||
return new UnknownUser();
|
return new UnknownUser();
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we have the user cached, return that directly
|
|
||||||
const userId = resolveUserId(bot, value);
|
const userId = resolveUserId(bot, value);
|
||||||
if (!userId) {
|
if (!userId) {
|
||||||
return new UnknownUser({ id: userId });
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If we have the user cached, return that directly
|
||||||
if (bot.users.has(userId)) {
|
if (bot.users.has(userId)) {
|
||||||
return bot.users.get(userId);
|
return bot.users.get(userId);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue