Skip to content
This repository has been archived by the owner on Mar 18, 2024. It is now read-only.

Командер #109

Open
SocketSomeone opened this issue Feb 14, 2022 · 0 comments
Open

Командер #109

SocketSomeone opened this issue Feb 14, 2022 · 0 comments

Comments

@SocketSomeone
Copy link
Member

export class CommandService extends BaseService {
public async onMessage(message: Message) {

	const channel = message.channel;
	const guild = (channel as GuildChannel).guild;

	const sets = guild ? await this.guilds.get(guild) : new BaseSettings();
	const t = <TranslateFunc>((phrase, replacements) => i18n.__({ locale: sets.locale, phrase }, replacements));

	const ratelimit = await this.isRatelimited(message, t);

	if (ratelimit) {
		return;
	}

	if (guild) {
		let member = message.member;

		if (!member) {
			member = guild.members.get(message.author.id);
		}

		if (!member) {
			member = await guild.getRESTMember(message.author.id);
		}

		if (!member) {
			return;
		}

		if (
			!(
				member.id === guild.ownerID ||
				member.permission.has(GuildPermission.ADMINISTRATOR) ||
				this.client.config.ownerID === member.id
			)
		) {
			const missingPerms = cmd.userPermissions.filter(
				(p) => !(channel as GuildChannel).permissionsOf(member.id).has(p)
			);

			if (missingPerms.length > 0) {
				const missed = Object.entries(GuildPermission)
					.filter(([s, v]) => missingPerms.some((x) => x === v))
					.map(([s]) => `\`${s}\``)
					.join(', ');

				await this.msg.sendReply(message, {
					color: Color.RED,
					author: { name: t('error.missed.userpermissions.title'), icon_url: Images.CRITICAL },
					description: t('error.missed.userpermissions.desc', { missed }),
					footer: null,
					timestamp: null
				});

				return;
			}
		}

		context.me = guild.members.get(this.client.user.id);

		if (!context.me) {
			context.me = await guild.getRESTMember(this.client.user.id).catch(() => undefined);
		}

		const missingPerms = cmd.botPermissions.filter(
			(p) => !(channel as GuildChannel).permissionsOf(this.client.user.id).has(p)
		);

		if (missingPerms.length > 0) {
			const missed = Object.entries(GuildPermission)
				.filter(([s, v]) => missingPerms.some((x) => x === v))
				.map(([s]) => `\`${s}\``)
				.join(', ');

			await this.msg.sendReply(message, {
				color: Color.RED,
				author: { name: t('error.missed.botpermissions.title'), icon_url: Images.CRITICAL },
				description: t('error.missed.botpermissions.desc', { missed }),
				footer: null,
				timestamp: null
			});

			return;
		}
	}

	moment.locale(sets.locale);

	try {
		await cmd.execute(message, args, context);
	} catch (err) {
		if (err instanceof InternalError) {
			const embed = this.msg.createEmbed({
				author: { name: err.message, icon_url: err.isWarn ? Images.WARN : Images.CRITICAL },
				color: err.isWarn ? Color.YELLOW : Color.RED,
				footer: null,
				timestamp: null
			});

			if (err.fallbackUser) {
				await this.msg.sendEmbed(await message.author.getDMChannel(), embed);
			} else {
				await this.msg.sendReply(message, embed);
			}

			return;
		}

		await this.msg.sendReply(message, {
			author: { name: t('error.execCommand.title'), icon_url: Images.CRITICAL },
			description: t('error.execCommand.desc', {
				error: err.message
			}),
			color: Color.RED,
			footer: {
				text: t('error.execCommand.footer')
			}
		});
	}
}

private async isRatelimited(message: Message, t: TranslateFunc) {
	const now = moment().valueOf();

	let lastCall = this.commandCalls.get(message.author.id);

	if (!lastCall) {
		lastCall = {
			last: moment().valueOf(),
			warned: false
		};

		this.commandCalls.set(message.author.id, lastCall);
	} else if (now - lastCall.last < (1 / RATE_LIMIT) * 1000) {
		if (!lastCall.warned) {
			lastCall.warned = true;
			lastCall.last = now + COOLDOWN;

			await this.msg.sendReply(message, {
				color: Color.RED,
				author: { name: t('error.ratelimit.title'), icon_url: Images.CRITICAL },
				description: t('error.ratelimit.desc'),
				footer: null,
				timestamp: null
			});
		}

		return true;
	} else if (lastCall.warned) {
		lastCall.warned = false;
	}

	lastCall.last = now;

	return false;
}

}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant