From e4326ecb39d72d1f081780e4578e96f24c612f4c Mon Sep 17 00:00:00 2001 From: Someone Date: Wed, 3 Aug 2022 01:35:02 +0200 Subject: [PATCH] core/AbstractCommand.py --- core/AbstractCommand.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/core/AbstractCommand.py b/core/AbstractCommand.py index 20d8e0a..9fd0afd 100644 --- a/core/AbstractCommand.py +++ b/core/AbstractCommand.py @@ -3,6 +3,8 @@ # published under MIT-License +import mattermost + class RequireException(Exception): """Required precondition not met-exception. Mostly used for permission checks. Message will be forwarded to command-caller/user""" @@ -177,3 +179,18 @@ class AbstractCommand(): raise RequireException("### The bot cannot be used by guests. :(") return False return True + + + def _require_in_channel(self, in_channel, data, exc=True): + """ + Require to be in a channel. + Throws RequireException if not in the given channel. + """ + + try: + channel_member = self.bot.api.get_channel_member(in_channel, data["user_id"], exc=True) + except mattermost.ApiException as ex: + if exc: + raise RequireException("### You are not in the required channel :(") + return False + return True -- 2.43.0