Пример #1
0
class Server:
    def __init__(self, appSettings: AppSettings):
        self.appSettings = appSettings
        self.chat = Chat(appSettings)
        self.database = Database(appSettings.mongoUri)

    def handleAppMention(self, payload):
        channel = payload["channel"]
        slackId = payload["user"]
        text = payload["text"]

        displayName = self.chat.getDisplayName(slackId)

        user = self.database.findUser(slackId)

        if not user:
            user = self.database.addUser(slackId, displayName)

        if not user:
            return

        self.parseAppMention(text, user)

        #message     = "Hello " + displayName + ", nice to meet you! Use /musicbotinfo to learn more about me!"
        #status      = self.chat.sendMessage     (channel, message)

    def parseAppMention(self, text: str, user: User) -> None:
        tokenCreate = "create list"
        tokenAdd = "add "
        tokenRemove = "remove "
        tokenToList = "to list "
        tokenFromList = "from list "

        tokenIndex = text.find(tokenCreate)

        if tokenIndex != -1:
            listNameIndex = tokenIndex + len(tokenCreate)
            listName = text[listNameIndex:]

            listName = listName.rstrip()
            listName = listName.lstrip()

            listName = listName.lower()

            self.database.addList(user.id, listName)

            return

        list, item = self.getListAndItem(text, tokenAdd, tokenToList)

        if list is not None and item is not None:
            self.database.addItemToList(item, list)

        list, item = self.getListAndItem(text, tokenRemove, tokenFromList)

        if list is not None and item is not None:
            return

    def getListAndItem(self, text: str, tokenFirst: str,
                       tokenSecond: str) -> Tuple[str, str]:
        tokenFirstIndex = text.find(tokenFirst)

        if tokenFirstIndex != -1:
            tokenSecondIndex = text.find(tokenSecond)

            if tokenSecondIndex != -1:
                listName = text[tokenSecondIndex + len(tokenSecond):]
                itemName = text[tokenFirstIndex +
                                len(tokenFirst):tokenSecondIndex - 1]

                listName = listName.rstrip()
                itemName = itemName.rstrip()

                return listName, itemName

        return None, None
Пример #2
0
class Server:
    def __init__(self, appSettings):
        self.appSettings = appSettings
        self.name = "MusicBot"
        self.chat = Chat(appSettings)
        self.spotify = SpotifyApi(appSettings)
        self.users = Users()

    def handleAppMention(self, payload):
        channel = payload['channel']
        userId = payload['user']
        blocks = payload['blocks']

        messageText = ""

        displayName = self.chat.getDisplayName(userId)

        message = "Hello " + displayName + ", nice to meet you! Use /musicbotinfo to learn more about me!"
        status = self.chat.sendMessage(channel, message)

    def handleLinkPosted(self, payload):
        if self.checkLastPostDate() == False:
            return

        channel = payload['channel']
        userId = payload['user']

        if self.chat.checkInChannel(channel) == False:
            return

        urls = []

        links = payload["links"]

        for link in links:
            domain = link["domain"]
            url = link["url"]

            if domain == "open.spotify.com":
                urls.append(url)

        success = True

        for url in urls:
            success = self.spotify.postSongToPlaylist(url)

        #if success == True and self.users.doesUserExist(userId) == False:
        #displayName = self.chat.getDisplayName(userId)
        #message     = "Hello " + displayName + ", I saw this was your first time posting since I have been active. I have archived this song for you! You can find the link using the /spotify command!"
        #status      = self.chat.sendMessage     (channel, message)

        #self.users.addExistingUser(userId)

    def checkLastPostDate(self):
        today = datetime.today()

        if today.month != self.appSettings.spotifyLastPostDate.month:
            todayString = str(today.day) + "-" + str(today.month) + "-" + str(
                today.year)

            #create new playlist
            newPlaylistId, newPlaylistLink = self.spotify.createNewPlaylist()

            if newPlaylistId != "" and newPlaylistLink != "":
                #reset last date in app settings
                self.appSettings.updateSetting("SPOTIFY", "playlistId",
                                               newPlaylistId)
                self.appSettings.updateSetting("SPOTIFY", "playlistLink",
                                               newPlaylistLink)
                self.appSettings.updateSetting("SPOTIFY", "lastPostDate",
                                               todayString)

                return True

            else:
                return False

    def handleSlashRequest(self, slashType):
        response = {}
        response["blocks"] = []

        section = {}
        section["type"] = "section"
        section["text"] = {}
        section["text"]["type"] = "mrkdwn"

        if slashType == SlashRequests.Github:
            section["text"]["text"] = self.appSettings.githubLink

        elif slashType == SlashRequests.Spotify:
            section["text"]["text"] = self.appSettings.spotifyPlaylistLink

        else:
            section["text"][
                "text"] = "A bot designed so you can find all the music posted here in one place\nCommands\n\t/spotify\n\t/musicbotinfo\n\t/github\nDesigned and \"tested\" by Alex Gray"

        response["blocks"].append(section)

        return response